Vscode-powershell: PSAvoidUsingDoubleQuotesForConstantString doesn't work

Created on 24 Feb 2021  路  17Comments  路  Source: PowerShell/vscode-powershell

System Details


System Details Output

### VSCode version: 1.53.2 622cb03f7e070a9670c94bae1a45d78d7181fbd4 x64

### VSCode extensions:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


### PSES version: 2.2.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Darwin 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Description

I am experiencing a problem with PSAvoidUsingDoubleQuotesForConstantString not working.

Expected Behaviour

It should print

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingDoubleQuotesForConstant Information  Test.ps1   1     Use single quotes when a string is constant.
String

Actual Behaviour

No problem is reported.

Also, running:

Invoke-ScriptAnalyzer -Path Test.ps1 -Settings PSScriptAnalyzerSettings.psd1

or

Invoke-ScriptAnalyzer -Path Test.ps1 -Settings PSScriptAnalyzerSettings.psd1 -Severity Information

doesn't print anything.

Attached Logs

Here are the files: 1614203961.zip.

Resolution-Answered

Most helpful comment

But I completely restarted Visual Studio Code before. Shouldn't that have been enough?

After you installed the new module in iTerm, a reload of the Integrated Console will pick that up. That's the difference.

Also, running gmo PSScriptAnalyzer doesn't print anything now. Do you know why?

The module won't be loaded until you import it or autoload it by running a command from it. Running Invoke-ScriptAnalyzer autoloaded it, so then gmo succeeded.

All 17 comments

Is this a problem you're only seeing in the Integrated Console in VSCode, or are you also seeing it in an ordinary pwsh console?

Both.

Wait, I thought you asked something else. Running Invoke-ScriptAnalyzer after pwsh prints:

Invoke-ScriptAnalyzer: The term 'Invoke-ScriptAnalyzer' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I had to install it. Running in iTerm works:

PS /Users/revolt/1614203961> Invoke-ScriptAnalyzer -Path Test.ps1 -Settings PSScriptAnalyzerSettings.psd1 -Severity Information

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingDoubleQuotesForConstant Information  Test.ps1   1     Use single quotes when a string is constant.
String

What are the contents of Test.ps1?

And what's in your PSScriptAnalyzerSettings.psd1?

It's in the zip file from the issue description, but here is it again:

$asd = "asd"

Write-Output $asd

That file is in there as well, but here is it again:

@{
    Severity = @('Error', 'Warning', 'Information')
    Rules = @{
        PSAvoidUsingDoubleQuotesForConstantString = @{
            Enable = $true
        }
    }
}

In both the Integrated Console and in iTerm, what are the results of:

  • $PSVersionTable
  • gmo PSScriptAnalyzer

FWIW I'm also seeing this behaviour in the ordinary non-integrated console:

image

Integrated Console:

PS /Users/revolt/1614203961> $PSVersionTable


Name                           Value
----                           -----
PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Darwin 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS /Users/revolt/1614203961> gmo PSScriptAnalyzer


ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.19.0                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}

iTerm:

PS /Users/revolt/1614203961> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Darwin 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS /Users/revolt/1614203961> gmo PSScriptAnalyzer

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.19.1                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}

It looks like Visual Studio Code is using 1.19.0.

Upgrading from PSScriptAnalyzer 1.19.0 to 1.19.1 fixed this for me. I suspect when you installed the module to use in iTerm you got the newest version.

Now that you have that installed on your machine, try restarting your integrated console and running the command again. The extension should pick up the latest version from the module path.

This newer version of PSScriptAnalyzer will also be shipped by default in the next release of the extension too (it shipped in the new preview yesterday)

try restarting your integrated console

But I completely restarted Visual Studio Code before. Shouldn't that have been enough?

Also, running gmo PSScriptAnalyzer doesn't print anything now. Do you know why?

I ran the Invoke-ScriptAnalyzer and then gmo PSScriptAnalyzer again and now it does print

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.19.1                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}

And yes, the problem is now reported, thanks for the help!

But I completely restarted Visual Studio Code before. Shouldn't that have been enough?

After you installed the new module in iTerm, a reload of the Integrated Console will pick that up. That's the difference.

Also, running gmo PSScriptAnalyzer doesn't print anything now. Do you know why?

The module won't be loaded until you import it or autoload it by running a command from it. Running Invoke-ScriptAnalyzer autoloaded it, so then gmo succeeded.

Was this page helpful?
0 / 5 - 0 ratings