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

fix: separate PosixShellType.Sh case with POSIX-safe single-quote escaping#334988
Viraj Mishra (VirajMishra1) wants to merge 2 commits into
microsoft:mainfrom
VirajMishra1:fix-terminal-sh-quoting

Conversation

@VirajMishra1

Copy link
Copy Markdown

What does this implement/fix?

POSIX sh does not support ANSI-C $'...' quoting. The existing code grouped PosixShellType.Sh with Bash in a fall-through case, so a path containing a single quote produced $'...\\'...' — valid in Bash but a syntax error in /bin/sh.

Fix: Give PosixShellType.Sh its own case using the POSIX-portable close-quote/escaped-quote/reopen-quote idiom: replace each ' with '\'').

Also tightens collapseTildePath to check startsWith(normalizedUserHome + '/') (or equality) instead of includes, so /home/user2 is not incorrectly collapsed when home is /home/user.

Regression tests added for both fixes.

AI usage disclosure

I used AI assistance for:

  • Research and understanding

…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.
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, '\\\'')}'`,
- 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
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.

4 participants