Psscriptanalyzer: Remove-Module PSScriptAnalyzer leaves behavior of removed module version

Created on 2 Mar 2017  路  7Comments  路  Source: PowerShell/PSScriptAnalyzer

Removing a version of PSScriptAnalyzer does not remove its behavior from the session. I don't know much about this, but it looks like we're missing code to clean up after a remove.

Version 1.10.0 and 1.11.0 are somewhat different. For example, in 1.11.0, the Settings parameter tab-completes the .psd1 file names in the Settings subdirectory. Also, the PSPlaceOpenBrace and PSPlaceCloseBrace rules do not trigger on a one-line function, such as:

function Test-Brace1 () {"Hello, World"}

However, if you import one version (Import-Module), remove that version (Remove-Module), and then import the other version, the result is the same as if you imported only the first version.

This is important, because the results of the different module versions are perceptibly different.

REPRO:

0. Start a new session, e.g. new console. 
1. ipmo PSScriptAnalyzer -RequiredVersion 1.10.0 -Passthru
2. gmo | rmo
3. ipmo PSScriptAnalyzer -RequiredVersion 1.11.0 -Passthru 

Now, test one of the differing features.

4. Invoke-ScriptAnalyzer -Settings <TAB>    #Fails to tab complete, as in 1.10.0.
5. Invoke-ScriptAnalyzer -ScriptDefinition 'function Test-Brace1 () {"Hello, World"}' -Settings $home\Documents\WindowsPowerShell\Modules\PSScriptAnalyzer\1.11.0\Settings\CodeFormatting.psd1

This returns output that you shouldn't see any more in 1.11.0.

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSPlaceOpenBrace                    Warning                 1     There is no new line after open brace.
PSPlaceCloseBrace                   Warning                 1     Close brace is not on a new line.

The same is true if you reverse the order of the versions.

0. Start a new session, e.g. new console. 
1. ipmo PSScriptAnalyzer -RequiredVersion 1.11.0 -Passthru
2. gmo | rmo
3. ipmo PSScriptAnalyzer -RequiredVersion 1.10.0 -Passthru 

Now, we have 1.10.0 with 1.11.0 behavior:

4. Invoke-ScriptAnalyzer -Settings <TAB>    #Tab completes!
5. Invoke-ScriptAnalyzer -ScriptDefinition 'function Test-Brace1 () {"Hello, World"}' -Settings CodeFormatting

Version 1.10.0 should error on the one-liner, but it doesn't.

PS C:\> Invoke-ScriptAnalyzer -Settings CodeFormatting -ScriptDefinition 'function Test-Brace1 () {"Hello, World"}'
PS C:\>

Strange, I know. If you need more info, let me know. Thanks!

Issue - Discussion

Most helpful comment

All 7 comments

This is a general problem with PowerShell modules that load .NET assemblies. Once you load a .NET assembly into PowerShell, you can't unload or reload it without restarting the process entirely. The reason why you see the old PSSA behavior is that the new version of the PSScriptAnalyzer .NET assemblies cannot be loaded to replace the old version in the same session.

Thanks for explaining! I'll close this issue.

This needs a warning at import time, as a minimum, I think. Especially since those of us with production code and CI may want to use more than one version at various times (i.e. to test whether we can upgrade the build system)

@Jaykul just recommended to me that we should check if the PSScriptAnalyzer assembly is already loaded when the PSScriptAnalyzer module gets imported and then show a warning to the user that they should restart their session to load the latest version of the module. I think this would be a helpful way to minimize this type of confusion in the future.

Jinx ;)

I think this issue is more general, in the sense that import-module cmdlet should provide such a warning. I think PSSA should definitely provide such a warning as a temporary fix till 'import-module' provides such a capability.

/cc @joeyaiello

Was this page helpful?
0 / 5 - 0 ratings