Repository navigation
Open
Description
Steps to reproduce
#Sub.psm1:
class Sub {
[int] $Value
}
#modrepo.psm1:
using module .\Sub.psm1
function Get-Sub {
[Sub]@{
Value = 42
}
}
Import-Module modrepo
Get-SubValue : 1
Change the definition of Sub:
#Sub.psm1:
class Sub {
[string] $Name # added field
[int] $Value
}
#modrepo.psm1:
using module .\Sub.psm1
function Get-Sub{
[Sub]@{
Name = ‘Staffan’ # added field
Value = 42
}
}
#Then
Remove-Module -force modrepo
Import-Module -force modrepo
Get-Sub Expected behavior
output of object of class Sub with
Name : 'Staffan'
Value : 42
Actual behavior
Cannot create object of type "Sub". The Name property was not found for the Sub object. The available property is: [Value <System.Int32>]
At C:\Users\<user>\Documents\WindowsPowerShell\Modules\modrepo\modrepo.psm1:9 char:3
+ [Sub] @{
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ObjectCreationError
Environment data
Name Value
---- -----
PSVersion 5.1.14393.206
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.206
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Reacted by Ethan Edwards, Petr Stupka, J, Alex Angas, Scott Walters, Tom Winter, Oblomoff and Andrei
Activity
daxian-dbw commentedon Oct 20, 2016
Hi Staffan Gustafsson (@powercode), I'm unable to reproduce this issue in both powershell 5.1 and latest powershell core:
powershell 5.1
powershell core
Am I missing anything? Are you able to reproduce the issue in the latest alpha.11 release of powershell core?
powercode commentedon Oct 20, 2016
I was actually a bit surprised when I created this repro that it failed the first time. I usually get this issue intermittently but it consistently is a pain when using classes in modules. I often end up restarting powershell.exe or the ise to get back to working modules.
I'll try installing the alpha and see if I can repro it there too.
LaurentDardenne commentedon Oct 20, 2016
I can reproduce this issue on Windows Seven x64 & x86 with PS v5.0.
$PSVersionTable
$StackTrace
powercode commentedon Oct 20, 2016
I did the following with trace-command to see module logging (still on the desktop):
Anything else you can do to trace the caching of modules?
The output is as follows. Note that the definitions of Sub has not been been updated and the same dynamic assembly is used.
iSazonov commentedon Oct 21, 2016
I confirm Laurent Dardenne (@LaurentDardenne)'s test on:
powercode commentedon Oct 21, 2016
Modrepo.ps1.txt
I can reproduce the problems on WSL
iSazonov commentedon Oct 21, 2016
Dongbo Wang (@daxian-dbw)
Do you modify submodule before second test?
powercode commentedon Oct 21, 2016
Ilya (@iSazonov)
Yes, he got
the first run and
the second.
So there are some other factors involved here too.
powercode commentedon Oct 21, 2016
Modrepo.Tests.ps1.txt
Attaching a pester test. It is failing on
joeyaiello commentedon Oct 25, 2016
sergei (@vors): Do you know if this is a known problem or not? I vaguely remember a conversation about classes getting cached in perpetuity for a given runspace?
powercode commentedon Oct 31, 2016
Are you saying that you can't repro this?
I have no machine where it works.
pcgeek86 commentedon Oct 31, 2016
Does this have to do with the inability to unload an assembly from an AppDomain, perhaps? What does the output of
[System.AppDomain]::CurrentDomain.GetAssemblies()look like, after each execution? If I recall correctly PowerShell generates dynamic, in-memory assemblies that only last for the lifetime of the process (or maybe runspace, as Joey mentioned).pcgeek86 commentedon Oct 31, 2016
Further reading on .NET: http://stackoverflow.com/questions/123391/how-to-unload-an-assembly-from-the-primary-appdomain
daxian-dbw commentedon Oct 31, 2016
Thank you Staffan Gustafsson (@powercode)! I'm able to reproduce the issue with your script Modrepo.Tests.ps1.txt. I will investigate further to find the root cause.
76 remaining items
iSazonov commentedon Aug 7, 2021
If Force reloads only main module and we need new option to reload submodules too we could use more informative name for the new switch like ReloadSubmodules. The switch could be implemented as dynamic parameter in addition to Force:
Import-Module name -Force -ReloadSubmodules.Alternative proposal: ReloadDependencies if we want to reload classes too as requested in #7654.
Ask PowerShell-Committee to revisit #2505 (comment).
rjmholt commentedon Aug 19, 2021
So we reviewed this on the @PowerShell/powershell-committee yesterday and our thinking is that implementing the original ask here doesn't address most of the actual issues people have in the comments:
Import-Module(or just changing the behaviour ofImport-Module -Force) wouldn't change the situation with PowerShell classes, which useusing module, or with DLLs, which can't be unloaded/reloaded currentlyRemove-ModuleandImport-ModuleGiven this, plus the historical notes and how long this issue has been open, the consensus was that we should close this issue as by design.
With that said, from me personally:
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.
DennisL68 commentedon Aug 19, 2024
So what was the fix or work around for this issue?
It just seems to have been auto closed by a bot...
Is this issue by design in .Net, as suggested at https://stackoverflow.com/a/77613311/8014824 ?