PowerShell / PowerShell/PSScriptAnalyzer

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

Aberta
#2,116 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Linguagem predominante
C#
Estrelas
2.2k
Forks
415
Merge médio
13h 1min
PRs com merge (30d)
2

Descrição


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.

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece reproduzindo o diagnóstico com os exemplos de operador de formatação e concatenação de strings do issue, usando o PSScriptAnalyzer 2.10.0 e o ambiente do PowerShell informado. Rastreie o ponto de entrada do analisador que relata o erro de referência de variável; considera-se concluído quando ambas as formas válidas não produzirem nenhum diagnóstico, incluindo os casos com dois-pontos mostrados.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
csharp, powershell
Domínio
tooling
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.