Skip to content

Harden shell completion generation and suggestion shims - #2902

Open
baronfel wants to merge 21 commits into
dotnet:mainfrom
baronfel:baronfel-review-shell-completions
Open

baronfel wants to merge 21 commits into
dotnet:mainfrom
baronfel:baronfel-review-shell-completions

Conversation

@baronfel

@baronfel baronfel commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Escape completion values and descriptions for each shell, normalize record separators, and preserve dynamic candidates as data. See the rendered shell escaping specification.
  • Share shell text encoding, emit LF-only scripts, and reduce generation allocations.
  • Harden the Bash, Zsh, and PowerShell dotnet-suggest shims and document how to update installed scripts.
  • Add shell execution tests, snapshots, and BenchmarkDotNet benchmarks.
  • Document the new shared escaping, benchmark, and test-fixture abstractions with XML comments.

Behavior changes

  • [suggest] writes InsertText when available, otherwise the label; it replaces CRLF, CR, LF, and tabs in each candidate with spaces. An empty list produces no output.
  • Zsh omits option aliases its completion grammar cannot represent. PowerShell's dotnet-suggest shim quotes inserted candidates.
  • Applications must regenerate or update installed dotnet-suggest shims to receive those changes.

Validation

  • Ran the static-completions tests at each commit, the core and dotnet-suggest tests where changed, and built the benchmarks.
  • Bash and PowerShell shim tests ran on Windows; Zsh record handling was also checked in WSL. Shell execution tests requiring unavailable tools were skipped.

baronfel and others added 18 commits November 30, 2026 11:58
Add snapshot tests that pass hostile completion values through every provider. Document the trust boundary, the per-shell encoding rules, and the test strategy in ShellEscapingDesign.md.

The snapshots record the current, unsafe output, so each fix that follows shows its effect as a snapshot diff.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Quote each compgen word-list entry independently and collect results line by line so whitespace and shell metacharacters remain literal.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
Emit completion text, labels, and tooltips as escaped single-quoted PowerShell strings and normalize tooltip line endings.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
Normalize line breaks and tabs before emitting tab-separated Fish completion descriptions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
Quote static values for the second evaluation that _arguments performs, escape single quotes in values, and escape backslashes and other _arguments metacharacters in help text.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Replace CR, LF, and tab characters with spaces before static candidates reach line-oriented shell protocols. This keeps one CompletionItem from becoming multiple Bash, Fish, or Zsh candidates and applies the same behavior consistently to PowerShell.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
Escape wordToComplete before using PowerShell's -like operator so wildcard characters remain literal and unmatched brackets cannot throw during tab completion.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
The [suggest] directive writes one completion per line. Line breaks and tabs inside a completion would split it into several records, so each CRLF, CR, LF, or tab is written as one space. The output is streamed without intermediate strings, and CompletionAction shares the last-token lookup with CompletionContext. Tests cover separators and empty output.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Normalize record separators in Zsh descriptions, omit option aliases that _arguments cannot represent, and keep dynamic completion values intact.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Read suggestion output line by line, match prefixes literally, and append candidates directly instead of reparsing dynamic data through compgen -W.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 444b4475-4f2c-4742-ba4b-0988ae880fcc
Add execution tests that load each generated script in its shell, request completions, and assert that hostile candidates stay literal and cause no side effects. Each shell has its own test class with a typed fixture that resolves the shell tool once, and attributes that declare platform and tool requirements. Document the coverage requirements. The tests found duplicate Bash candidates, so Bash now removes duplicates from COMPREPLY.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
PowerShell treats U+2018, U+2019, U+201A, and U+201B as single-quote delimiters. Completion text containing them, which is common in localized descriptions, could end the quoted string and run code while the completion script loads. Double every quote-like character, and cover it in snapshot and execution tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
BenchmarkDotNet benchmarks generate completion scripts for a medium-sized CLI with each shell provider.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Bash, Fish, Zsh, and PowerShell now write script text directly to the output instead of building intermediate strings, lists, and LINQ sequences. Fish computes each command's subcommands and options once. The shared helpers skip text that needs no changes. Allocations drop by 23% to 67% per provider on the medium-sized benchmark, and the benchmark README records the results. The only snapshot change is a blank line after the Bash shebang.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Raw string script templates take the line endings of the source checkout, so scripts generated from a Windows build contained CR characters that break Bash and Zsh. Providers now convert template CRLF sequences to LF, and a test checks every provider.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Each quoting context is now an IShellEscaper that lists its special characters and writes their escaped form. ShellText.WriteEscaped owns the scan, the record-separator normalization, and the output of unchanged text, so a new context cannot forget the separators. Joined-name writing and the LF conversion also move to ShellText, and the unused NormalizeCompletionText is removed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Each provider's embedded shell commands and templates now have XML documentation that explains what the generated code does and why it is safe, and the inline fragments are named constants or methods so they can carry that documentation. The generated scripts are unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Read completion records without word-list evaluation, pass Zsh records as values, and quote PowerShell insertion text as a literal. Add regression tests for shell metacharacters and document shim refresh steps.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@baronfel baronfel added the Area-Completions Related to support for tab completion label Nov 30, 2026
@baronfel baronfel self-assigned this Nov 30, 2026
Add XML documentation that explains the shared shell text rules, benchmark setup, and shell test fixture requirements.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
baronfel and others added 2 commits November 30, 2026 16:41
Add ShellTemplateHandler so providers can write script templates as interpolated strings. String holes are encoded for the template's quoting context by default; author-controlled text must be marked with Trusted, and a second context with Encode. Escapers now own their quotes, which replaces the per-provider quoting helpers. Generated scripts are unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Encode each value separately and allow explicitly trusted per-list separators. Use list templates for Bash completion word lists.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Completions Related to support for tab completion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants