PowerShell / PowerShell/PSScriptAnalyzer

False Positive: Variable Reference Error for Colon After Variable in Double-Quoted Strings (Format Operator and Concatenation)

Offen
#2,116 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
C#
Sterne
2.2k
Forks
414
Ø Merge
13 Std. 1 Min.
Gemergte PRs (30 T.)
2

Beschreibung


Before submitting a bug report:

  • I have reproduced this on the latest released version of PSScriptAnalyzer and VS Code PowerShell extension.
  • I have searched for existing issues and did not find this bug reported.

Steps to reproduce

$errorMsg = 'Some error details'
$feature = 'EnablePlacesWebApp'

# Using format operator
Write-Log -Message ('Failed to enable {0}: {1}' -f $feature, $errorMsg) -Level 'ERROR'
Write-Host ("`n❌ Failed to enable {0}: {1}" -f $feature, $errorMsg) -ForegroundColor Red

# Using string concatenation
Write-Log -Message ('Failed to enable ' + $feature + ': ' + $errorMsg) -Level 'ERROR'
Write-Host ("`n❌ Failed to enable " + $feature + ": " + $errorMsg) -ForegroundColor Red

Expected behavior

No error or warning should be reported by PSScriptAnalyzer for valid PowerShell syntax using the format operator or string concatenation, regardless of the character following the variable.

Actual behavior

PSScriptAnalyzer reports:
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.

This occurs even when using the format operator (-f) or string concatenation, which are valid and idiomatic PowerShell.

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

No unexpected error thrown; this is a linter false positive.


Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
...

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

2.10.0

Additional context:
This issue also appears in the VS Code PowerShell extension, which integrates PSScriptAnalyzer for diagnostics. Please coordinate with the extension maintainers if needed.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Diagnose mit den Beispielen für den Formatoperator und die Zeichenkettenverkettung im Issue zu reproduzieren, unter Verwendung von PSScriptAnalyzer 2.10.0 und der angegebenen PowerShell-Umgebung. Verfolge den Einstiegspunkt des Analyzers, der den Variablenreferenzfehler meldet; abgeschlossen ist die Arbeit, wenn beide gültigen Formen keine Diagnose erzeugen, einschließlich der gezeigten Fälle mit Doppelpunkten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp, powershell
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.