Skip to content

Local powershell module changes do not persist, seemingly due to cacheing. #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Fortnox opened this issue Oct 31, 2017 · 9 comments
Closed

Comments

@Fortnox
Copy link

Fortnox commented Oct 31, 2017

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.
@Fortnox
Copy link
Author

Fortnox commented Oct 31, 2017

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?

@rkeithhill
Copy link
Contributor

rkeithhill commented Nov 1, 2017

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.

@Fortnox
Copy link
Author

Fortnox commented Nov 1, 2017

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?

@rkeithhill
Copy link
Contributor

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

@wouldBeNerd
Copy link

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.

@mmascolino
Copy link

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

@rjmholt
Copy link
Contributor

rjmholt commented Sep 27, 2018

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

@rjmholt
Copy link
Contributor

rjmholt commented Sep 27, 2018

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.

@wouldBeNerd
Copy link

Noted and implemented! Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants