Skip to content

Problem with formatting Hash #1219

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
PrzemyslawKlys opened this issue Apr 17, 2019 · 6 comments · Fixed by #1235
Closed

Problem with formatting Hash #1219

PrzemyslawKlys opened this issue Apr 17, 2019 · 6 comments · Fixed by #1235

Comments

@PrzemyslawKlys
Copy link
Contributor

PrzemyslawKlys commented Apr 17, 2019

Steps to reproduce

I hvae simple .psd1 like this.

#
# Module manifest for module 'PSPublishModule'
#
# Generated by: Przemyslaw Klys
#
# Generated on: 17.04.2019
#

@{

    # Script module or binary module file associated with this manifest.
    RootModule           = 'PSPublishModule.psm1'

    # Version number of this module.
    ModuleVersion        = '0.7.1'

    # Supported PSEditions
    CompatiblePSEditions = 'Desktop', 'Core'

    # ID used to uniquely identify this module
    GUID                 = 'eb76426a-1992-40a5-82cd-6480f883ef4d'

    # Author of this module
    Author               = 'Przemyslaw Klys'

    # Company or vendor of this module
    CompanyName          = 'Evotec'

    # Copyright statement for this module
    Copyright            = '(c) 2011 - 2019 Przemyslaw Klys. All rights reserved.'

    # Description of the functionality provided by this module
    Description          = 'Simple project allowing preparing, managing and publishing modules to PowerShellGallery'

    # Minimum version of the Windows PowerShell engine required by this module
    PowerShellVersion    = '5.1'

    # Name of the Windows PowerShell host required by this module
    # PowerShellHostName = ''

    # Minimum version of the Windows PowerShell host required by this module
    # PowerShellHostVersion = ''

    # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
    # DotNetFrameworkVersion = ''

    # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
    # CLRVersion = ''

    # Processor architecture (None, X86, Amd64) required by this module
    # ProcessorArchitecture = ''

    # Modules that must be imported into the global environment prior to importing this module
    # RequiredModules = @()

    # Assemblies that must be loaded prior to importing this module
    # RequiredAssemblies = @()

    # Script files (.ps1) that are run in the caller's environment prior to importing this module.
    # ScriptsToProcess = @()

    # Type files (.ps1xml) to be loaded when importing this module
    # TypesToProcess = @()

    # Format files (.ps1xml) to be loaded when importing this module
    # FormatsToProcess = @()

    # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
    # NestedModules = @()

    # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
    FunctionsToExport    = 'New-PrepareModule', 'Remove-Comments'

    # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
    CmdletsToExport      = @()

    # Variables to export from this module
    # VariablesToExport = @()

    # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
    AliasesToExport      = @()

    # DSC resources to export from this module
    # DscResourcesToExport = @()

    # List of all modules packaged with this module
    # ModuleList = @()

    # List of all files packaged with this module
    # FileList = @()

    # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
    PrivateData          = @{

        PSData = @{

            # Tags applied to this module. These help with module discovery in online galleries.
            Tags       = 'Windows', 'MacOS', 'Linux', 'Build', 'Module'

            # A URL to the license for this module.
            # LicenseUri = ''

            # A URL to the main website for this project.
            ProjectUri = 'https://github.com/EvotecIT/PSPublishModule'

            # A URL to an icon representing this module.
            IconUri    = 'https://evotec.xyz/wp-content/uploads/2019/02/PSPublishModule.png'

            # ReleaseNotes of this module
            # ReleaseNotes = ''

        } # End of PSData hashtable

    } # End of PrivateData hashtable

    # HelpInfo URI of this module
    # HelpInfoURI = ''

    # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
    # DefaultCommandPrefix = ''

}

I've run a command that strips white chars and comments and this gives me this:

@{RootModule = 'PSPublishModule.psm1'
    ModuleVersion = '0.7.1'
    CompatiblePSEditions = 'Desktop', 'Core'
    GUID = 'eb76426a-1992-40a5-82cd-6480f883ef4d'
    Author = 'Przemyslaw Klys'
    CompanyName = 'Evotec'
    Copyright = '(c) 2011 - 2019 Przemyslaw Klys. All rights reserved.'
    Description = 'Simple project allowing preparing, managing and publishing modules to PowerShellGallery'
    PowerShellVersion = '5.1'
    ScriptsToProcess = @()
    FunctionsToExport = 'New-PrepareModule', 'Remove-Comments'
    CmdletsToExport = @()
    AliasesToExport = @()
    PrivateData = @{PSData = @{Tags = 'Windows', 'MacOS', 'Linux', 'Build', 'Module'
            ProjectUri = 'https://github.com/EvotecIT/PSPublishModule'
            IconUri = 'https://evotec.xyz/wp-content/uploads/2019/02/PSPublishModule.png'}}}

