question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`PSUseConsistentIndentation.PipelineIndentation.None` or `3` deletes commandlet name

See original GitHub issue

When PSUseConsistentIndentation.PipelineIndentation is set to None or 3 it deletes where-object and foreach-object

Steps to reproduce

Sample input:

Get-ChildItem |
Where-Object Name -Like 'foo'

Get-ChildItem |
ForEach-Object Name

It happens using the format

foo |
bar

These formats work correctly

foo | bar

foo
| bar

Test cases

$script = @'
Get-ChildItem |
Where-Object Name -Like 'foo'

Get-ChildItem |
ForEach-Object Name
'@

$settings = @{
    IncludeRules = @('PSUseConsistentIndentation')
    Rules        = @{
        PSUseConsistentIndentation = @{
            Enable              = $true
            # Kind                = 'space'
            PipelineIndentation = 'None' # broke
            # IndentationSize     = 4
        }

    }
}
"

To reproduce: PipelineIndentation = None/3`n"
Invoke-Formatter -ScriptDefinition $script -Settings $settings

foreach ($mode in ('IncreaseIndentationAfterEveryPipeline', 'IncreaseIndentationForFirstPipeline', 'IncreaseIndentationAfterEveryPipeline', 'NoIndentation', 'None', ''))
{
    "`n`nMode: '$Mode'`n"
    $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $Mode
    Invoke-Formatter -ScriptDefinition $script -Settings $settings
}

Expected behavior

Results that contain Foreach-Object and Where-Object

Get-ChildItem |
    Where-Object Name -Like 'foo'

Get-ChildItem |
    ForEach-Object Name

Actual behavior

Get-ChildItem |
Name -Like 'foo'

Get-ChildItem |
Name

All enum types:

Mode: 'IncreaseIndentationAfterEveryPipeline'

Get-ChildItem |
    Where-Object Name -Like 'foo'

Get-ChildItem |
    ForEach-Object Name


Mode: 'IncreaseIndentationForFirstPipeline'

Get-ChildItem |
    Where-Object Name -Like 'foo'

Get-ChildItem |
    ForEach-Object Name


Mode: 'IncreaseIndentationAfterEveryPipeline'

Get-ChildItem |
    Where-Object Name -Like 'foo'

Get-ChildItem |
    ForEach-Object Name


Mode: 'NoIndentation'

Get-ChildItem |
Where-Object Name -Like 'foo'

Get-ChildItem |
ForEach-Object Name

Mode: 'None'

Get-ChildItem |
Name -Like 'foo'

Get-ChildItem |
Name


Mode: ''

Get-ChildItem |
    Where-Object Name -Like 'foo'

Get-ChildItem |
    ForEach-Object Name

Environment data

Occurs in normal powershell and vscode-powershell’s integrated terminal

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
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.19.1
1.19.1

The existing issues like #1168 deal with formatting, not mutating code

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bergmeistercommented, Nov 8, 2021

Thanks for the updated repro info, I know there were a few cases with the formatter removing code in 1.19.0 that were fixed in 1.19.1 but it seems there is still one case outstanding (or re-introduced), I will take a look at it this week ✋. At least it’s only for a non-default settings and the affected PipelineIndentationStyle.None option was also only introduced in 1.19.0.

1reaction
SydneyhSmithcommented, Nov 4, 2021

I was able to reproduce this on 1.20.0 with indentation style ‘none’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formatting .where and .foreach methods is incorrect #1168
When using the methods .where and .foreach in VS Code there's an incorrect indentation of the closing brackets and a space is added...
Read more >
Restoring (Recovering) PowerShell Scripts from Event Logs
VERBOSE: Running PSUseConsistentIndentation rule. ... post from Mathias Jessen on how to recover deleted PowerShell scripts from the cache.
Read more >
Using PSScriptAnalyzer to check PowerShell version ...
The new compatibility checking functionality is provided by three new rules: PSUseCompatibleSyntax, which checks whether a syntax used in a script will work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found