Skip to content

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

@twcau

Description

@twcau

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

Problem

A false positive diagnostic is reported by the VS Code PowerShell extension (and PSScriptAnalyzer) when a variable is followed by a colon in a double-quoted string, even when using the format operator (-f) or string concatenation. This is valid PowerShell syntax, but the extension reports:

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

Expected Behavior

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

Actual Behavior

The extension reports the above error in the Problems panel, even though the code is valid and runs without issue in standalone PowerShell.

Troubleshooting Steps Taken

  • Verified extension and VS Code are up to date.
  • Confirmed issue does not reproduce in standalone PowerShell (outside VS Code).
  • Searched for similar issues in the repo (none found).
  • Read the troubleshooting guide.

Additional Context

This issue also appears in the PSScriptAnalyzer linter, which is integrated into the VS Code PowerShell extension for diagnostics. Please coordinate with the PSScriptAnalyzer maintainers if needed.

Additional Information

  • Workspace type: PowerShell 7+ modular project

PowerShell Version

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

Visual Studio Code Version

code --version
1.89.1

Extension Version

code --list-extensions --show-versions | Select-String powershell
ms-vscode.powershell@2024.6.0

Steps to Reproduce

  1. Open a PowerShell script in VS Code with the following code:
$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
  1. Save the file and observe the diagnostics reported by the PowerShell extension.

Error Details

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

Visuals

No response

Logs

[PowerShell][Diagnostic] Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.
File: Configuration.psm1
Line: <affected line number>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Script AnalysisIssue-BugA bug to squash.Needs: Author FeedbackPlease give us the requested feedback!Pending: ExternalThis issue is not fixed here, it is fixed in a dependent repo such as PowerShell,PSScriptAnalyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions