PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.87k stars 379 forks source link

Support for null coalescing operator #1943

Closed stephenatwork closed 1 year ago

stephenatwork commented 1 year ago

Steps to reproduce

#Requires -Version 7.0
$a = $b ?? $c

Expected behavior

PSScriptAnalyzer warns about a missing #Requires when using new features. If a correct #Requires is present, then no error is issued.

Actual behavior

Unexpectedtoken.unexpectedtoken

Environment data

This is from an internal TSA MS run with id 22782574

andyleejordan commented 1 year ago

We cannot repro Unexpectedtoken.unexpectedtoken. In Windows PowerShell 5.1.22621.1778 I get:

PS C:\Users\andschwa> Invoke-ScriptAnalyzer ./test.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
UnexpectedToken                     ParseError   test.ps1   1     Unexpected token '??' in expression or
                                                                  statement.
PSUseDeclaredVarsMoreThanAssignment Warning      test.ps1   1     The variable 'a' is assigned but never used.
s

Which is what we expected since ?? is a parse error for Windows PowerShell. In PowerShell 7.4.0-preview.6 I'm getting no warning about the missing requires but also no Unexpectedtoken.unexpectedtoken:

> Invoke-ScriptAnalyzer .\test.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      test.ps1   1     The variable 'a' is assigned
s

So what version of PowerShell are you seeing this?

stephenatwork commented 1 year ago

Thanks Andy! For powershell 5, shouldn't it fail because it doesn't meet the #requires version in the script?

The failure is from a CI pipeline so I don't have visibility on the version. I posted the id of the TSA run above, perhaps that can tell if the machine is not correctly configured?

kilasuit commented 1 year ago

PSScriptAnalyzer doesn't run the code, (would be very bad if it did) so the requires statement is not being invoked by the engine, so unless you actually run the code then you'd never have it fail.

If the failure is from a CI pipeline perhaps the task in the pipeline needs updating from PowerShell to pwsh depending on the CI pipeline tool that you are using.

andyleejordan commented 1 year ago

@stephenatwork, @kilasuit is correct. The #requires isn't going to be executed by PSSA, and since ?? is a parsing error for Windows PowerShell it is throwing the correct error message. I would follow their advice and update the CI pipeline appropriately.

stephenatwork commented 1 year ago

I'm going to close this bug since a) it's a config issue & b) I rewrote the code not to use ??. FWIW I still think the analyzer should check for #requires & warn if the current version is not compatible.

And for any microsoft employees here, I note that the this is running in CI via a guardian task "PSScriptAnalyzer@1". It's not clear from a scan of the docs how I can select the correct PS version there. Worthwhile to follow up with the guardian team?

andyleejordan commented 1 year ago

I searched but could not figure out what a "Guardian" task is but the naming scheme makes it look like an ADO task. Send me a link on Teams to the docs you read?

I agree, the ability to parse #requires when running PSSA would be a great improvement, though I'm not sure if it's possible with the existing implementation. That sounds like something @JamesWTruher would know.