Skip to content

Fix PowerShell parsing for standalone -- tokens - #335465

Open
Ingvar Stepanyan (RReverser) wants to merge 6 commits into
microsoft:mainfrom
RReverser:fix-pwsh-auto-approve-bare-double-dash
Open

Ingvar Stepanyan (RReverser) wants to merge 6 commits into
microsoft:mainfrom
RReverser:fix-pwsh-auto-approve-bare-double-dash

Conversation

@RReverser

@RReverser Ingvar Stepanyan (RReverser) commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes an issue where VSCode would keep asking for approvals for any commands using -- for argument separation, even if such commands have already been included in the autoApprove patterns.

This is caused by treesitter being unable to parse this separator and VSCode forcing explicit approval for any unparsed commands.

Inspired by a similar fix in #310839.

Copilot AI balanced review requested due to automatic review settings September 10, 2026 13:10
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/treeSitterCommandParser.ts
  • src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/electron-browser/treeSitterCommandParser.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Terminal auto-approval parsing is security-sensitive and warrants final human review of PowerShell edge cases.

Pull request overview

Fixes PowerShell parsing of standalone -- tokens used by native CLI tools.

Changes:

  • Masks standalone -- tokens before tree-sitter parsing while preserving offsets.
  • Adds regression coverage for Git pathspecs and npm exec.
File summaries
File Description
treeSitterCommandParser.ts Extends PowerShell native-argument masking.
treeSitterCommandParser.test.ts Adds standalone -- parsing tests.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@iket0731

Copy link
Copy Markdown

Hi Ingvar Stepanyan (@RReverser)

I was also looking into the standalone -- issue in PowerShell. I noticed one concern while reading your PR.

pwshStandaloneDoubleDashRegex replaces standalone -- with __ regardless of context. For example:

$val --  ->  $val __

In my local testing, Tree-sitter parses the original $val -- successfully, but reports an error for $val __. This means that cases where -- is not being used as a native CLI argument separator could become unanalyzable, even though they were previously parsed successfully.

Rather than masking every standalone --, it may be worth revisiting the masking approach so that it only applies when -- is used as a native CLI argument separator. Sharing this in case it is useful.

@RReverser

Copy link
Copy Markdown
Contributor Author

Tree-sitter parses the original $val -- successfully, but reports an error for $val __.

Ugh it just keeps getting weirder. So tree sitter is fine with trailing -- but not a standalone one?

But yeah, good catch if so, I'll update the PR.

@iket0731

Copy link
Copy Markdown

tree-sitter-powershell accepts -- as a decrement operator in PowerShell syntax, but reports it as an error when it appears in a command. (This appears to be a bug or limitation of tree-sitter-powershell.)
This behavior does not depend on the position of --.

$val --  # OK
-- $val  # OK
git diff --  # Error
git diff -- file.txt  # Error

@RReverser

Copy link
Copy Markdown
Contributor Author

Ugh that's trickier to distinguish. I guess an easy option might be to replace -- with (double space instead of __) - in most cases they are equivalent, and it would preserve $val -- -> $val and -- $val -> $val as valid syntax too.

@RReverser
Ingvar Stepanyan (RReverser) force-pushed the fix-pwsh-auto-approve-bare-double-dash branch from d15b09a to 2e599be Compare September 11, 2026 20:07
@RReverser

Copy link
Copy Markdown
Contributor Author

I guess an easy option might be to replace -- with (double space instead of __) - in most cases they are equivalent, and it would preserve $val -- -> $val and -- $val -> $val as valid syntax too.

Did this. I think it's conceptually more correct too - standalone -- is normally used as a whitespace-style separator between whole arguments, which is why the --flag -> __flag trick doesn't apply here.

@RReverser

Copy link
Copy Markdown
Contributor Author

Megan Rogge (@meganrogge) Would really appreciate reviews on this one - GPT models love issuing a lot of git diff -- file1 file2 file3, and without the fix every single one of those interrupts the agentic session to ask for an approval, even if git diff is pre-approved.

@iket0731

Copy link
Copy Markdown

