Skip to content

PSParser Tokenize with Switch Default fails #13462

@LAltinell

Description

@LAltinell

Trouble when trying to Tokenize a script containing a Switch-statement with a Default case in Powershell 7.1.0-preview.5. This works as expected in Powershell 7.0.3 (stable). If one does comment-out the default case, it works as expected. This has been reproduced on more than one computer.

Steps to reproduce

Create a file containing a simple function that has a switch-statement with a default case (see below). Save that file. Then try to tokenize that file (also see below) from a console running Powershell 7.1.0-preview.5.

# Save this as a file called 'TEST.ps1'
Function Test {
    Param (
        $String
    )

    Switch ($String) {
        "test" {
            "sfdsf"
        }

        "test2" {
            "sfdsf"
        }

        default {
            "sdfsdf"
        }
    }
}
# From a Powershell 7.1.0-preview.5 prompt execute the following:
$scripts = Get-ChildItem -Path "C:\__TEMP\TEST.ps1"

Foreach ($file in $scripts) {
    $contents = Get-Content -Path $file.fullname -ErrorAction Stop
    $errors = $null
    $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
    $errors.Count -eq 0
}

Expected behavior

True

Actual behavior

MethodInvocationException:
Line |
   4 |      $null = [System.Management.Automation.PSParser]::Tokenize($conten|      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Tokenize" with "2" argument(s): "Index was outside the bounds of the array."
True

Environment data


Name                           Value
----                           -----
PSVersion                      7.1.0-preview.5
PSEdition                      Core
GitCommitId                    7.1.0-preview.5
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productMustHaveResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions