Repository navigation
Fix Set-Location failure with special characters when switching drives - #26510
Yoshifumi (yotsuda) wants to merge 5 commits into
Conversation
…cters when switching drives When switching drives using drive-letter-only syntax (e.g., `E:`), PowerShell attempts to restore the current location on that drive. However, if the path contains special characters like brackets, wildcard expansion incorrectly interprets them, causing Set-Location to fail with "Cannot find path" error. This fix suppresses wildcard expansion when restoring paths from drive current locations, treating them as literal paths to preserve special characters. Changes: - Added pathRestoredFromDrive flag to track when path is restored from drive - Set SuppressWildcardExpansion when path comes from drive's CurrentLocation - Added test case for drive switching with special characters in path Fixes PowerShell#25282
- Combine nested if statements into single condition with && operator - Remove issue reference comment from test (context name is sufficient)
50c9504 to
fd9d16d
Compare
- Remove Windows-specific subst tests - Remove $script: prefixes (Pester handles scope automatically) - Replace physical drive tests with PSDrive-based tests - Add regression tests for cd ., cd .., cd drive:., cd drive:.. - Tests now run on all platforms (Windows/Linux/macOS) Addresses feedback from @iSazonov: - Line 135: Remove $script: prefixes - Line 151: Support non-Windows platforms via PSDrive
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where Set-Location fails when switching drives to paths containing special characters like brackets. The issue occurred because PowerShell was treating restored paths as wildcard patterns instead of literal paths, causing bracket characters to be interpreted as wildcard character classes.
Key changes:
- Added
pathRestoredFromDriveflag to track when paths are restored from a drive'sCurrentLocation - Suppresses wildcard expansion for restored paths by setting
context.SuppressWildcardExpansion = true - Added comprehensive test coverage for various drive-switching scenarios with special characters
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…cation.Tests.ps1 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…cation.Tests.ps1 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Yoshifumi (@yotsuda) After looking original issue I see WG conclusion was to fix embedded functions like |
|
Ilya (@iSazonov) Thanks for the review and clarification! |

PR Summary
Fix
Set-Locationfailure when switching drives to restore paths containing special characters like brackets.PR Context
Fixes #25282
When switching drives using drive-letter-only syntax (e.g.,
E:), PowerShell attempts to restore the previous location on that drive fromCurrentLocation. However, if the path contains special characters like brackets, wildcard expansion incorrectly interprets them as wildcard patterns, causingSet-Locationto fail with "Cannot find path" error.Before this fix:
After this fix:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerDescription
Root Cause
When switching drives with syntax like
E:, PowerShell restores the drive's current location by combining the drive letter with the path stored inCurrentDrive.CurrentLocation. This restored path is then processed through the normal path resolution logic, which performs wildcard expansion on special characters.For paths containing brackets (e.g.,
E:\Test [Folder]), the brackets are interpreted as wildcard character classes, causing path resolution to fail.Solution
This PR treats paths restored from drive's
CurrentLocationas literal paths by suppressing wildcard expansion, similar to how-LiteralPathparameter works.Implementation:
pathRestoredFromDriveflag to track when a path is restored from a drive'sCurrentLocationcontext.SuppressWildcardExpansion = truewhenpathRestoredFromDriveis trueFiles Changed
src/System.Management.Automation/engine/SessionStateLocationAPIs.cs- Added path restoration tracking and wildcard suppressiontest/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1- Added comprehensive test coverageTesting
Test Coverage
Added 10 comprehensive test cases covering:
Basic navigation with escaped brackets
Physical drive ↔ C: drive switching (3 tests)
PSDrive ↔ C: drive switching (3 tests)
Same-drive navigation (3 tests)
T:when already on T:)C:when already on C:)TestPS:when already on TestPS:)Test Environment
substcommand to create temporary physical drives (1-character names: T, U, V, etc.)New-PSDriveto create drives with multi-character names (e.g., "TestPS")[]as the primary problematic charactersManual Testing
Automated Tests
All 10 tests pass successfully:
Additional Notes
-LiteralPathusageCurrentLocationare affected