PowerShell / PowerShell/PSScriptAnalyzer

Unable to run rules with hashtable that run fine with settings file

Ouverte
#2,047 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
C#
Étoiles
2.2k
Forks
414
Merge moyen
13 h 1 min
PR mergées (30 j)
2

Description

Steps to reproduce

This is essentially the opposite of #1160

The attached zip file concisely contains the repro:

PSScriptAnalyzerSettingsHashtableRepro.zip

But here it is in plain text too

# repro.ps1

Write-Output "Using settings file"

Invoke-ScriptAnalyzer -Path .\repro.ps1 -Settings '.\PSScriptAnalyzerSettings.psd1'

# Create an unused variable which triggers a warning that the settings file should suppress
$unusedVariable = 1

$settings = Import-PowerShellDataFile -Path '.\PSScriptAnalyzerSettings.psd1'

Write-Output "Loaded settings to hashtable:"

$settings | ConvertTo-Json | Write-Output

Write-Output "Using setting hashtable"

Invoke-ScriptAnalyzer -Path .\repro.ps1 -Settings $settings
# PSScriptAnalyzerSettings.psd1

@{
    ExcludeRules = @(
        'PSUseDeclaredVarsMoreThanAssignments'
    )
}

Expected behavior

The warning should be suppressed in both calls to Invoke-ScriptAnalyzer. Output from the repro script should be this:

Using settings file
Loaded settings to hashtable:
{
    "ExcludeRules":  [
                         "PSUseDeclaredVarsMoreThanAssignments"
                     ]
}
Using setting hashtable

Actual behavior

The warning is only suppressed in the first call (the one that references the file directly). The call that passes the settings hashtable does not suppress the error. Output from the repro script is this:

Using settings file
Loaded settings to hashtable:
{
  "ExcludeRules": [
    "PSUseDeclaredVarsMoreThanAssignments"
  ]
}
Using setting hashtable

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      repro.ps1  6     The variable 'unusedVariable' is assigned but never
s                                                                 used.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.23.0

Note that this issue does NOT happen in this environment (i.e. the warning is suppressed in both calls):

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.26100.2161
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.26100.2161
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).
Version | ForEach-Object { $_.ToString() }
1.23.0

It appears to be related to the Powershell version.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par exécuter repro.ps1 avec PSScriptAnalyzerSettings.psd1 dans les environnements PowerShell 7.4.6 et 5.1 signalés, puis retracez la manière dont Invoke-ScriptAnalyzer traite le chemin du fichier par rapport à la table de hachage de paramètres importée. C’est terminé lorsque l’entrée ExcludeRules supprime PSUseDeclaredVarsMoreThanAssignments dans les deux appels, avec une couverture de régression pour le comportement différent de PowerShell.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
powershell
Domaine
tooling
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.