Skip to content
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

Allow VSCode configuration of PSScriptAnalyzer rules #1443

Open
rjmholt opened this issue Jul 20, 2018 · 12 comments
Open

Allow VSCode configuration of PSScriptAnalyzer rules #1443

rjmholt opened this issue Jul 20, 2018 · 12 comments
Labels
Area-Configuration Area-Script Analysis Issue-Discussion Let's talk about it. Issue-Enhancement A feature request (enhancement). Up for Grabs Will shepherd PRs.

Comments

@rjmholt
Copy link
Contributor

rjmholt commented Jul 20, 2018

To follow on from the comments starting at #1429 (comment)...

PSSA rules should be enable-able and disable-able in VSCode configuration. The current proposal is:

  • Disabled rules turn off default rules, but not a local PSSA settings file
  • Enabled rules add to the default rules

Current proposed format is:

{
  "powershell.scriptAnalysis.rules": {
    "disable": [
      "PSAvoidTrailingWhitespace"
    ],
    "enable": [
      "RuleThatILikeButMyColleaguesDont",
    ]
  }
}
@rjmholt rjmholt added Issue-Enhancement A feature request (enhancement). Issue-Discussion Let's talk about it. Area-IntelliSense labels Jul 20, 2018
@Halkcyon
Copy link

How would you plan on addressing rules that are configurable such as PSProvideCommentHelp or PSAvoidUsingCmdletAliases?

@rjmholt
Copy link
Contributor Author

rjmholt commented Sep 10, 2018

@TheIncorrigible1 Good question. I don't have anything particular in mind. The fact that we are even proposing to make a settings-embedded JSON configuration for a psd1-file-configured tool already feels a bit odd to me, but it seems to be what some users are asking for.

The first two solutions I can imagine suggested are:

  1. You can't configure them; if you want that kind of configuration, it's time to write a psd1 file.
  2. We just translate the whole psd1 schema into JSON

@fourpastmidnight
Copy link

I don't see why we can't have our cake and eat it, too! There are probably a few ways to go about this in JSON syntax, but, perhaps the easiest is to modify the proposed enable property above:

{
  "powershell.scriptAnalysis.rules": {
    "disable": [
      "PSAvoidTrailingWhitespace"
    ],
    "enable": [
      {
        "rule": "PSPlaceOpenBrace",
        "configuration": {
          "Enable": true,
          "OnSameLine": true,
          "NewLineAfter": true
        }
      },
      { "rule": "PSUseSingulareNouns" },
    ]
  }
}

So, one rule can have a configuration, while the second rule, which is not configurable, does not--so the user would not provide a configuration. But even if the user were to provide a configuration for a non-configurable rule, and even if the configuration is added to a generated PSScriptAnalyserSettings.psd1 file, wouldn't it simply be ignored since PSScriptAnalyzer would never attempt to configure it in the first place since the rule is not configurable?

@moymike
Copy link

moymike commented Jan 23, 2019

It would be nice if PSScriptAnalyzer Rules were stored in the User Settings / Extensions area (JSON).

I ran into this as I wanted to customize which rules apply, and I chased this link down:
https://superuser.com/questions/1393186/how-to-disable-psscriptanalyzers-psavoidusingcmdletaliases-in-powershell-extens/1397066?noredirect=1#comment2107064_1397066

I set the: "powershell.scriptAnalysis.settingsPath": "", to toggle via a script (which is example #3 in the 'superuser' page.

I currently use an extension 'Settings Sync'. If the PSScriptAnalyzer Rules were stored in the User Settings, it would simplify dealing with these toggles for me, as they would sync along with all my other settings.

@mklement0
Copy link
Contributor

The aspect of selective disabling / enabling of rules is essentially a duplicate of #823, which asks that the existing GUI capability of selecting rules (>PowerShell: Select PSScriptAnalyzer Rules) be persisted across sessions.

Adding rule configuration would be nice too (for which a GUI method will probably never be implemented due to complexity, if I were to guess).

@fourpastmidnight: I like the idea, but for rule-name-only entries you should be able to use just the name as a string value; e.g., "PSUseSingularNouns" rather than { "rule": "PSUseSingularNouns" }.

@PrzemyslawKlys
Copy link
Contributor

It seems >PowerShell: Select PSScriptAnalyzer Rules was removed at some point. Could we get this ability to configure PSScriptAnalyzer thru user settings/workspace settings?

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Aug 12, 2020
@rkeithhill
Copy link
Contributor

PSSA has it's own settings file format. You can use it and configure user/workspace setting to point to that file:

    "powershell.scriptAnalysis.settingsPath": "./PSScriptAnalyzerSettings.psd1",

The nice thing with this approach is that you can use this PSSA settings file with PSSA outside of VSCode - as part of a command line build, interactive invocation of PSSA, etc. If you open the VSCode examples, there is a sample PSScriptAnalyzerSettings.psd1 file you can start with.

@SydneyhSmith SydneyhSmith removed the Needs: Maintainer Attention Maintainer attention needed! label Aug 13, 2020
@hongwen000
Copy link

I wonder if there is a way to set global configuration instead of making a copy of psd1 file in every project?

@ALIENQuake
Copy link

@hongwen000 My workaround is:

  • have PSScriptAnalyzerSettings.psd1 file inside your GitHub repo
  • clone the repo locally into your user profile/Documents directory
  • create C:\Users\Public\PSScriptAnalyzerSettings.psd1 as a Symbolic Link into the repo
  • set powershell.scriptAnalysis.settingsPath to C:\Users\Public\PSScriptAnalyzerSettings.psd1

Helper function:

# Check admin rights
function Test-Elevation {
    $windowsPrincipal = [System.Security.Principal.WindowsPrincipal]::new( [System.Security.Principal.WindowsIdentity]::GetCurrent() )
    $windowsPrincipal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator )
}

# VSCode PSScriptAnalyzerSettings
if (Test-Elevation) {
    if ( -NOT (Test-Path "C:\Users\Public\PSScriptAnalyzerSettings.psd1")) {
        New-Item -ItemType SymbolicLink -Path "C:\Users\Public\PSScriptAnalyzerSettings.psd1" -Value "$PSProfileDirectory\PSScriptAnalyzerSettings.psd1" | Out-Null
    }
}

Hope this help.

@hongwen000
Copy link

hongwen000 commented Jul 13, 2022

@ALIENQuake Wow, thanks so much for your reply! It looks great, but I am a little confused about some details:

  1. Should I create a symbolic link file PSScriptAnalyzerSettings.psd1 targeting C:\Users\Public\PSScriptAnalyzerSettings.psd1 in the root directory of every repo that use PSScriptAnalyzer?
  2. Or just set powershell.scriptAnalysis.settingsPath as C:\Users\Public\PSScriptAnalyzerSettings.psd1 in the VsCode User-wise configuration?
  3. Or PSScriptAnalyzerSettings.psd1 under $PSProfileDirectory is a global configuration, so I just need create one under $PSProfileDirectory?

@ALIENQuake
Copy link

@hongwen000 Nr 2 😃

@falkheiland
Copy link

i suggest @hongwen000 create a .vscode folder with a settings.json in the root of the repo containing:

{
  "powershell.scriptAnalysis.settingsPath": "PSScriptAnalyzerSettings.psd1"
}

an have you repo specific PSScriptAnalyzerSettings.psd1 in the root of the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Configuration Area-Script Analysis Issue-Discussion Let's talk about it. Issue-Enhancement A feature request (enhancement). Up for Grabs Will shepherd PRs.
Projects
None yet
Development

No branches or pull requests