Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell reports MissingEndParenthesisInExpression even though the sub-expression is balanced

Tags:

powershell

Here is my sub-expression:

    """$((($l -split " """)[1] -split """ ")[0])"""

I have checked and found no unpaired parentheses. However powershell insists in saying "Missing closing ')' in expression." Interestingly, the expression

    $((($l -split " """)[1] -split """ ")[0])

works fine.

Has anyone had similiar experience before? Is it a bug of Powershell?

like image 203
hunter.wang Avatar asked Oct 28 '25 04:10

hunter.wang


1 Answers

This.. is really interesting, and yes I would consider it a bug at least so far.

Here's a much simpler repro:

"$("`"")"
"$("""")"

It seems to be caused by using either form of double quote escaping (backtick or double double quote), inside a sub-expression, inside an expandable string.

It also appears that this is error comes right down to the parser itself:

$sb = @'
"$("`"")"
'@

$tokens = $null
$er = $null

$ast = [System.Management.Automation.Language.Parser]::ParseInput($sb, [ref]$tokens, [ref]$er)

$ast.EndBlock.Statements[0].PipelineElements[0].Expression.NestedExpressions

$tokens[0].NestedTokens

The nested tokens/expressions it finds just aren't correct.

I tested with Windows PowerShell 5.1 and PowerShell Core 6.0.0-rc.2 on WLS.

Relevant Issues

  • Two double-quotes in inline expressions
  • String in Sub-Expression incorrectly parsed
  • Powershell: Doubled double quotes in inline expressions
like image 168
briantist Avatar answered Oct 30 '25 05:10

briantist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!