Using VS Code variables in PSScriptAnalyzer settings path causes settings to not be found
See original GitHub issuePrerequisites
- I have written a descriptive issue title.
- I have searched all issues to ensure it has not already been reported.
- I have read the troubleshooting guide.
- I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
Summary
In VS Code settings.json
if you set your PSScriptAnalyzer settings to include a path that uses VS Code variables like ${workspaceFolder}
the settings will not be found.
This used to work, though I can’t tell how long it’s been since it was broken.
PowerShell Version
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Darwin 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195…
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visual Studio Code Version
1.57.1
507ce72a4466fbb27b715c3722558bb15afa9f48
x64
Extension Version
ms-vscode.powershell@2021.6.1
Steps to Reproduce
Create a .vscode/settings.json
that looks like this:
{
"powershell.scriptAnalysis.enable": true,
"powershell.scriptAnalysis.settingsPath": "${workspaceFolder}/PSScriptAnalyzerSettings.psd1"
}
Add an empty PSScriptAnalyzerSettings.psd1
to the project.
@{
}
Add a PowerShell script that has a script analyzer violation.
Write-Host "Analyzer doesn't like Write-Host."
Note the violation for script analyzer will not appear. You will also see a log message indicating the analyzer settings wasn’t found.
Update the settings.json
to remove the ${workspaceFolder}
reference.
{
"powershell.scriptAnalysis.enable": true,
"powershell.scriptAnalysis.settingsPath": "PSScriptAnalyzerSettings.psd1"
}
Restart VS Code. (There’s another issue already filed about how the settings don’t necessarily take place just by changing them.)
Open your workspace again. You should now see the Write-Host
line in the script as a violation.
Update the PSScriptAnalyzer.psd1
to exclude that rule.
@{
ExcludeRules = @('PSAvoidUsingWriteHost')
}
Restart VS Code and reopen the solution. You should no longer see the violation.
If you want, you can put the ${workspaceFolder}
reference back in settings.json
and restart VS. If you do, you probably won’t see the violation - not because it’s suppressed, but because the default set of rules doesn’t seem to include this; or because failing to find the rule set causes analysis in general to fail. (I don’t know which.)
The only way to keep analysis working is to not use variables from VS Code like ${workspaceFolder}
in the path to the analyzer settings.
Visuals
No response
Logs
2021-06-22 13:05:26.701 -07:00 [INF] Unable to find PSSA settings file at '/Users/tillig/dev/project/${workspaceFolder}/PSScriptAnalyzerSettings.psd1'. Loading default rules.
2021-06-22 13:05:26.703 -07:00 [INF] PSScriptAnalyzer settings file not found. Falling back to default rules
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top GitHub Comments
@tillig my chief concern is that the package hasn’t been updated in 2 years, and I know some of their variables have changed. Upon inspection, it’s simply doing a find-replace, not even taking into account escape characters. Just at a quick glance it’s clear that it’s out-of-date as it’s missing
${userHome}
,${execPath}
, etc. Doing this correctly without upstream support is difficult and error-prone at best. We really need VS Code to support it natively.Looks like someone made a shim for it since the issue has been running so long. Perhaps that could be used?