PowerShell / PowerShell/PSScriptAnalyzer

Settings param cannot be set to $_ inside of a foreach-object loop

Open
#807 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Engine in progress Issue - Bug
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

When the value of the -Settings parameter is set to $_ inside of a foreach-object loop, PSScriptAnalyzer is unable to find the settings file.

Steps to reproduce:
Create a file that violates a rule that is only checked if a particular setting is triggered.
IE:

if ($true)
{
    Write-Output 'Something'
}

This code will throw errors if -Settings is set to CodeFormattingStroustrup but not if -Settings it is omitted.

The following command returns warnings.
invoke-scriptanalyzer -path C:\files\ax.ps1 -Settings 'CodeFormattingStroustrup'
But this one does not.
'CodeFormattingStroustrup' | % { invoke-scriptanalyzer -path C:\files\ax.ps1 -Settings $_ }

Using -Verbose on the non-working example shows that PSScriptAnalyzer cannot find a settings file.

This occurs on version 1.15.0 of PSScriptAnalyzer.

Screenshot
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Invoke-ScriptAnalyzer entry point and reproduce the difference between a direct -Settings value and the foreach-object form using -Verbose. Trace how the settings value is resolved in both invocations; done means the piped $_ form locates CodeFormattingStroustrup and reports the same warnings as the direct command.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.