Skip to content

Parse Error in PS5 #135

Closed
Closed

Description

@dan-hughes

Module does not load in PowerShell 5.1, it seems to be unhappy with the below.

Version = if (($V = $BuildInfo.SemVer.Split("+")[0].Split("-", 2)[0])) {
[version]$V
}

Run Import-Module in PS5 to reproduce. PS7 is fine.

Activity

  1. johlju commented on Feb 17, 2025

    @johlju
    Contributor

    I think I got the same error, it starts failing on line 505 in ModuleBuilder which is the line 42 in the above snippet from InitializeBuild.ps1.

    It happens in integration tests in Sampler module pipeline that is using PS5.1, integration tests for PS7.x works.

    The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: At D:\a\1\s\output\RequiredModules\ModuleBuilder\3.1.6\ModuleBuilder.psm1:505 char:30
    ##[error]     +                             }
    ##[error]     +                              ~
    ##[error]     The hash literal was incomplete.
    ##[error]     At D:\a\1\s\output\RequiredModules\ModuleBuilder\3.1.6\ModuleBuilder.psm1:509 char:34
    ##[error]     +     } elseif($BuildInfo.Version) {
    ##[error]     +                                  ~
    ##[error]     Unexpected token '{' in expression or statement.
    ##[error]     At D:\a\1\s\output\RequiredModules\ModuleBuilder\3.1.6\ModuleBuilder.psm1:533 char:1
    ##[error]     + }
    ##[error]     + ~
    
  2. grey0ut commented on Mar 6, 2025

    @grey0ut

    I just installed the module in 5.1 and encounter the same error for psm1:505 as well.
    As a test I tried wrapping the value definition in a subexpression and the module loaded.
    Using this code as a test:

    @{
        Key1 = "Somevalue"
        Key2 = if ($R = 2/2) {
            [Boolean]$R
        }
        Key3 = "SomeOtherValue"
    }

    When executing it in Windows Powershell 5.1 it errors that the hashliteral is incomplete.
    When executing it in Pwsh 7.5.0 it completes and outputs successfully.
    Changing the code to use a subexpression like this works in both versions:

    @{
        Key1 = "Somevalue"
        Key2 = $(if ($R = 2/2) {
            [Boolean]$R
        })
        Key3 = "SomeOtherValue"
    }

    Oh, now I see that #136 fixes this by changing the order of the definition. That works too it seems.

  3. dan-hughes commented on Apr 12, 2025

    @dan-hughes
    Author

    Fixed in #136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions