vscode-powershell
vscode-powershell copied to clipboard
IScriptExtent.File Property is null in VS Code
When attempting to write a custom PSScriptAnalyzer rule, the IScriptExtent.File property is null in the Problems pane of VS Code, but is not null not outside of the VS Code Problems pane.
Steps to reproduce
git clone https://github.com/bergmeister/PSScriptAnalyzer-VSCodeIntegration.git
(Get-Content .\PSScriptAnalyzer-VSCodeIntegration\CustomPSScriptAnalyzerRules.psm1) -replace 'Message\s+=.+$', 'Message = "`$ast.Extent.File = $($ast.Extent.File)"' | Set-Content .\PSScriptAnalyzer-VSCodeIntegration\CustomPSScriptAnalyzerRules.psm1
Set-Location PSScriptAnalyzer-VSCodeIntegration
Invoke-ScriptAnalyzer -Path .\scriptWithViolations.ps1 -CustomRulePath .\CustomPSScriptAnalyzerRules.psm1 -IncludeRule Warning*
code .
In VS Code, open scriptWithViolations.ps1, then open the Problems pane (Ctrl + Shift + M).
Expected behavior
$ast.Extent.File is populated with the absolute path of the script file scriptWithViolations.ps1, the same as the output from Invoke-ScriptAnalyzer
Actual behavior
Invoke-ScriptAnalyzer returns:
PS C:\Users\<redacted>\PSScriptAnalyzer-VSCodeIntegration> Invoke-ScriptAnalyzer -Path .\scriptWithViolations.ps1 -CustomRulePath .\CustomPSScriptAnalyzerRules.psm1 -IncludeRule WarningAboutDoSomething
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
CustomPSScriptAnalyzerRules\Warning Warning scriptWith 1 $ast.Extent.File = C:\Users\<redacted>\PSScriptAnaly
AboutDoSomething Violations zer-VSCodeIntegration\scriptWithViolations.ps1
.ps1
CustomPSScriptAnalyzerRules\Warning Warning scriptWith 1 $ast.Extent.File = C:\Users\<redacted>\PSScriptAnaly
AboutDoSomething Violations zer-VSCodeIntegration\scriptWithViolations.ps1
.ps1
while VS Code's Problems pane shows $ast.Extent.File as $null
$ast.Extent.File =
If an unexpected error was thrown, then please report the full error details using e.g. $error[0] | Select-Object *
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.4.5
PSEdition Core
GitCommitId 7.4.5
OS Microsoft Windows 10.0.22631
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.22.0
1.22.0
Thanks to @JustinGrote and @HeyItsGilbert for troubleshooting this issue. The working theory is that the VS Code PowerShell extension passes the content of files to PSScriptAnalyzer so there's no file path available, which is why it's empty. See https://github.com/PowerShell/PowerShellEditorServices/blob/1a988a964d9803a4b82d2f0f56b5c8a225b11156/src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs#L200-L203 and https://discord.com/channels/180528040881815552/447579065877266454/1352017799731482645 for more details and context.