Megan Rogge (@meganrogge) This PR addresses the -- case tracked in #328110 (along with #325635, #324059 and #321748). Would you be able to take a look when you have a chance? Thanks!

@RReverser

Copy link
Copy Markdown
Contributor Author

Martin Aeschlimann (@aeschli) Anthony Kim (@anthonykim1) If Megan is not available, can someone else please prioritise reviewing this PR? The fix is already ready and fairly small - leaving it unresolved pushes users to the less secure /autoApprove variant as a habit, allowing arbitrary code execution on the machine by default, just to avoid frequent session pauses just to approve the git diff -- ... or npm exec -- ... commands.

@RReverser

Copy link
Copy Markdown
Contributor Author

Please, it's been stuck for a long time, wasting hours every week when agent gets stuck in yet another git diff -- foo bar. This is a pretty trivial fix for a very common issue.

@anthonykim1 Anthony Kim (anthonykim1) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing on this. git diff -- … prompting every time is a real pain, and the diagnosis is right. Two things block merging as-is (details inline):

  1. With --, a redirect to a variable (> $target, >> $PROFILE) isn't detected as a file write. On main, the parse error still forced confirmation. With this change that guard clears, so the command becomes auto-approvable.
  2. The new git diff -- test fails because the trailing -- is dropped from the extracted command.

I left suggested fixes for both, plus a non-blocking ask to fail closed on stray --. Agent Host has its own equals-only copy of this workaround, so this fix won't reach it. No need to touch it here; consolidation is tracked in #329028.

@RReverser
Ingvar Stepanyan (RReverser) force-pushed the fix-pwsh-auto-approve-bare-double-dash branch from 2e599be to 5ad59e9 Compare October 17, 2026 10:36
@RReverser

Copy link
Copy Markdown
Contributor Author

Anthony Kim (@anthonykim1) Addressed all comments, please take another look.

@RReverser

Copy link
Copy Markdown
Contributor Author

Anthony Kim (@anthonykim1) Any outstanding concerns? I hate pinging on Github as much as the next OSS maintainer, but this one has genuinely been annoying issue for a while now...

@anthonykim1 Anthony Kim (anthonykim1) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earlier concerns are addressed, but one regression remains in the standalone -- handling. Details inline.

@anthonykim1

Copy link
Copy Markdown
Contributor

lets try to get this in for next week's release

@RReverser

Copy link
Copy Markdown
Contributor Author

Looks like it didn't make it into 144.

@RReverser

Copy link
Copy Markdown
Contributor Author

Oh no, why was it removed from new 1.145 release too? Anthony Kim (@anthonykim1)

@RReverser

Copy link
Copy Markdown
Contributor Author

Aaron Munger (@amunger) This was supposed to go out couple of releases ago - any particular reason it was removed?

@RReverser

Copy link
Copy Markdown
Contributor Author

Ping again... we are so close 😅

@anthonykim1 Anthony Kim (anthonykim1) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is close. The latest commit handles the comment case, and the cases from the earlier reviews now behave as expected. Two small things inline.

Authored with Copilot on behalf of anthonykim1

