Fix for Set-Location failing to restore current working directory (#6749)#6774
Fix for Set-Location failing to restore current working directory (#6749)#6774daxian-dbw merged 9 commits intoPowerShell:masterfrom
Conversation
β¦rShell#5228) [Feature] Checks to see if the root of a PSDrive was given as the Path argument to a parameter that supports it. If so, returns the current context's Drive's Root.
If given path is a colon-terminated drive
|
Tests still need to be written, creating PR for tracking. |
9abc2fc to
bbcac74
Compare
5ff03a4 to
c8e5d51
Compare
| It "Should set location to new drive's current working directory when path is the colon-terminated name of a different drive" { | ||
| try | ||
| { | ||
| Set-Location 'TestDrive:\' |
There was a problem hiding this comment.
I believe we should save current location before set new one and restore in finally.
There was a problem hiding this comment.
Should I add that before the try block, or inside it?
There was a problem hiding this comment.
I meant:
try {
$oldLocation - Get-Location
Set-Location 'TestDrive:\'
...
} finally {
Set-Location $oldLocation
}|
Scratch that, I did something wrong. Rebuilt and it works! |
| // If the path is simply a colon-terminated drive, | ||
| // not a slash-terminated path to the root of a drive, | ||
| // set the path to the current working directory of that drive. | ||
|
|
There was a problem hiding this comment.
A small style point -- I'd remove the newline here so the comment doesn't float.
Having looked at some of the older parts of this codebase, my experience is that comments that aren't glued to code have a tendency to float off into limbo (so that a couple of years down the track, an old comment will sit above a wildly different block of code with nobody certain about what it's referring to).
There was a problem hiding this comment.
Thanks for the update! I'll adjust this later tonight.
There was a problem hiding this comment.
Newlines have been removed from the floating comments.
|
@SteveL-MSFT The PR is a good place to understand whether we will address CodeFactor issues and how we will do it. |
| string colonTerminatedVolume = CurrentDrive.Name + ':'; | ||
| if (CurrentDrive.VolumeSeparatedByColon && (path.Length == colonTerminatedVolume.Length)) | ||
| { | ||
| path = Path.Combine((colonTerminatedVolume + "\\"), CurrentDrive.CurrentLocation); |
There was a problem hiding this comment.
colonTerminatedVolume + "\"
Please use Path.DirectorySeparatorChar instead of "\\".
|
I think a PR doesn't need to address CodeFactor issues that are not introduced by the PR itself. |
mcbobke
left a comment
There was a problem hiding this comment.
Path.DirectorySeparatorChar is now being used.
| string colonTerminatedVolume = CurrentDrive.Name + ':'; | ||
| if (CurrentDrive.VolumeSeparatedByColon && (path.Length == colonTerminatedVolume.Length)) | ||
| { | ||
| path = Path.Combine((colonTerminatedVolume + "\\"), CurrentDrive.CurrentLocation); |
|
@mcbobke Thanks for your contribution! |
|
Thank you all! |
PR Summary
#5228 was originally fixed by #6600, however this caused a regression as noted in #6749. Using
Set-Locationto change to a different drive such asSet-Location D:will fail to restore the current working directory of that drive as a result of the changes in #6600. This PR contains the changes from #6600 as well as a new check inSessionStateLocationAPIs.csthat determines if the path given toSet-Locationis simply a drive name terminated by a colon. If so, it changes the desired path to be the new drive's current working directory.This fixes #6749 and fixes #5228.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests