Repository navigation
Open
Labels
KeepOpenThe bot will ignore these and not auto-close
Description
Steps to reproduce
New-Module m {
class c {
[object] Invoke () { return Invoke-Something }
}
function Invoke-Something { 'Invoke-Something' }
function New-InstanceOfC { [c]::new() }
function Invoke-MethodOfC { (New-InstanceOfC).Invoke() }
Export-ModuleMember *OfC
} | Out-Null
(New-InstanceOfC).Invoke()
Invoke-MethodOfCExpected behavior
I expected both calls to Invoke-Something to succeed and output
Invoke-Something
Invoke-Something
Actual behavior
Neither call to Invoke-Something succeeds.
Invoke-Something : The term 'Invoke-Something' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\un1\Desktop\test.ps1:3 char:37
+ [object] Invoke () { return Invoke-Something }
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-Something:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Invoke-Something : The term 'Invoke-Something' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\un1\Desktop\test.ps1:3 char:37
+ [object] Invoke () { return Invoke-Something }
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-Something:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.0-beta
PSEdition Core
GitCommitId v6.0.0-beta.6
OS Microsoft Windows 6.3.9600
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Metadata
Metadata
Assignees
Labels
KeepOpenThe bot will ignore these and not auto-close
Activity
mklement0 commentedon Sep 1, 2017
I can't speak to the underlying design issues (how things should work in this case), but I can offer a pointer:
Your code would work if you put the module code into a
*.psm1file instead and import it withImport-Moduleorusing module.Note that only with
using moduleis the module's custom class truly imported into the caller's scope and accessible as[c](modules neither by default export custom classes nor allow their explicit exporting; they're imported (implicitly) only with ausing modulestatement, which is used at parse time and must be the very 1st statement in the script).alx9r commentedon Sep 1, 2017
Interesting. I confirmed this behavior. I wonder if there is a good reason for the discrepancy in behaviors between the
.psm1-backed and dynamic modules.rjmholt commentedon Apr 19, 2018
Dongbo Wang (@daxian-dbw) I remember discussing this with you. What are the exact mechanics here?
daxian-dbw commentedon May 2, 2018
This issue is a duplicate of #2841
All classes in a script block, including those in the nested script blocks, are emitted when the top level script block (
scriptblockast.Parent == null) is compiled. After emitting the dyanmic types, the compiler will generate code to initialize the produced types when the top-level script block runs, of which one task is to associate thsoe types with the current active session state, so instances of those types can run methods using that session state.This casues a problem to
New-Module m { class c { ... } }, where the classcis supposed to be associated with the module session state when it's evaluated withinNew-Module, but it has already been associated with the active session state when running the top-level scirpt blockNew-Module.The instance of
foocannot findInvoke-Somethingbecause it's not using the module session state when runningInvoke().When
Invoke()runs, it's actually using the outer session state to resolve the command, and in this case, ifInvoke-Somethingis defined in the scope chain in the outer session state, then it will find it. See the below code for comparison.microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.