PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

IScriptExtent.File Property is null in VS Code

Open tablackburn opened this issue 1 year ago • 0 comments

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 = 

image

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

tablackburn avatar Aug 28 '24 18:08 tablackburn