question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using VS Code variables in PSScriptAnalyzer settings path causes settings to not be found

See original GitHub issue

Prerequisites

  • 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
andschwacommented, Dec 7, 2022

@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.

1reaction
tilligcommented, Dec 7, 2022

Looks like someone made a shim for it since the issue has been running so long. Perhaps that could be used?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using VS Code variables in PSScriptAnalyzer settings path causes ...
Using VS Code variables in PSScriptAnalyzer settings path causes settings to not be found.
Read more >
Using PSScriptAnalyzer - PowerShell - Microsoft Learn
This article describes various features of PSScriptAnalyzer and how to use them.
Read more >
PSScriptAnalyzer (PSSA) VS-code integration & customization ...
PSScriptAnalyzer (PSSA) VS - code integration & customization Compatibility Analysis by Christoph BergmeisterThe default setup of PSSA does ...
Read more >
PowerShell editing with Visual Studio Code
To disable PSScriptAnalyzer, open your settings (Ctrl+,), browse Extensions, select the PowerShell extension, and deselect the checkbox for ...
Read more >
Applying custom rules in the vscode powershell extension ...
After changing the settings path, I don't get any highlighting while ... it possible to use PSSA to set up custom code formatting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found