Skip to content

powershell auto-format indent not tab indenting correctly #1958

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

Closed
ebmarquez opened this issue May 13, 2019 · 4 comments
Closed

powershell auto-format indent not tab indenting correctly #1958

ebmarquez opened this issue May 13, 2019 · 4 comments

Comments

@ebmarquez
Copy link

ebmarquez commented May 13, 2019

Issue Type: Bug

Updating a psm1 function the auto format is incorrectly setting the tabs for the second line past the param().

function verb-noun {
    <#
        .SYNOPSIS
        example

        .INPUTS
        String
        System.Management.Automation.PSCredential

        .OUTPUTS
        System.Management.Automation.PSCustomObject

        .NOTES

    #>
    [CmdLetBinding()]
    [OutputType([PSCustomObject])]
    Param(

        # Computer
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Computer,

        # Credential
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    $cmd = Get-ShowCommands | Where-Object { $_.Name -eq 'running-config' }
$result = Invoke-NetDeviceShowCommand -Computer $Computer -Credential $Credential -Command $cmd.CommandLine
Add-Member -InputObject $result -MemberType NoteProperty -Name "Name" -Value $cmd.Name
return $result
}

Settings JSON

{
    "files.autoSave": "afterDelay",
    "editor.formatOnSave": true,
    "editor.formatOnPaste": true,
    "editor.formatOnType": true,
    "files.defaultLanguage": "powershell",
    "files.enableTrash": false,
    "files.trimTrailingWhitespace": true,
    "files.trimFinalNewlines": true,
    "powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationForFirstPipeline",
    "powershell.codeFormatting.useCorrectCasing": true,
    "powershell.debugging.createTemporaryIntegratedConsole": true,
    "git.autofetch": true
}

Logs

5/13/2019 9:56:54 AM [NORMAL] - Visual Studio Code v1.33.1 64-bit
5/13/2019 9:56:54 AM [NORMAL] - PowerShell Extension v1.12.1
5/13/2019 9:56:54 AM [NORMAL] - Operating System: Windows 64-bit
5/13/2019 9:56:54 AM [NORMAL] - Path specified by 'powerShellExePath' setting - '' - not found, reverting to default PowerShell path.
5/13/2019 9:56:54 AM [NORMAL] - Language server starting --
5/13/2019 9:56:54 AM [NORMAL] -     exe: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
5/13/2019 9:56:54 AM [NORMAL] -     args: C:\Users\emarq\.vscode\extensions\ms-vscode.powershell-1.12.1\modules\PowerShellEditorServices\Start-EditorServices.ps1 -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '1.12.1' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath 'C:\Users\emarq\.vscode\extensions\ms-vscode.powershell-1.12.1\modules' -EnableConsoleRepl -LogLevel 'Diagnostic' -LogPath 'C:\Users\emarq\.vscode\extensions\ms-vscode.powershell-1.12.1\logs\1557766614-fb346685-a289-43f6-aaec-e284919b3d7a1557766611178\EditorServices.log' -SessionDetailsPath 'C:\Users\emarq\.vscode\extensions\ms-vscode.powershell-1.12.1\sessions\PSES-VSCode-17324-476715' -FeatureFlags @()
5/13/2019 9:56:55 AM [NORMAL] - powershell.exe started, pid: 8816
5/13/2019 9:56:56 AM [NORMAL] - Language server started.
5/13/2019 9:56:56 AM [NORMAL] - {"languageServiceTransport":"NamedPipe","languageServicePipeName":"\\\\.\\pipe\\PSES_nwfi4set.gka","debugServiceTransport":"NamedPipe","status":"started","debugServicePipeName":"\\\\.\\pipe\\PSES_zz4eppmk.g3d"}
5/13/2019 9:56:56 AM [NORMAL] - Connecting to language service on pipe \\.\pipe\PSES_nwfi4set.gka...
5/13/2019 9:56:56 AM [NORMAL] - Language service connected.
5/13/2019 9:57:00 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:04 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:04 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:05 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:05 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:05 AM [VERBOSE] - Document formatting finished in 4.673s
5/13/2019 9:57:08 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:08 AM [VERBOSE] - Document formatting finished in 0.298s
5/13/2019 9:57:09 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:09 AM [VERBOSE] - Document formatting finished in 0.127s
5/13/2019 9:57:09 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:09 AM [VERBOSE] - Document formatting finished in 0.073s
5/13/2019 9:57:09 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:09 AM [VERBOSE] - Document formatting finished in 0.086s
5/13/2019 9:57:09 AM [VERBOSE] - Formatting entire document - file:///c%3A/Users/emarq/source/repos/AzureStack/Solution/OnRamp/src/Utils/Azs-PtkNetDevice/DellEmc/ShowCommand/ShowCommand.psm1...
5/13/2019 9:57:09 AM [VERBOSE] - Document formatting finished in 0.088s

Extension version: 1.12.1
VS Code version: Code 1.33.1 (51b0b28134d51361cf996d2f0a1c698247aeabd8, 2019-04-11T08:27:14.102Z)
OS version: Windows_NT x64 10.0.18362

System Info
Item Value
CPUs Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz (8 x 2112)
GPU Status 2d_canvas: enabled
checker_imaging: disabled_off
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: disabled_software
rasterization: enabled
surface_synchronization: enabled_on
video_decode: enabled
webgl: enabled
webgl2: enabled
Memory (System) 15.93GB (7.03GB free)
Process Argv
Screen Reader no
VM 0%
@rjmholt
Copy link
Contributor

rjmholt commented May 13, 2019

Hi @ebmarquez, thanks for opening an issue.

This is likely to be a bug in PSScriptAnalyzer, which provides us with formatting functions.

Can you please check this by running Get-Content -Raw ./your/PowerShell/script.ps1 | Invoke-Formatter -Settings @{ Rules = @{ PSUseConsistentIndentation = @{ Enable = true; PipelineIndentationStyle = 'IncreaseIndentationForFirstPipeline'; IndentationSize = 4; Kind = 'space' } } } (I might have that invocation slightly wrong. The settings are translated here and the cmdlet documentation is here)

@ebmarquez
Copy link
Author

yeah, that's not working. I'm trying to figure out the correct syntax.

@ebmarquez
Copy link
Author

This seems to do what you were intending.

$PSUseConsistentIndentation = @{ 
            Enable = 'true'
            IndentationSize     = 4;
             PipelineIndentation = 'IncreaseIndentationForFirstPipeline'; 
             Kind                = 'space'
        }
$rules = @{
    PSUseConsistentIndentation = $PSUseConsistentIndentation;
}

$scriptDefinition = Get-Content -Raw C:\ShowCommand.psm1
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings @{ 
    IncludeRules = @('PSUserConsistentIndentation')
    Rules        = $rules
}

and it looks like it is indenting incorrectly.

@rjmholt
Copy link
Contributor

rjmholt commented May 13, 2019

Ah, yeah, I think the formatting rules require that IncludeRules part.

Ok, that's definitely a PSScriptAnalyzer issue. I've opened a new issue there (linked above).

@rjmholt rjmholt closed this as completed May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants