New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defining a PowerShell class in a script causes a parser error if the class references external types that aren't currently loaded #3641
Comments
The issue is that PS class definitions are parsed before script execution begins, which means that any types referenced in class definitions must currently already be loaded in the context of the caller. In other words: your script breaks in the parsing stage, and no statements are ever executed. I presume this is a bug; if it isn't, it's certainly unexpected and an inconvenience. The following script demonstrates the problem more succinctly: # Module PSQuickGraph can be installed with
# Install-Module -Scope CurrentUser PSQuickGraph
# Importing / `using` it loads type [PSGraph.PSGraphVertex], among others,
# but that is currently NOT (early) enough for such types to be recognized
# in the class definition below.
using module PSQuickGraph
# !! Because type [PSGraph.PSGraphVertex] is referenced in the context of a
# !! *PS class definition*, this only works if the module that loads that type
# !! was imported *before* the script is invoked, because PS classes are
# !! parsed *before* script execution begins.
# !! If you execute `Import-Module PSQuickGraph` (or `using module PSQuickGraph`)
# !! *before* invoking this script, it will work.
class MyType {
[string] Foo() { return [PSGraph.PSGraphVertex].FullName }
}
# Assuming that the script doesn't break in the parsing stage,
# this always works, because by the time this statement is reached,
# the `using module` statement has already taken effect.
[PSGraph.PSGraphVertex].FullName Can I suggest you update the title and body of your original post to focus on the crux of the issue? |
It is by design that we detect errors at parse time with classes. Discovering types in binary modules or assemblies is not yet implemented. Whomever picks this up - we do not want to load assemblies at parse time - parsing is considered safe - it doesn't execute any code - and loading an assembly can execute code. Also related issue #2074 - but in that case, it's not |
Is this title reflects the issue more precisely? |
This comment has been minimized.
This comment has been minimized.
Ran into this issue with a GUI project I've been working on. Loaded |
This is made possible by dotnet/corefx#33201 |
When will this happen? It is 6 years now... |
As mr Klement already knows, i stumbled across this problem while was having troubles with a script because i couldn't load an external (system) before referencing them, which leads to a parsing error ( https://stackoverflow.com/questions/75770407/ ). I came here with the brilliant idea of suggesting something like the
could this be relevant? And aside from this, is there any update at all on the matter?
|
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
@PowerShellTeam @SteveL-MSFT Please re-open issue as it was not solved |
I found this issue linked from the excellent StackOverflow post PowerShell: Unable to find type when using PS 5 classes and wanted to share how it's complicating something I'm trying to do that I hoped would be quite simple. I am trying to write a script that runs different code based on $PSEdition with a simple "if" statement. The "if ($PSEdition -eq 'Desktop')" statement attempts to load a class definition, ICertificatePolicy, that is available in PS Desktop (5) but not PS Core (6+). The code with this class definition is not executed in the "elseif ($PSEdition -eq 'Core')" branch, yet the code won't run at all as PS Core throws:
Below is a code sample to reproduce. It runs in PS 5 without error but fails to execute with a ParserError in PS Core. No normal PowerShell error handling methods I've tried like Try/catch, -ErrorAction Ignore, etc. seem to affect this blocking ParserError.
As a workaround I might end up writing three scripts, a starting one to check $PSEdition, then conditionally execute either .\PSDesktop_Version.ps1 or .\PSCore_Version.ps1. That feels like an unnecessarily complicated way to have to handle an error for code that never runs. |
Strange behavior with "using module"
Steps to reproduce
Install-Module psquickgraph -Scope CurrentUser
save the following script to c:\temp\processgraph.ps1
Expected behavior
a file c:\temp\testGraph.gv should be generated.
Actual behavior
script fails with the following message
Environment data
Name Value
PSVersion 5.1.14393.953
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.953
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Source code for PSQuickGraph is here
The text was updated successfully, but these errors were encountered: