Skip to content

Use tree sitter for terminal auto approve command parsing #261794

Closed

Description

@Tyriar

The current logic for parsing a full command line into sub-commands is a naive string splitting algorithm using the following list:

// Derived from https://github.com/microsoft/vscode/blob/315b0949786b3807f05cb6acd13bf0029690a052/extensions/terminal-suggest/src/tokens.ts#L14-L18
// Some of these can match the same string, so the order matters.
//
// This isn't perfect, at some point it would be better off moving over to tree sitter for this
// instead of simple string matching.
const shellTypeResetChars = new Map<'sh' | 'zsh' | 'pwsh', string[]>([
['sh', sortByStringLengthDesc([
// Redirection docs (bash) https://www.gnu.org/software/bash/manual/html_node/Redirections.html
...createNumberRange(1, 9).concat('').map(n => `${n}<<<`), // Here strings
...createNumberRange(1, 9).concat('').flatMap(n => createNumberRange(1, 9).map(m => `${n}>&${m}`)), // Redirect stream to stream
...createNumberRange(1, 9).concat('').map(n => `${n}<>`), // Open file descriptor for reading and writing
...createNumberRange(1, 9).concat('&', '').map(n => `${n}>>`),
...createNumberRange(1, 9).concat('&', '').map(n => `${n}>`),
'0<', '||', '&&', '|&', '<<', '&', ';', '{', '>', '<', '|'
])],
['zsh', sortByStringLengthDesc([
// Redirection docs https://zsh.sourceforge.io/Doc/Release/Redirection.html
...createNumberRange(1, 9).concat('').map(n => `${n}<<<`), // Here strings
...createNumberRange(1, 9).concat('').flatMap(n => createNumberRange(1, 9).map(m => `${n}>&${m}`)), // Redirect stream to stream
...createNumberRange(1, 9).concat('').map(n => `${n}<>`), // Open file descriptor for reading and writing
...createNumberRange(1, 9).concat('&', '').map(n => `${n}>>`),
...createNumberRange(1, 9).concat('&', '').map(n => `${n}>`),
'<(', '||', '>|', '>!', '&&', '|&', '&', ';', '{', '<(', '<', '|'
])],
['pwsh', sortByStringLengthDesc([
// Redirection docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.5
...createNumberRange(1, 6).concat('*', '').flatMap(n => createNumberRange(1, 6).map(m => `${n}>&${m}`)), // Stream to stream redirection
...createNumberRange(1, 6).concat('*', '').map(n => `${n}>>`),
...createNumberRange(1, 6).concat('*', '').map(n => `${n}>`),
'&&', '<', '|', ';', '!', '&'
])],
]);

This should use a proper parser like tree-sitter to parse the command. This would fix cases like echo "a|b|c" being incorrectly treated as sub commands ['echo', 'a', 'b', 'c"'] for example.

Activity

  1. added this to the Backlog milestone on Aug 15, 2025
  2. added
    feature-request
    chat-terminal
    on Aug 15, 2025
  3. Tyriar commented on Aug 19, 2025

    @Tyriar
    ContributorAuthor

    Example from #262317

    jq '.[] | to_entries' lint.json and head -n 40
    
    2025-08-19 14:56:26.013 [info] autoApprove: Command line NOT auto-approved []
    2025-08-19 14:56:26.013 [info] - Command 'jq '.[]' is approved by allow list rule: jq []
    2025-08-19 14:56:26.013 [info] - Command 'to_entries' lint.json' has no matching auto approve entries []
    2025-08-19 14:56:26.013 [info] - Command 'head -n 40' is approved by allow list rule: head []
    2025-08-19 14:56:26.013 [info] - Command line 'jq '.[] | to_entries' lint.json | head -n 40' has no matching auto approve entries []
    
  4. rseymour commented on Oct 3, 2025

    @rseymour

    I've also seen this split on a | in quotes like grep "something\|otherthing" in a weird way.

  5. added
    unreleased
    insiders-released
    and removed
    unreleased
    on Oct 22, 2025
  6. locked and limited conversation to collaborators on Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

chat-terminalfeature-requestinsiders-releasedon-testplan

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions