Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke-ScriptAnalyzer -EnableExit sets exit code based on the last evaluated file with piped input #2054

Closed
MatejKafka opened this issue Feb 4, 2025 · 1 comment · Fixed by #2055

Comments

@MatejKafka
Copy link
Contributor

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce

$null = mkdir analyzer
cd .\analyzer\
$PSNativeCommandUseErrorActionPreference = $false

'function fn() {ls}' >1.psm1
'function fn() {}' >2.psm1

# exits with code 0, because the last evaluated file (2.psm1) does not have any violations
pwsh -noprofile {gi .\1.psm1, .\2.psm1 | Invoke-ScriptAnalyzer -EnableExit}
$LastExitCode

# exits with code 1
pwsh -noprofile {gi .\2.psm1, .\1.psm1 | Invoke-ScriptAnalyzer -EnableExit}
$LastExitCode

Expected behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingCmdletAliases           Warning      1.psm1     1     'ls' is an alias of 'Get-ChildItem'. Alias can introduce
                                                                  possible problems and make scripts hard to maintain. Please
                                                                  consider changing alias to its full content.

1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingCmdletAliases           Warning      1.psm1     1     'ls' is an alias of 'Get-ChildItem'. Alias can introduce
                                                                  possible problems and make scripts hard to maintain. Please
                                                                  consider changing alias to its full content.

1

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingCmdletAliases           Warning      1.psm1     1     'ls' is an alias of 'Get-ChildItem'. Alias can introduce
                                                                  possible problems and make scripts hard to maintain. Please
                                                                  consider changing alias to its full content.

0

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingCmdletAliases           Warning      1.psm1     1     'ls' is an alias of 'Get-ChildItem'. Alias can introduce
                                                                  possible problems and make scripts hard to maintain. Please
                                                                  consider changing alias to its full content.

1

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.6.0-preview.2
PSEdition                      Core
GitCommitId                    7.6.0-preview.2
OS                             Microsoft Windows 10.0.19045
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.23.0
@MatejKafka
Copy link
Contributor Author

MatejKafka commented Feb 4, 2025

Imo, Invoke-ScriptAnalyzer should set exit code based on the total number of violations, not just based on the last file – if there are 2 file paths piped in and each file has 2 violations, the exit should be set to 4. If you agree, I can make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant