Skip to content

Reloading module does not reload submodules. #2505

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-Sub

Value : 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                                                                                                                                                     


Activity

  1. added
    WG-Engine
    on Oct 20, 2016
  2. daxian-dbw commented on Oct 20, 2016

    @daxian-dbw
    Member

    Hi Staffan Gustafsson (@powercode), I'm unable to reproduce this issue in both powershell 5.1 and latest powershell core:

    powershell 5.1

    PS:3> dir
    
        Directory: C:\arena\tmp
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----       10/19/2016   9:16 PM            127 modrepo.psm1
    -a----       10/19/2016   9:16 PM             74 Sub.psm1
    
    PS:4> v .\Sub.psm1
    PS:5> v .\modrepo.psm1
    PS:6> Import-Module .\modrepo.psm1
    PS:7> Get-Sub
    
    Value
    -----
       42
    
    PS:8> v .\Sub.psm1
    PS:9> v .\modrepo.psm1
    PS:10> Remove-Module modrepo -Force
    PS:11> Import-Module .\modrepo.psm1 -Force
    PS:12>
    PS:12> Get-Sub
    
    Name    Value
    ----    -----
    Staffan    42
    
    PS:13> $PSVersionTable
    
    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
    

    powershell core

    PS C:\arena\tmp> dir
    
        Directory: C:\arena\tmp
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----       10/19/2016   9:15 PM            128 modrepo.psm1
    -a----       10/19/2016   9:15 PM             75 Sub.psm1
    
    PS C:\arena\tmp> Import-Module .\modrepo.psm1
    PS C:\arena\tmp>
    PS C:\arena\tmp> Get-Sub
    
    Value
    -----
       42
    
    PS C:\arena\tmp>
    PS C:\arena\tmp> Remove-Module modrepo -Force
    PS C:\arena\tmp> Import-Module .\modrepo.psm1 -Force
    PS C:\arena\tmp>
    PS C:\arena\tmp> Get-Sub
    
    Name    Value
    ----    -----
    Staffan    42
    

    Am I missing anything? Are you able to reproduce the issue in the latest alpha.11 release of powershell core?

  3. powercode commented on Oct 20, 2016

    @powercode
    CollaboratorAuthor

    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.

  4. LaurentDardenne commented on Oct 20, 2016

    @LaurentDardenne

    I can reproduce this issue on Windows Seven x64 & x86 with PS v5.0.

    remove-item c:\temp\Sub.psm1 -ea Silently
    remove-item c:\temp\modrepo.psm1 -ea Silently 
    @'
      class Sub {
        [int] $Value
      }
    '@ >c:\temp\Sub.psm1
    
    @'
      using module c:\temp\Sub.psm1
    
      [sub]::new()|gm > c:\temp\c1.txt
    
      function Get-Sub { 
         [Sub]@{
             Value = 42
         }
      }
    '@ > C:\temp\modrepo.psm1
    
    Set-location c:\temp
    Import-Module c:\temp\modrepo.psm1
    Get-Sub
    
    @'
      class Sub {  
        [string] $Name # added field
        [int] $Value
      }
    '@ >c:\temp\Sub.psm1
    
    @'
      using module c:\temp\Sub.psm1
    
      [sub]::new()|gm > c:\temp\c2.txt
    
      function Get-Sub{ 
         [Sub]@{
             Name = ‘Staffan’ # added field
             Value = 42
         }
      }
    '@ > C:\temp\modrepo.psm1
    
    Set-location c:\temp
    Remove-Module -force modrepo
    Import-Module -force c:\temp\modrepo.psm1
    Get-Sub 
    #-> Exception
    
    fc.exe c:\temp\c2.txt c:\temp\c1.txt
    # Equal
    # Comparaison des fichiers C:\TEMP\c2.txt et C:\TEMP\C1.TXT
    # FC : aucune différence trouvée

    $PSVersionTable

    Name                           Value
    ----                           -----
    PSVersion                      5.0.10586.117
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0.10586.117}
    BuildVersion                   10.0.10586.117
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    

    $StackTrace

       à System.Management.Automation.LanguagePrimitives.CreateMemberNotFoundError(PSObject pso, DictionaryEntry property, T
    ype resultType)
       à System.Management.Automation.LanguagePrimitives.SetObjectProperties(Object o, IDictionary properties, Type resultTy
    pe, MemberNotFoundError memberNotFoundErrorAction, MemberSetValueError memberSetValueErrorAction, Boolean enableMethodCa
    ll, IFormatProvider formatProvider, Boolean recursion, Boolean ignoreUnknownMembers)
       à System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type
     resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable,
    Boolean ignoreUnknownMembers)
    
  5. powercode commented on Oct 20, 2016

    @powercode
    CollaboratorAuthor

    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?

    [datetime]::Now
    $modPath = '~/documents/windowspowershell/modules/modrepo/'
    $null = mkdir $modPath -ea:0
    
    
    Set-Content $modPath/modrepo.psm1 -Value @'
    using module .\Sub.psm1
    function Get-Sub{  
      $s = [Sub]::new(); 
      $s | get-Member | Format-Table 
      $s.ToString();
      $s.GetType().Assembly.Modules.ModuleVersionId 
    }
    '@
    
    $sub1 = @'
    class Sub {
      [int] $Value 
      #[string] $Name
      Sub(){
        $this.Value = 42
        #$this.Name='Answer'
      }     
      [string] ToString() {return 'I am the first version'}
    }   
    '@
    
    $sub2 = @'
    class Sub {
      [int] $Value 
      [string] $Name
      Sub(){
        $this.Value = 42
        $this.Name='Answer'
      }     
      [string] ToString() { return 'I am new and improved' }
    }   
    '@
    
    
    Set-Content $modPath/sub.psm1 -Value $sub1
    Trace-Command -Expression {Import-Module -Force modrepo} -PSHost Modules
    Get-Sub
    
    # now change the definition of sub.psm1
    Set-Content $modPath/sub.psm1 -Value $sub2
    Trace-Command -Expression {Import-Module -Force modrepo} -PSHost Modules
    Get-Sub

    The output is as follows. Note that the definitions of Sub has not been been updated and the same dynamic assembly is used.

    den 20 oktober 2016 11:55:25
    DEBUG: Modules Information: 0 :  WriteLine   C:\Users\powercode\Documents\WindowsPowerShell\Modules\modrepo\Sub.psm1: cache entry out of date, cached on 2016-10-20 11:29:20, last updated on 2016-10-20 11:55:25
    DEBUG: Modules Information: 0 :  WriteLine   Returning NULL for exported commands.
    DEBUG: Modules Information: 0 :  WriteLine   Analyzing path: C:\Users\powercode\Documents\WindowsPowerShell\Modules\modrepo\Sub.psm1
    DEBUG: Modules Information: 0 :  WriteLine   Requested caching for Sub
    DEBUG: Modules Information: 0 :  WriteLine   Caching command: Sub
    DEBUG: Modules Information: 0 :  WriteLine   Requested caching for Sub
    DEBUG: Modules Information: 0 :  WriteLine   Existing cached info up-to-date. Skipping.
    DEBUG: Modules Information: 0 :  WriteLine   Requested caching for modrepo
    DEBUG: Modules Information: 0 :  WriteLine   Caching command: Get-Sub
    DEBUG: Modules Information: 0 :  WriteLine   Caching command: Sub
    
    
    
    
       TypeName: Sub
    
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString    Method     string ToString()
    Value       Property   int Value {get;set;}
    
    
    I am the first version
    
    DEBUG: Modules Information: 0 :  WriteLine   Requested caching for Sub
    DEBUG: Modules Information: 0 :  WriteLine   C:\Users\powercode\Documents\WindowsPowerShell\Modules\modrepo\Sub.psm1: cache entry out of date, cached on 2016-10-20 11:55:25, last updated on 2016-10-20 11:55:31
    DEBUG: Modules Information: 0 :  WriteLine   Caching command: Sub
    DEBUG: Modules Information: 0 :  WriteLine   Requested caching for modrepo
    DEBUG: Modules Information: 0 :  WriteLine   Existing cached info up-to-date. Skipping.
    Guid
    ----
    fa9abef1-c3b5-4c5f-88f2-aa11f6a8d427
    
    
    
    
       TypeName: Sub
    
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString    Method     string ToString()
    Value       Property   int Value {get;set;}
    
    
    I am the first version
    
    Guid
    ----
    fa9abef1-c3b5-4c5f-88f2-aa11f6a8d427```
    
  6. iSazonov commented on Oct 21, 2016

    @iSazonov
    Collaborator

    I confirm Laurent Dardenne (@LaurentDardenne)'s test on:

    $PSVersionTable
    
    Name                           Value
    ----                           -----
    WSManStackVersion              3.0
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   3.0.0.0
    PSEdition                      Core
    SerializationVersion           1.1.0.1
    PSVersion                      6.0.0-alpha
    GitCommitId                    v6.0.0-alpha.11-39-g0104d205a46846b9df0ef4940eedb226d21324dc
    PSRemotingProtocolVersion      2.3
    CLRVersion
    
  7. powercode commented on Oct 21, 2016

    @powercode
    CollaboratorAuthor

    Modrepo.ps1.txt

    I can reproduce the problems on WSL

    Name                           Value
    ----                           -----
    PSVersion                      6.0.0-alpha
    PSEdition                      Core
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   3.0.0.0
    GitCommitId                    v6.0.0-alpha.11
    CLRVersion
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    
  8. iSazonov commented on Oct 21, 2016

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw)

    Am I missing anything?

    Do you modify submodule before second test?

  9. powercode commented on Oct 21, 2016

    @powercode
    CollaboratorAuthor

    Ilya (@iSazonov)
    Yes, he got

    Value
    -----
       42
    

    the first run and

    Name    Value
    ----    -----
    Staffan    42
    

    the second.

    So there are some other factors involved here too.

  10. powercode commented on Oct 21, 2016

    @powercode
    CollaboratorAuthor

    Modrepo.Tests.ps1.txt

    Attaching a pester test. It is failing on

    PSVersion     BuildVersion   PSEdition GitCommitId            
    ---------     ------------   --------- -----------            
    6.0.0-alpha   3.0.0.0        Core      v6.0.0-alpha.11 wsl (Ubuntu 14)  
    6.0.0-alpha   3.0.0.0        Core      v6.0.0-alpha.11 windows
    5.1.14393.206 10.0.14393.206 Desktop                          
    
  11. joeyaiello commented on Oct 25, 2016

    @joeyaiello
    Contributor

    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?

  12. powercode commented on Oct 31, 2016

    @powercode
    CollaboratorAuthor

    Are you saying that you can't repro this?
    I have no machine where it works.

  13. pcgeek86 commented on Oct 31, 2016

    @pcgeek86

    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).

  14. daxian-dbw commented on Oct 31, 2016

    @daxian-dbw
    Member

    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.

  15. 76 remaining items

  16. iSazonov commented on Aug 7, 2021

    @iSazonov
    Collaborator

    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).

  17. added
    Review - Committee
    and removed
    Committee-Reviewed
    on Aug 7, 2021
  18. rjmholt commented on Aug 19, 2021

    @rjmholt
    Collaborator

    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:

    • Adding a new switch to Import-Module (or just changing the behaviour of Import-Module -Force) wouldn't change the situation with PowerShell classes, which use using module, or with DLLs, which can't be unloaded/reloaded currently
    • Adding a new switch to the module cmdlets is also quite complex given the complexity of their implementation and need for expanded testing, so the risk of regression is higher than we'd like given what it buys
    • This is not a scenario that's expected to be encountered for most users, since you have to be editing a psm1 file and reloading it to see an issue here (i.e. be a module developer)
    • You can achieve what's needed here today by composing two existing commands: Remove-Module and Import-Module

    Given 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:

    • I've added this for Engine WG discussion, since I think that's now the best forum for this. We'll discuss that in our next meeting and delay closing until then.
    • If the module cache issue still exists, that is a bug and should be fixed
    • There was some discussion around a developer-mode style setting where (for example) modules are always reloaded. It might be worth continuing that discussion in a separate issue
    • The DLL reloading situation could possibly be improved with module ALC isolation, but it's complicated, comes with its own set of issues, and isn't likely to be in an initial implementation for those reasons
  19. removed
    Review - Committee
    on Aug 19, 2021
  20. added
    Resolution-No Activity
    on Nov 16, 2023
  21. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  22. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  23. DennisL68 commented on Aug 19, 2024

    @DennisL68

    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 ?

  24. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugKeepOpenWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions