Skip to content

fix: separate PosixShellType.Sh case with POSIX-safe single-quote escaping - #334988

Open
Viraj Mishra (VirajMishra1) wants to merge 2 commits into
microsoft:mainfrom
VirajMishra1:fix-terminal-sh-quoting
Open

Viraj Mishra (VirajMishra1) wants to merge 2 commits into
microsoft:mainfrom
VirajMishra1:fix-terminal-sh-quoting

Conversation

@VirajMishra1

@VirajMishra1 Viraj Mishra (VirajMishra1) commented Sep 7, 2026 •

Copy link
Copy Markdown

What does this implement/fix?

POSIX sh does not support ANSI-C $'...' quoting. In escapeNonWindowsPath, PosixShellType.Sh was grouped with Bash and Zsh, and unknown shells used the same $'...' form for paths that contain both a single and a double quote. In /bin/sh (dash, for example) that is a syntax error.

PosixShellType.Sh now has its own case, and the default case uses the same quoting. Both use the POSIX close/escape/reopen idiom, so each ' becomes '\''. Bash, Zsh and Git Bash keep $'...' for the both-quotes case.

This also changes collapseTildePath to check startsWith(normalizedUserHome + '/') (or equality) instead of includes, so /home/user2 is not collapsed when home is /home/user, and /tmp/backup/home/user/file is not collapsed either.

Tests

  • Separate sh test covering a single quote, a double quote and both.
  • Updated Bash, Zsh, Git Bash and default-shell expectations.
  • Two collapseTildePath cases for the substring and shared-prefix collisions.

I could not run the full suite locally. I checked every escapeNonWindowsPath expectation in the test file against the real function output (25 of 25 match).

AI usage disclosure

I used AI assistance for:

  • Research and understanding

Copilot AI balanced review requested due to automatic review settings September 7, 2026 22:57
@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/platform/terminal/common/terminalEnvironment.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.

🟡 Changes recommended

Critical test expectation failures and missing quote-combination coverage must be addressed.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Separates POSIX sh escaping and tightens home-path collapsing.

Changes:

  • Adds POSIX-safe single-quote escaping.
  • Prevents false tilde-path collapses.
  • Adds path-boundary regression tests.
File summaries
File Review
src/vs/platform/terminal/test/common/terminalEnvironment.test.ts Adds tilde-collapse tests, but shell-escaping expectations and bothQuotes coverage require updates.
src/vs/platform/terminal/common/terminalEnvironment.ts Updates shell escaping and home-path matching; current changes conflict with existing Bash, Zsh, Git Bash, and sh assertions.
Review details

Suppressed comments (1)

src/vs/platform/terminal/common/terminalEnvironment.ts:69

  • The default branch now returns '/foo/bar'\''baz', but terminalEnvironment.test.ts:97 still asserts the previous '/foo/bar\'baz' output. This leaves the unit suite failing; update the default-shell expectation to the new POSIX form.
				bothQuotes: (path) => `'${path.replace(/'/g, `'\\''`)}'`,
				singleQuotes: (path) => `'${path.replace(/'/g, `'\\''`)}'`,
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +35 to +36
bothQuotes: (path) => `'${path.replace(/'/g, `'\\''`)}'`,
singleQuotes: (path) => `'${path.replace(/'/g, `'\\''`)}'`,
escapeConfig = {
bothQuotes: (path) => `$'${path.replace(/'/g, '\\\'')}'`,
singleQuotes: (path) => `'${path.replace(/'/g, '\\\'')}'`,
singleQuotes: (path) => `$'${path.replace(/'/g, '\\\'')}'`,
…aping

POSIX sh does not support ANSI-C $'...' quoting. When a path contains
a single quote, the existing code fell through to the Bash case and
emitted $'...\\'...' which sh rejects.

Fix: give PosixShellType.Sh its own case using close-quote/escaped-quote/
reopen-quote: path.replace(/'/g, "'\\''"). Also tighten collapseTildePath
to check startsWith(home + separator) so /home/user2 is not collapsed when
home is /home/user.

Add regression tests for both cases.
- Bash/Zsh/GitBash now use ANSI-C $'...' quoting for single-quote paths
- PosixShellType.Sh and unknown shells use POSIX close/escape/reopen
- Split bash/sh/zsh test into two suites (different output formats)
- Add Sh bothQuotes coverage for paths with both single and double quotes
- Update default-shell test to expect POSIX form

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

Development

Successfully merging this pull request may close these issues.

5 participants