Switch block - error thrown if comment is on the second line of a Switch block #16889

Closed
opened 2026-01-31 05:26:14 +00:00 by claunia · 3 comments
Owner

Originally created by @thegraffix on GitHub (Feb 27, 2022).

Windows Terminal Version

1.11.3471.0

Windows Build Number

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19041  1320

Steps to reproduce

Code example below taken directory from Microsoft

Error:

$day = 3
switch ( $day )
{
    # This comment will throw an error in WindowsTerminal.exe v1.11.3471.0 (but not in PowerShell.exe or the ISE)
    0 { $result = 'Sunday'    }
    1 { $result = 'Monday'    }
    2 { $result = 'Tuesday'   }
    3 { $result = 'Wednesday' }
    4 { $result = 'Thursday'  }
    5 { $result = 'Friday'    }
    6 { $result = 'Saturday'  }
}
$result

At line:2 char:2
+ {
+  ~
Missing condition in switch statement clause.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingSwitchConditionExpression

No error:

$day = 3
switch ( $day )
{
    0 { $result = 'Sunday'    }
    # This comment will not throw an error in WindowsTerminal.exe, nor PowerShell.exe, nor ISE
    1 { $result = 'Monday'    }
    2 { $result = 'Tuesday'   }
    3 { $result = 'Wednesday' }
    4 { $result = 'Thursday'  }
    5 { $result = 'Friday'    }
    6 { $result = 'Saturday'  }
}
$result
Wednesday

Also no error:

$day = 3
switch ( $day )
{   # This comment will not throw an error in WindowsTerminal.exe, nor PowerShell.exe, nor ISE
    0 { $result = 'Sunday'    }
    1 { $result = 'Monday'    }
    2 { $result = 'Tuesday'   }
    3 { $result = 'Wednesday' }
    4 { $result = 'Thursday'  }
    5 { $result = 'Friday'    }
    6 { $result = 'Saturday'  }
}
$result
Wednesday

Expected Behavior

I expect Windows Terminal to allow a comment on the second line of the block since PowerShell.exe (5.1) and the ISE (v10.0.19041.1) allow it.

Actual Behavior

This error is thrown:

At line:2 char:2
+ {
+  ~
Missing condition in switch statement clause.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingSwitchConditionExpression
Originally created by @thegraffix on GitHub (Feb 27, 2022). # Windows Terminal Version 1.11.3471.0 # Windows Build Number ``` Major Minor Build Revision ----- ----- ----- -------- 10 0 19041 1320 ``` # Steps to reproduce [Code example](https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch?view=powershell-5.1) below taken directory from Microsoft Error: ``` $day = 3 switch ( $day ) { # This comment will throw an error in WindowsTerminal.exe v1.11.3471.0 (but not in PowerShell.exe or the ISE) 0 { $result = 'Sunday' } 1 { $result = 'Monday' } 2 { $result = 'Tuesday' } 3 { $result = 'Wednesday' } 4 { $result = 'Thursday' } 5 { $result = 'Friday' } 6 { $result = 'Saturday' } } $result At line:2 char:2 + { + ~ Missing condition in switch statement clause. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingSwitchConditionExpression ``` No error: ``` $day = 3 switch ( $day ) { 0 { $result = 'Sunday' } # This comment will not throw an error in WindowsTerminal.exe, nor PowerShell.exe, nor ISE 1 { $result = 'Monday' } 2 { $result = 'Tuesday' } 3 { $result = 'Wednesday' } 4 { $result = 'Thursday' } 5 { $result = 'Friday' } 6 { $result = 'Saturday' } } $result Wednesday ``` Also no error: ``` $day = 3 switch ( $day ) { # This comment will not throw an error in WindowsTerminal.exe, nor PowerShell.exe, nor ISE 0 { $result = 'Sunday' } 1 { $result = 'Monday' } 2 { $result = 'Tuesday' } 3 { $result = 'Wednesday' } 4 { $result = 'Thursday' } 5 { $result = 'Friday' } 6 { $result = 'Saturday' } } $result Wednesday ``` # Expected Behavior I expect Windows Terminal to allow a comment on the second line of the block since PowerShell.exe (5.1) and the ISE (v10.0.19041.1) allow it. # Actual Behavior This error is thrown: ``` At line:2 char:2 + { + ~ Missing condition in switch statement clause. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingSwitchConditionExpression ```
claunia added the Needs-TriageResolution-ExternalNeeds-Tag-FixProduct-Powershell labels 2026-01-31 05:26:15 +00:00
Author
Owner

@elsaco commented on GitHub (Feb 27, 2022):

@thegraffix the error is thrown also when pwsh is using conhost only:

wt_12588

Same error when pwsh is run on Linux:

wt_linux_12588

@elsaco commented on GitHub (Feb 27, 2022): @thegraffix the error is thrown also when `pwsh` is using `conhost` only: ![wt_12588](https://user-images.githubusercontent.com/3933920/155893262-d1799a20-4bb1-45cd-bbd1-d7440c7a8ee4.png) Same error when `pwsh` is run on Linux: ![wt_linux_12588](https://user-images.githubusercontent.com/3933920/155894189-9f9172f4-46bd-4ff9-981e-72d9a1d398c1.png)
Author
Owner

@237dmitry commented on GitHub (Feb 27, 2022):

This is rather the behavior of pwsh itself. The same in the native linux (not WSL) in any terminal emulator.
I am always use invocation script block in order to escape of creation of new variables in the main runspace.

PS > & {
>>    $a = 1
>>    switch ($a)
>>    {
>>       # comment
>>       1 {  $r = 'a' }
>>       2 {  $r = 'b' }
>>    }
>>    $r
>>  }
@237dmitry commented on GitHub (Feb 27, 2022): This is rather the behavior of pwsh itself. The same in the native linux (not WSL) in any terminal emulator. I am always use invocation script block in order to escape of creation of new variables in the main runspace. ```powershell PS > & { >> $a = 1 >> switch ($a) >> { >> # comment >> 1 { $r = 'a' } >> 2 { $r = 'b' } >> } >> $r >> } ```
Author
Owner

@zadjii-msft commented on GitHub (Feb 28, 2022):

Huh. This has to do with the way powershell is reading the text from the clipboard (for some reason).

If you unbind ctrl+v in your settings, that'll let PowerShell use its own built-in paste handler.

        {
            "command": "unbound",
            "keys": "ctrl+v"
        },

If you do that, the the code snippet can be pasted perfectly fine in the Terminal
image

In fact, if you type this in line-by-line, you'll get the same error even in conhost:

image

I'd recommend following up upstream at https://github.com/powershell/powershell/. This looks like it's just something weird that powershell does.

@zadjii-msft commented on GitHub (Feb 28, 2022): Huh. This has to do with the way powershell is reading the text from the clipboard (for some reason). If you unbind `ctrl+v` in your settings, that'll let PowerShell use its own built-in paste handler. ```json { "command": "unbound", "keys": "ctrl+v" }, ``` If you do that, the the code snippet can be pasted perfectly fine in the Terminal ![image](https://user-images.githubusercontent.com/18356694/155975496-6cf18339-fed5-45f5-9e8d-d87967016e43.png) In fact, if you type this in line-by-line, you'll get the same error even in conhost: ![image](https://user-images.githubusercontent.com/18356694/155975742-1b8f5e2a-1420-43ac-bdfc-eca1fde17691.png) I'd recommend following up upstream at https://github.com/powershell/powershell/. This looks like it's just something weird that powershell does.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#16889