{"action":"created","issue":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2505","repository_url":"https://api.github.com/repos/PowerShell/PowerShell","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2505/labels{/name}","comments_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2505/comments","events_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2505/events","html_url":"https://github.com/PowerShell/PowerShell/issues/2505","id":183901892,"number":2505,"title":"Reloading module does not reload submodules.","user":{"login":"powercode","id":3505151,"avatar_url":"https://avatars.githubusercontent.com/u/3505151?v=3","gravatar_id":"","url":"https://api.github.com/users/powercode","html_url":"https://github.com/powercode","followers_url":"https://api.github.com/users/powercode/followers","following_url":"https://api.github.com/users/powercode/following{/other_user}","gists_url":"https://api.github.com/users/powercode/gists{/gist_id}","starred_url":"https://api.github.com/users/powercode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/powercode/subscriptions","organizations_url":"https://api.github.com/users/powercode/orgs","repos_url":"https://api.github.com/users/powercode/repos","events_url":"https://api.github.com/users/powercode/events{/privacy}","received_events_url":"https://api.github.com/users/powercode/received_events","type":"User","site_admin":false},"labels":[{"id":401720735,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Area-Engine","name":"Area-Engine","color":"5319e7","default":false},{"id":311043048,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Issue-Bug","name":"Issue-Bug","color":"fc2929","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/PowerShell/PowerShell/milestones/16","html_url":"https://github.com/PowerShell/PowerShell/milestones/6.0.0","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/milestones/16/labels","id":1922225,"number":16,"title":"6.0.0","description":"Issues needed for 6.0.0 production quality release","creator":{"login":"SandeepSutari","id":20233168,"avatar_url":"https://avatars.githubusercontent.com/u/20233168?v=3","gravatar_id":"","url":"https://api.github.com/users/SandeepSutari","html_url":"https://github.com/SandeepSutari","followers_url":"https://api.github.com/users/SandeepSutari/followers","following_url":"https://api.github.com/users/SandeepSutari/following{/other_user}","gists_url":"https://api.github.com/users/SandeepSutari/gists{/gist_id}","starred_url":"https://api.github.com/users/SandeepSutari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SandeepSutari/subscriptions","organizations_url":"https://api.github.com/users/SandeepSutari/orgs","repos_url":"https://api.github.com/users/SandeepSutari/repos","events_url":"https://api.github.com/users/SandeepSutari/events{/privacy}","received_events_url":"https://api.github.com/users/SandeepSutari/received_events","type":"User","site_admin":false},"open_issues":224,"closed_issues":35,"state":"open","created_at":"2016-08-03T22:20:16Z","updated_at":"2016-11-28T21:42:53Z","due_on":null,"closed_at":null},"comments":27,"created_at":"2016-10-19T08:50:13Z","updated_at":"2016-11-28T23:01:47Z","closed_at":null,"body":"<!--\n\nIf it is a bug report:\n- make sure you are able to repro it on the latest released version. \nYou can install the latest version from https://github.com/PowerShell/PowerShell/releases\n- Search the existing issues.\n- Refer to the [FAQ](../docs/FAQ.md).\n- Refer to the [known issues](../docs/KNOWNISSUES.md).\n- Fill out the following repro template\n\nIf it's not a bug, please remove the template and elaborate the issue in your own words.\n-->\n## Steps to reproduce\n\n``` powershell\n#Sub.psm1:\n  class Sub {\n    [int] $Value\n  }\n\n#modrepo.psm1:\n  using module .\\Sub.psm1\n\n  function Get-Sub { \n     [Sub]@{\n         Value = 42\n     }\n  }\n\nImport-Module modrepo\nGet-Sub\n```\n\n`Value : 1`\n\nChange the definition of Sub:\n\n``` powershell\n#Sub.psm1:\n  class Sub {  \n    [string] $Name # added field\n    [int] $Value\n  }\n\n#modrepo.psm1:\n  using module .\\Sub.psm1\n\n  function Get-Sub{ \n     [Sub]@{\n         Name = ‘Staffan’ # added field\n         Value = 42\n     }\n  }\n\n#Then\n\nRemove-Module -force modrepo\nImport-Module -force modrepo\nGet-Sub \n```\n## Expected behavior\n\noutput of object of class Sub with \n\n```\nName : 'Staffan'\nValue : 42\n```\n## Actual behavior\n\n```\nCannot create object of type \"Sub\". The Name property was not found for the Sub object. The available property is: [Value <System.Int32>]\nAt C:\\Users\\<user>\\Documents\\WindowsPowerShell\\Modules\\modrepo\\modrepo.psm1:9 char:3\n+   [Sub] @{\n+   ~~~~~~~~\n    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException\n    + FullyQualifiedErrorId : ObjectCreationError\n```\n## Environment data\n\n``` powershell\n\nName                           Value                                                                                                                                                       \n----                           -----                                                                                                                                                       \nPSVersion                      5.1.14393.206                                                                                                                                               \nPSEdition                      Desktop                                                                                                                                                     \nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                     \nBuildVersion                   10.0.14393.206                                                                                                                                              \nCLRVersion                     4.0.30319.42000                                                                                                                                             \nWSManStackVersion              3.0                                                                                                                                                         \nPSRemotingProtocolVersion      2.3                                                                                                                                                         \nSerializationVersion           1.1.0.1                                                                                                                                                     \n\n\n\n```\n"},"comment":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/comments/263422781","html_url":"https://github.com/PowerShell/PowerShell/issues/2505#issuecomment-263422781","issue_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2505","id":263422781,"user":{"login":"alx9r","id":11237922,"avatar_url":"https://avatars.githubusercontent.com/u/11237922?v=3","gravatar_id":"","url":"https://api.github.com/users/alx9r","html_url":"https://github.com/alx9r","followers_url":"https://api.github.com/users/alx9r/followers","following_url":"https://api.github.com/users/alx9r/following{/other_user}","gists_url":"https://api.github.com/users/alx9r/gists{/gist_id}","starred_url":"https://api.github.com/users/alx9r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alx9r/subscriptions","organizations_url":"https://api.github.com/users/alx9r/orgs","repos_url":"https://api.github.com/users/alx9r/repos","events_url":"https://api.github.com/users/alx9r/events{/privacy}","received_events_url":"https://api.github.com/users/alx9r/received_events","type":"User","site_admin":false},"created_at":"2016-11-28T23:01:47Z","updated_at":"2016-11-28T23:01:47Z","body":"@daxian-dbw \r\n\r\n> Thanks for the thorough pester tests!\r\n\r\nYou're welcome. :)\r\n\r\n> I believe `Import-Module -Force Foo` should works the same as `Remove-Module Foo -Force; Import-Module Foo`, and that means it should remove the `Foo` module and its nested modules, and then import it again.\r\n\r\nThat sounds like good behavior to me.\r\n\r\n> I played with your pester tests and found that the root module with a class definition does get reloaded with `Import-Module -Force`, however, the class definition from it doesn't get updated unless the module file has been changed since the last loading.\r\n\r\n>Actually, the class definition doesn't get updated even you run `Remove-Module` and then `Import-Module`:\r\n\r\nInteresting.  I've [expanded the test matrix to cover the different reloading methods](https://gist.github.com/alx9r/7d3c220163bf55daca6fcc544c07ee27).  `Import-Module -Force` definitely behaves different from `Remove-Module -Force; Import-Module`.  \r\n\r\n> It looks like there is some caching for powershell class definition, which I need to investigate further to find out.\r\n\r\nThat explains a good chunk of the problems I've seen related to module reloading.  The results of the automated test `DifferentArgs_Classes_ForceRemoveThenImport` is consistent with caching of the class definition.\r\n\r\nI'll try to summarize the findings:\r\n\r\n1. With current versions of PowerShell use `Remove-Module; Import-Module` instead of `Import-Module -Force`.  `Remove-Module; Import-Module` reloads nested modules (but not class definitions contained in them) whereas `Import-Module -Force` does not.\r\n2. With current versions of PowerShell, the only known way to reload a class definition is to modify its module file.\r\n\r\nThere is another scenario that isn't part of the automated test: Dot-sourcing files in the `.psm1`.  Dot-sourcing files in `.psm1` files is commonplace in bigger modules.  [Pester is one example](https://github.com/pester/Pester/blob/dc550d2b9254bf09b225dafbf34be69f12901527/Pester.psm1#L113-L116).  [ZeroDSC is another](https://github.com/alx9r/ZeroDSC/blob/master/ZeroDSC.psm1#L5-L21).  I wonder whether the cache validation (for both modules and class definitions) is handled correctly when `.ps1` files are dot-sourced in `.psm1` files."}}
{"actor":{"display_login":"alx9r"}}