Vscode-powershell: Local powershell module changes do not persist, seemingly due to cacheing.

Created on 31 Oct 2017  路  9Comments  路  Source: PowerShell/vscode-powershell

System Details

  • Operating system name and version: Windows 10 Pro, 10.0.15063 bild 15063
  • VS Code version: 1.17.2
  • PowerShell extension version: 1.5.0
  • Output from $PSVersionTable: (Below. Seems to be duplicated in your template)
PS C:\...> code -v
1.17.2
b813d12980308015bcd2b3a2f6efa5c810c33ba5
PS C:\Stuff\assembly nupkgs\src\Utilities> $pseditor.EditorServicesVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      5      0      0


PS C:\....> code --list-extensions --show-versions
[email protected]
[email protected]
PS C:\...> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.674
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.674
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

I am a: developer. I want to: Modify a Powershell module in a local psm1 file and test those changes using a Powershell script (ps1) in the same folder, using the "PowerShell Launch Current File" default configuration.

What I expect to happen: When I make a change in the psm1 file, these changes should persist when I run the ps1 script referring to that module.

What actually happens: Running the script uses an old version of the module. Restarting Visual Studio Code seemingly refreshes the cache, resulting in the changes now being present in the script's behaviour.

Steps to Recreate

  • Create a psm1 module such as test.psm1.
  • Create a function such as function test() {...}
  • Have it do something very simple and easily recognisable, perhaps Write-Host "hello world".
  • Export the function, i.e. Export-ModuleMember test
  • Create a ps1 file such as test.ps1 in the same folder that imports the module, i.e. Import-Module .\test.psm1.
  • Call the function from the module, i.e. test()
  • Run the file using the "PowerShell Launch Current File" default configuration. It should behave as expected.
  • Change the behaviour of the module, perhaps by changing the text it writes out.
  • Run the script again, with the same configuration. It should exhibit the behaviour of the prior version of the powershell module, without the changes.
  • Restart Powershell and run the script again. Now it should exhibit the expected behaviour.

Most helpful comment

PowerShell does not reload a module by default, it uses a cache. You get the new module when you restart PowerShell because you dump the old PowerShell process. There's not much the extension can do for you here -- you need to use Import-Module -Force.

All 9 comments

I've found one workaround is to place Remove-Module *; at the start of the script. This removes the cache.

But it seems to me the problem stems from re-using the same Powershell process. As a developer when I start a debug instance I expect it to be a new instance, not containing state from the previous instance. Perhaps a solution would be to add a launch.json option to start a new PS terminal with each launch, and include that as the default setting for the launch current PS file config?

I understand that this feature was added to better emulate folks experience with the ISE editor. It is not my preference though. Fortunately there is a setting you can enable to force a fresh session:

  // Determines whether a temporary PowerShell Integrated Console is created for each debugging session, useful for debugging PowerShell classes and binary modules.
  "powershell.debugging.createTemporaryIntegratedConsole": true

Please try this setting and let me know if it meets your needs enough to close this issue.

Thanks for the reply. This seems like a better solution, but the new console immediately closes after run. As such you don't get a chance to check the output. Is there some way to set it to only close the terminal when you start a new run?

Yup, I noticed the same thing. I'm closing this as a duplicate of #907. Feel free to add feedback/suggestions on that issue.

Any news on this?

This is pretty annoying. I am currently debugging my functions to a ps1 file before moving them to the psm1 file. When I am not doing that then I am trashing the Powershell Session with the bucket icon. It then asks me if I want to restart a session to which I reply "Yes". But that's 2 more actions than I would like.

For my PowerShell modules, I use the following in my testing .ps1's:

Import-Module"$PSScriptRoot\MyModuleName.psd1"-Force #I always have psd1's for each of my psm1's

Have you tried adding -Force to the import? This may be a PowerShell thing, not an extension one. Related: https://github.com/PowerShell/PowerShell/issues/2505.

PowerShell does not reload a module by default, it uses a cache. You get the new module when you restart PowerShell because you dump the old PowerShell process. There's not much the extension can do for you here -- you need to use Import-Module -Force.

Noted and implemented! Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings