PowerShell / PSScriptAnalyzer

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

Compressed/minified one-liner output #2043

Open suchverynice opened 12 hours ago

suchverynice commented 12 hours ago

Per subject, can't figure out why when running PSSA via dprint-plugin-exec and/or via bash???

Steps to reproduce

// dprint config fail
{
    lineWidth: 80,
    indentWidth: 2,
    newLineKind: 'auto',
    useTabs: false,
    incremental: false,
    exec: {
        cwd: '${configDir}',
        commands: [
            {
                // Works, but why minified?!
                command: 'pwsh -noprofile -NonInteractive -Command "Invoke-Formatter -ScriptDefinition $Input"',
                stdin: true,
                exts: ['ps1', 'psm1', 'psd1']
            }
        ]
    },
    plugins: [
        'https://plugins.dprint.dev/exec-0.5.0.json@8d9972eee71fa1590e04873540421f3eda7674d0f1aae3d7c788615e7b7413d0'
    ]
}

Got the same "minified" results in bash:

# GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
# Same "minified" results
echo 'string-of-code' | pwsh -noprofile -noninteractive -c 'Invoke-Formatter -ScriptDefinition $input'
cat 'path/to/file' | pwsh -noprofile -noninteractive -c 'Invoke-Formatter -ScriptDefinition $input'

While either of below works:

// Alternate dprint config that works with same npm run script...
{
    // ...
    exec: {
        cwd: '${configDir}',
        commands: [
            // Works
            {
                command: 'pwsh -noprofile -NonInteractive -Command Invoke-Formatter -ScriptDefinition (Get-Content -Raw -Path {{file_path}})',
                stdin: false,
                exts: ['ps1', 'psm1', 'psd1']
            },
            // Or, this works too
            {
                command: "pwsh -noprofile -NonInteractive -Command Invoke-Formatter -ScriptDefinition ([System.IO.File]::ReadAllText('{{file_path}}'))",
                stdin: false,
                exts: ['ps1', 'psm1', 'psd1']
            }
        ]
    },
    plugins: [
        'https://plugins.dprint.dev/exec-0.5.0.json@8d9972eee71fa1590e04873540421f3eda7674d0f1aae3d7c788615e7b7413d0'
    ]
}
# Works via pwsh
cat 'in.ps1' | Invoke-Formatter
cat 'in.ps1' | Invoke-Formatter | Out-File -FilePath 'out.ps1' -Encoding utf8
cat 'in.ps1' | Invoke-Formatter | Set-Content -Path 'out.ps1' -Encoding utf8
([System.IO.File]::ReadAllText('in.ps1')) | Invoke-Formatter
([System.IO.File]::ReadAllText('in.ps1')) | Invoke-Formatter | Out-File -FilePath 'out.ps1' -Encoding utf8
([System.IO.File]::ReadAllText('in.ps1')) | Invoke-Formatter | Set-Content -Path 'out.ps1' -Encoding utf8
Get-Content -Raw -Path 'in.ps1' | Invoke-Formatter
Get-Content -Raw -Path 'in.ps1' | Invoke-Formatter | Out-File -FilePath 'out.ps1' -Encoding utf8
Get-Content -Raw -Path 'in.ps1' | Invoke-Formatter | Set-Content -Path 'out.ps1' -Encoding utf8

Expected behavior

# nicely formatted code

Actual behavior

function New-Profile { Write-Host "Running New-Profile function"        $profileName = Split-Path $profile -Leaf  if (Test-Path $profile) { Write-Error "Profile $profileName already exists on this computer." } else { New-Item -type file -Path $profile -Force } }  dir | % { "crtime: {0}" -f $_.creationtime } start-process example.exe start anotherexample.exe & "./dir/meow.exe"  enum Color { Black, White }  function Test-Code { [CmdletBinding()] param (        [int]$ParameterOne  )   end { if (10 -gt $ParameterOne) { "Greater" }       else { "Lesser" } } } enum Color { Black, White }  function Test-Code { [CmdletBinding()] param( [int]$ParameterOne   )   end { if (10 -gt $ParameterOne) { "Greater" } else { "Lesser" } } }

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.2
PSEdition                      Core
GitCommitId                    7.3.2
OS                             Linux 6.2.16 #1-NixOS SMP PREEMPT_DYNAMIC Tue Jan  1 00:00…
Platform                       Unix
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