Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix for Set-Location failing to restore current working directory (#6749)#6774

Merged
daxian-dbw merged 9 commits intoPowerShell:masterfrom
mcbobke:workon5228and6749
May 17, 2018
Merged

Fix for Set-Location failing to restore current working directory (#6749)#6774
daxian-dbw merged 9 commits intoPowerShell:masterfrom
mcbobke:workon5228and6749

Conversation

@mcbobke
Copy link

@mcbobke mcbobke commented Apr 30, 2018

PR Summary

#5228 was originally fixed by #6600, however this caused a regression as noted in #6749. Using Set-Location to change to a different drive such as Set-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 in SessionStateLocationAPIs.cs that determines if the path given to Set-Location is 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

Matthew Bobke and others added 3 commits April 24, 2018 16:27
…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
@mcbobke
Copy link
Author

mcbobke commented Apr 30, 2018

Tests still need to be written, creating PR for tracking.

@mcbobke mcbobke force-pushed the workon5228and6749 branch 2 times, most recently from 9abc2fc to bbcac74 Compare April 30, 2018 08:57
@mcbobke mcbobke force-pushed the workon5228and6749 branch 2 times, most recently from 5ff03a4 to c8e5d51 Compare April 30, 2018 16:56
@daxian-dbw daxian-dbw requested a review from rjmholt April 30, 2018 22:41
@mcbobke mcbobke changed the title WIP: Fix for Set-Location failing to restore current working directory (#6749) Fix for Set-Location failing to restore current working directory (#6749) May 1, 2018
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:\'
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we should save current location before set new one and restore in finally.

Copy link
Author

Choose a reason for hiding this comment

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

Should I add that before the try block, or inside it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant:

try {
    $oldLocation - Get-Location
    Set-Location 'TestDrive:\'
    ...
} finally {
    Set-Location $oldLocation
}

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, will add that now.

@rjmholt
Copy link
Collaborator

rjmholt commented May 11, 2018

I just cloned and built this branch, and I think I'm still getting a repro of #5228.

Scratch that, I did something wrong. Rebuilt and it works!

Copy link
Collaborator

@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

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

Looks good to me πŸ˜„

// 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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

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).

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the update! I'll adjust this later tonight.

Copy link
Author

Choose a reason for hiding this comment

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

Newlines have been removed from the floating comments.

@iSazonov
Copy link
Collaborator

@SteveL-MSFT The PR is a good place to understand whether we will address CodeFactor issues and how we will do it.
The factor does not evaluate changes but only the entire files. Current policy don't allow PR unrelated style changes.

string colonTerminatedVolume = CurrentDrive.Name + ':';
if (CurrentDrive.VolumeSeparatedByColon && (path.Length == colonTerminatedVolume.Length))
{
path = Path.Combine((colonTerminatedVolume + "\\"), CurrentDrive.CurrentLocation);
Copy link
Member

@daxian-dbw daxian-dbw May 14, 2018

Choose a reason for hiding this comment

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

colonTerminatedVolume + "\"

Please use Path.DirectorySeparatorChar instead of "\\".

Copy link
Author

Choose a reason for hiding this comment

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

This has been changed.

@daxian-dbw
Copy link
Member

I think a PR doesn't need to address CodeFactor issues that are not introduced by the PR itself.
We need to review the CodeFactor rules, not all of them make sense to keep, then we can see if it's possible to bulk update the code base.

Copy link
Author

@mcbobke mcbobke left a comment

Choose a reason for hiding this comment

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

Path.DirectorySeparatorChar is now being used.

string colonTerminatedVolume = CurrentDrive.Name + ':';
if (CurrentDrive.VolumeSeparatedByColon && (path.Length == colonTerminatedVolume.Length))
{
path = Path.Combine((colonTerminatedVolume + "\\"), CurrentDrive.CurrentLocation);
Copy link
Author

Choose a reason for hiding this comment

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

This has been changed.

@daxian-dbw daxian-dbw merged commit 4e3db1b into PowerShell:master May 17, 2018
@iSazonov
Copy link
Collaborator

@mcbobke Thanks for your contribution!

@mcbobke
Copy link
Author

mcbobke commented May 17, 2018

Thank you all!

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.

cd-ing to a new drive and back does not restore path New-Item ignore's Path param if Name param is specified

4 participants