const isWithin = (span: { start: number; end: number }) => span.start <= start && span.end >= start + 2;
// A `--` inside a command is a native CLI separator, and one inside a comment is inert text;
// anything else was dropped by the parser and must fail closed.
if (!commandSpans.some(isWithin) && !commentSpans.some(isWithin)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same false positive as the comment case, but for strings: a -- inside a string that isn't part of a command now makes the line unanalyzable. git status; 'a -- b', (Get-Content f) -replace ' -- ', ' ' and if ($a -eq ' -- ') { git status } were analyzable on main and now need confirmation, even though git status and Get-Content are allowed by default.

Skipping matches inside single-quoted strings fixes it: capture (verbatim_string_characters) and (verbatim_here_string_characters) in the query on L87 and treat them like comment spans. I'd leave double-quoted strings alone, since a -- inside their $( … ) is code. I tried this against the tests in this file and they all still pass. Could you add git status; 'a -- b' as an analyzable case?

Authored with Copilot on behalf of anthonykim1

Comment on lines +47 to +48
.replace(pwshFlagEqualsRegex, (_, pre, flag) => `${pre}${flag} `)
.replace(pwshStandaloneDoubleDashRegex, (_, pre) => `${pre} `);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two masks chain: the --flag= mask runs first and can create a new standalone --, which the second mask then blanks. So git log --format=-- is reported as an analyzable git log --format instead of failing closed as on main, and git status; --=x becomes analyzable with x captured as a command.

Running the standalone -- mask first fixes both, and the tests in this file all still pass. Could you add git log --format=-- as an unanalyzable case?

Authored with Copilot on behalf of anthonykim1

@RReverser
Ingvar Stepanyan (RReverser) force-pushed the fix-pwsh-auto-approve-bare-double-dash branch from 7cc1b3a to 99aa5d0 Compare November 11, 2026 14:45
@RReverser

Copy link
Copy Markdown
Contributor Author

Anthony Kim (@anthonykim1) The complexity started getting out of hand, and instead of fixing new cases one by one I've gone back to the simpler approach.

Most of the new regressions (-- inside strings, comments, $val --, -- $val) came from trying to make a stray -- like cmd; -- unanalyzable. I don't think that's needed, and it adds a lot of complexity just for an already-invalid syntax.

Stripping a stray -- never hides or adds a command from the approval mechanism: every extracted command still goes through the normal approval rules, and the line itself is rejected by PowerShell's parser (MissingExpressionAfterOperator for git status; --, -- git status and git status; -- git diff), so it never runs. The general fail-closed behavior on tree-sitter parse errors is unchanged.

So I went back to the simpler version while keeping the new tests:

  • mask standalone -- as whitespace, before the --flag= mask, so git log --format=-- and git status; --=x fail closed as on main again
  • drop the stray--- check, so strings (single- and double-quoted), comments and decrements stay analyzable as on main
  • keep the trailing -- slicing and the masked getFileWrites, so git diff -- f.txt > $target still reports $target

@anthonykim1 Anthony Kim (anthonykim1) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more case where the -- mask hides a command. Details inline.

Authored with Copilot on behalf of anthonykim1

* TODO: Remove once upstream tree-sitter PowerShell grammer is updated.
*/
const pwshFlagEqualsRegex = /(^|\s)(-{1,2}[\w-]+)=/g;
const pwshStandaloneDoubleDashRegex = /(^|\s)--(?=\s|$)/g;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regresses a case that main handles correctly: a malformed here-string header requires confirmation on main, but is auto-approved with this change. The test at L289 covers -- in the body of a valid here-string. The header line is different: PowerShell rejects anything but whitespace after @' or @", and masking -- there turns an invalid header into one tree-sitter accepts. For this input:

Write-Output @' --
Remove-Item -Recurse -Force ./important
'@

extractAutoApprovalSubCommands returns:

// main: requires confirmation
{ subCommands: [], hasUnanalyzableSyntax: true }

// this PR: the whole input as one sub-command, auto-approved by the default `Write-Output` rule
{ subCommands: [commandLine], hasUnanalyzableSyntax: false }

PowerShell only rejects line 1. pwsh doesn't turn on bracketed paste, so sendText sends each line with Enter and line 2 runs on its own. I confirmed that in pwsh 7.5.4 on macOS. Please leave -- on a header line unmasked, for example by adding (?<!@["'][^\S\r\n]*) to the front of pwshStandaloneDoubleDashRegex, and add a test for each quote style asserting hasUnanalyzableSyntax: true. The tests in this file, including L289, still pass with that change.

Authored with Copilot on behalf of anthonykim1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, is this the last regression? I know Copilot reviewer tends to be prone to finding and reporting one-two issues at a time, and it feels pretty tedious when you have to spend few days waiting for the next round.

I just wanted to fix a very common issue with the auto-approver, but now it's been 2.5 months of the review loop, and it doesn't feel very sustainable... If Copilot has a better fix, please feel free for it to push straight to this PR - I just want the issue with the git diff -- to be fixed.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

7 participants