image

And during runtime of formater:

image

image

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.316
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.316
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

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

1.17.1
1.18.0
writeErrorStream      : True
PSMessageDetails      : 
Exception             : System.IndexOutOfRangeException: Index was outside the bounds of the array.
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.PlaceCloseBrace.GetIndentation(Token[] tokens, Int32 closeBracePos, Int32 openBracePos)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.PlaceCloseBrace.GetCorrectionsForBraceShouldBeOnNewLine(Token[] tokens, Int32 closeBracePos, Int32 openBracePos, 
                        String fileName)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.PlaceCloseBrace.GetViolationForBraceShouldBeOnNewLine(Token[] tokens, Int32 closeBracePos, Int32 openBracePos, St
                        ring fileName)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.PlaceCloseBrace.AnalyzeScript(Ast ast, String fileName)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()
TargetObject          : 
CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], IndexOutOfRangeException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Format-Code, C:\Users\przemyslaw.klys\OneDrive - Evotec\Support\GitHub\PSPublishModule\Private\Format-PSM1.ps1: line 17
                        at Merge-Module, C:\Users\przemyslaw.klys\OneDrive - Evotec\Support\GitHub\PSPublishModule\Private\Merge-Module.ps1: line 80
                        at New-PrepareModule<Process>, C:\Users\przemyslaw.klys\OneDrive - Evotec\Support\GitHub\PSPublishModule\Public\New-PrepareModule.ps1: line 221
                        at <ScriptBlock>, C:\Users\przemyslaw.klys\OneDrive - Evotec\Support\GitHub\PSPublishModule\Publish\Manage-Module.ps1: line 143
PipelineIterationInfo : {0, 1}

@bergmeister
Copy link
Collaborator

bergmeister commented Apr 22, 2019

Thanks, I can repro. This seems to be a bug from day 1 as the rule has not changed since 2017. The problem happens on this line where openBracePos is 0 when trying to close the outmost brace, it has nothing to do with hashes. Inserting a newline at the first line or putting the last brace on a new line works as a temporary workaround

@PrzemyslawKlys
Copy link
Contributor Author

Great! Thank you. I can wait for a fix. Temporary I've disabled that rule. I only use comments/whitespace stripping for publishing modules (leaving only comments that are "help"). Rest of the comments/whitespace is stripped. So not really a big deal for me.

@bergmeister
Copy link
Collaborator

Ok, there is no need for whitespace trimming if you publish to the PSGallery or NuGet based feed like Azure Artifacts because a module gets downloaded/uploaded as a NuGet package, i.e. it gets compressed, therefore the trimming does not have much of an impact on the size anyway.

@PrzemyslawKlys
Copy link
Contributor Author

PrzemyslawKlys commented Apr 23, 2019

I do it for a bit different purpose. I actually clean up all my dev comments, old commented out code, new commented out code that is not yet ready and some other stuff that shouldn't be in code when publishing.

You can see the diff here:

Or here:

Functionality is the same, output much smaller, less junk. So I apply my code that strips down comments but while it does it strips down any formatting (including whitespaces). And then I bring back formatting/whitespaces that make code readable using your formatter.

If one wants to have original data all code is available on GitHub https://github.com/EvotecIT/PSWriteHTML with each function having a separate file. So this process is only used for publishing (merging into 1 file, cleaning up and so on). I believe it makes my modules a bit faster to load (in case of just merging it can take 12-15 seconds to load unmerged module vs 250ms for merged.

I'm not applying the same approach to PSD1, but I was able to reproduce it on PSD1 in an easier way, then trying to pinpoint the problem in psm1 file (as I got similar crashes in there).

@bergmeister
Copy link
Collaborator

Bug is boiled down. It is due to the hashtable opening character @{ being the first token and the code resulting in a PSCloseBrace violation. Code path that creates this violation was looking at the token before the opening brace, which caused it to crash because it was the first token, I opened a PR to make it handle this edge case

@PrzemyslawKlys
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants