PowerShell / PSScriptAnalyzer

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

formatting partial / dot sourced ps1 dsc scripts #1081

Open sfcmgr opened 6 years ago

sfcmgr commented 6 years ago

Steps to reproduce

I originally opened this on the vscode-powershell#1583 project, but I've been directed to open an issue here directly.

I'm currently creating DSC Resource modules to apply settings in our environment, I'm breaking down the actual settings to individual files to make managing the settings easier and to have an easy structure of those settings.

The issue is that the formatter is not performing the align dsc resource / hash table function on the files to import.

It's not really relevant but i'm using the following code in my policy.schema.psm1 file

Configuration Win10CustomComputerPolicy
{
    Param(
        [Parameter(Mandatory = $false)]
        [array]
        $excludeImports = @()
    )

    Import-DscResource -ModuleName PSDesiredStateConfiguration

    $importParams = @{
        Path        = $PSScriptRoot
        Exclude     = $excludeImports
        Filter      = '*.ps1'
        ErrorAction = 'SilentlyContinue'
        Recurse     = $true
    }
    $imports = @(Get-ChildItem @importParams)

    #Dot source the files
    Foreach ($import in $imports) {
        Try {
            . $import.fullname
        }
        Catch {
            Write-Error -Message "Failed to import function $($import.fullname): $_"
        }
    }
}

each individual file then looks like this:

DisableLocalMachineRunOnce.ps1

#\System\Logon\Do not process the run once list
Registry DisableLocalMachineRunOnce {
    Ensure = "Present"
    Key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}

most of the formatting works, but not the alignment, if I add a configuration section around the code it works, but that would break my import.

#\System\Logon\Do not process the run once list
configuration test {
    Registry DisableLocalMachineRunOnce {
        Ensure    = "Present"
        Key       = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
        Force     = $true
        ValueName = "DisableLocalMachineRunOnce"
        ValueType = "DWord"
        ValueData = "1"
    }
}

I can provide a bit more information when I'm back in the office tomorrow if any is needed, but I wanted to make sure I'd opened the issue since I was aware.

Expected behavior

Registry DisableLocalMachineRunOnce {
    Ensure    = "Present"
    Key       = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}

Actual behavior

Registry DisableLocalMachineRunOnce {
    Ensure = "Present"
    Key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}

Environment data

Name Value
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.17134.228
BuildVersion 10.0.17134.228
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

(Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

I'm using PSScriptAnalyzer as part of vscode-powershell using the current version.

bergmeister commented 5 years ago

@chrisshearing The behaviour that you describe is because the default of the VS Code setting powershell.codeFormatting.alignPropertyValuePairs is set to true by default. To get your expected formatting, set the VS Code setting powershell.codeFormatting.alignPropertyValuePairs to false. Can you please confirm that this fixes your issue?

sfcmgr commented 5 years ago

Hi @bergmeister ,

I was just wondering how you came to that conclusion, when I realised that when transferring the bug report from vscode-powershell, I managed to reverse the expected and actual behaviour code snippets! (I've corrected them in the original post now)

Sorry about that, powershell.codeFormatting.alignPropertyValuePairs is set to true, but it is failing to align the key / value pairs when the dsc resource is alone in an included powershell file, which is what I would have expected to happen

bergmeister commented 5 years ago

@chrisshearing The powershell.codeFormatting.alignPropertyValuePairs is a mapping to the AlignAssignmentStatement rule from PSScriptAnalyzer that the VS Code extension configures with this setting when calling the formatter. Locally, I cannot reproduce the issue, having the setting set to true, aligns the assignments and setting it to false set changes it to your actual behaviour. Have you re-opened VS Code after changing the setting? Do you use PSSA setting files (that may override the settings)? To confirm the latter, please provide the output of Invoke-ScriptAnalyzer -scriptdefinition foo -Verbose and Invoke-Formatter -ScriptDefinition foo -Verbose when being executed from within the VS code integrated terminal?

sfcmgr commented 5 years ago

@bergmeister I have not changed the setting from the default true ever - it works correctly in all other circumstances I'm using it in, except this one,

I've just downloaded the latest release of psscriptanalyzer and verified it's still an issue on the command line

I am not using any settings that override the settings

are you using exactly this code and nothing else in the .ps1 file or directly using the invoke-formatter command to test?

Registry DisableLocalMachineRunOnce {
    Ensure = "Present"
    Key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}

I do not have access to my work environment on a saturday night - but as I mentioned the behaviour is exactly the same with the command line usage of invoke-formatter,

Here is a reproduction of the issue, you can see with the configuration statement the formatter works, without it, it doesn't and below is the output of the commands you requested.

PS D:\test> Invoke-Formatter {configuration test {
>>     Registry DisableLocalMachineRunOnce {
>>         Ensure    = "Present"
>>         Key    = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
>>         Force     = $true
>>         ValueName = "DisableLocalMachineRunOnce"
>>         ValueType = "DWord"
>>         ValueData = "1"
>>     }
>> }}
configuration test {
    Registry DisableLocalMachineRunOnce {
        Ensure    = "Present"
        Key       = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
        Force     = $true
        ValueName = "DisableLocalMachineRunOnce"
        ValueType = "DWord"
        ValueData = "1"
    }
}
PS D:\test>
PS D:\test> Invoke-Formatter {    Registry DisableLocalMachineRunOnce {
>>         Ensure    = "Present"
>>         Key    = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
>>         Force     = $true
>>         ValueName = "DisableLocalMachineRunOnce"
>>         ValueType = "DWord"
>>         ValueData = "1"
>>     }}
Registry DisableLocalMachineRunOnce {
    Ensure    = "Present"
    Key    = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
    Force     = $true
    ValueName = "DisableLocalMachineRunOnce"
    ValueType = "DWord"
    ValueData = "1"
}
PS D:\test>
PS D:\test> Invoke-ScriptAnalyzer -scriptdefinition foo -Verbose
VERBOSE: Settings object could not be resolved.
VERBOSE: Analyzing Script Definition.
PS D:\test> Invoke-Formatter -ScriptDefinition foo -Verbose
VERBOSE: Using settings file at C:\Program
Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.17.1\Settings\CodeFormatting.psd1.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSPlaceCloseBrace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSPlaceOpenBrace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSUseConsistentWhitespace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSUseConsistentIndentation rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSAlignAssignmentStatement rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
foo
bergmeister commented 5 years ago

Thanks for the detailed response. I will look into this, this week is a bit busy but I hope to do it at least by the end of the month

sfcmgr commented 5 years ago

Hi @bergmeister ,

Did you manage to reproduce the issue?

bergmeister commented 5 years ago

@chrisshearing I believe this is due to #769 which I hope to fix in PSSA 1.18 or at least 1.19 in the next months