Skip to content

terminal: fix collapseTildePath substring collisions and exact home collapse - #343203

Merged
Dmitriy Vasyura (dmitrivMS) merged 14 commits into
microsoft:mainfrom
Aditya-kumar-yadav:fix-collapse-tilde-path-collisions
Nov 6, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 14 commits into
microsoft:mainfrom
Aditya-kumar-yadav:fix-collapse-tilde-path-collisions

Conversation

@Aditya-kumar-yadav

Copy link
Copy Markdown
Contributor

Summary of Changes

  1. Fix substring & prefix collisions in collapseTildePath:
    collapseTildePath previously used .includes(normalizedUserHome). This caused incorrect path collapses when:

    • A sibling user directory shared a prefix (e.g. /home/user_admin/project with home /home/user collapsed to ~/admin/project).
    • A path contained the home path as an unrelated substring (e.g. /var/log/home/user/app collapsed to ~/g/home/user/app).
    • On Windows, C:\Users\user_backup\code with home C:\Users\user collapsed to ~\_backup\code.
      This change replaces .includes() with directory boundary checks (.startsWith(normalizedUserHome + '/')).
  2. Format exact home directory as ~:
    When path matches userHome (with or without a trailing separator), collapseTildePath now returns '~' instead of '~/' (or ~\).

  3. Root directory guard:
    Added a guard when userHome is '/' or \ to prevent treating the filesystem root as a user home directory.

Verification

Ran unit tests:

node test/unit/node/index.js --run src/vs/platform/terminal/test/common/terminalEnvironment.test.ts
./scripts/test.bat --grep=collapseTildePath
npm run eslint -- src/vs/platform/terminal/common/terminalEnvironment.ts src/vs/platform/terminal/test/common/terminalEnvironment.test.ts

Copilot AI balanced review requested due to automatic review settings October 21, 2026 11:08
@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

@Aditya-kumar-yadav

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Fixes incorrect ~ collapsing in collapseTildePath by enforcing directory-boundary matching and formatting the exact home directory as ~ (not ~/), with accompanying unit tests.

Changes:

  • Replace substring matching with startsWith(normalizedUserHome + '/') to avoid prefix/substring collisions.
  • Return ~ when the path is exactly the home directory (with optional trailing separator).
  • Add unit tests covering exact-home collapsing and non-collapsing for substring/prefix cases.
File Description
src/​vs/​platform/​terminal/​common/​terminalEnvironment.ts Tightens home matching logic and returns ~ for exact-home paths
src/​vs/​platform/​terminal/​test/​common/​terminalEnvironment.test.ts Adds regression tests for exact-home behavior and collision avoidance

Review effort: Lite

@Aditya-kumar-yadav

Copy link
Copy Markdown
Contributor Author

Addressed in 91347a0 by adding drive root guard and regression tests.

@meganrogge Megan Rogge (meganrogge) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix!

@meganrogge
Megan Rogge (meganrogge) enabled auto-merge (squash) November 3, 2026 19:30
roblourens
roblourens previously approved these changes Nov 3, 2026

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

The anchored comparison rejects valid Windows OSC 7 home paths with a URI-style leading slash, causing command-history entries to display the full path instead of collapsing it.

1 open issue
2 resolved since last review

Review effort: Max

@meganrogge

Copy link
Copy Markdown
Collaborator

We should address the comment Aditya Kumar Yadav (@Aditya-kumar-yadav)

TylerLeonhardt
TylerLeonhardt previously approved these changes Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please consider CCR comment

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

Both modified files contain an unrelated carriage-return-only trailing blank line that mixes line endings and needs to be removed.

2 open issues
1 resolved since last review

Review effort: Max

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

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

Malformed line endings in terminalEnvironment.ts break copyright-header validation and cause the hygiene check to fail.

1 open issue
2 resolved since last review

Review effort: Max

Fix hygiene copyright-header check and eliminate stray CR line endings across terminalEnvironment and its test suite.
auto-merge was automatically disabled November 6, 2026 09:18

Head branch was pushed to by a user without write access

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.

🟢 Approval recommended

No active review issues are recorded for the terminal command-history path-shortening change.

No open issues 🎉

1 resolved since last review

Review effort: Max

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit dc999c8 into microsoft:main Nov 6, 2026
51 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 6, 2026
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.

9 participants