PowerShell/Polaris

Unable to find type [PolarisResponse] when using static route

TylerSiegrist opened this issue · 4 comments

This issue occurs in the repository from @Tiberriver256 . This does occur in the default example.ps1 when accessing index.html.

A script block in New-PolarisStaticRoute references the PolarisResponse class but it is unable to find that type when called unless the module is loaded outside the script or the "using module .\Polaris.psd1" command is called to load the classes prior to starting polaris.

PSVersionTable included below:

PSVersion 5.1.16299.251
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.251
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Hey thanks for moving this over Tyler. This one really has me curious. You're version is fine so that shouldn't be an issue.

I honestly had lots of trouble getting this to work myself. Getting the classes to be available outside of the module without the "using module" is kind of squirelly. What I ended up doing that has been working is to put the class files in the ScriptsToProcess section of the psd1. This should executes each of the class files in the current scope before importing the psm1 file which means the classes should get loaded into your scope.

I know you said you're using the example so you are importing the psd1 and not the psm1. I wonder if the ScriptsToProcess is not getting called for some reason?

If you're handy with ProcMon a recording of PowerShell when you're running the Import-Module command would really clear up if it's running ScriptsToProcess or not but I get the feeling it's not.

@tylerl0706 - I'm wondering on this if we should just point out in documentation to load it by doing "using module" instead of import-module because of the PowerShell classes. It does seem to be a more reliable way to get the classes exported. If you have any other ideas on it though let me know.

Some Research References

https://stackoverflow.com/questions/47499240/powershell-not-always-running-scriptstoprocess-on-import -- Looks like someone else had the problem at least
PowerShell/PowerShell#3738 -- Some bug that looks like it was fixed in core but not in regular PowerShell for when parameters are used with Import-Module. Maybe removing -Name from the Import-Module command would make it work?

Yeah this is a known issue. @rjmholt and I saw a bunch of issues with classes at PSHSummit last week.

using module is the way to go. I don't really know of a better way off the top of my head.

Yeah, PowerShell classes are compiled to .NET IL. So to make that more efficient, that's done when the script is parsed rather than when it's executed. So Import-Module hasn't run when the class needs to get compiled, meaning the class isn't known to exist and you get an error.

using module is designed to be a parse-time way of pulling a module in so that the parser/compiler in PowerShell can find the class at parse-time and pull it in. PowerShell/PowerShell#6652 is probably the best explanation of this (and links to a few other discussions about it and similar behaviour).

Thanks Rob :)