Skip to content

Fix Resolve-Path -LiteralPath. - #2572

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
charub:2570
Nov 2, 2016
Merged

Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
charub:2570

Conversation

@charub

Copy link
Copy Markdown
Contributor

Resolving #2570.

Ignore drive paths in the check. If the input is a drive path, then "" is passed as the path to GetDriveQualifiedPath with drive info. Since, the path here starts with "", treatAsRelative is set to false
which prevents further formatting of this path. The check is only valid for PSDrives with root path equals to UNC paths or the paths in unix.

@msftclas

Hi Charu Bassi (@charub), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Charu Bassi). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

@msftclas

Hi Charu Bassi (@charub), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Charu Bassi). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

@vors

Copy link
Copy Markdown
Collaborator

Re-triggered the builds, because master was broken

@JamesWTruher

James Truher (JamesWTruher) commented Nov 1, 2016 •

Copy link
Copy Markdown
Collaborator

please add more tests for resolve-path. the ones we have here don't have any which use -literalpath. the resolve-path.tests.ps1 file is the right place for them

@JamesWTruher

Copy link
Copy Markdown
Collaborator

LGTM

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These comparison is tedious. We can achieve the same with the following:

if (path.IndexOfAny(new char[]{ '\\', '/' }, 0, 1) == 0 && path.Length > 1)
{
    treatAsRelative = false;
}

Note that since this code path may be hot, the new char[]{ '\\', '/' } probably should be declared as a static char array in StringLiterals using StringLiterals.DefaultPathSeparator and StringLiterals.AlternatePathSeparator. This is based on Performance considerations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about (path.StartsWith(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal) || path.StartsWith(StringLiterals.AlternatePathSeparatorString, StringComparison.Ordinal) && path.length>1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not path.IndexOfAny(new char[]{ '\\', '/' }, 0, 1) == 0? It's concise and efficient -- only check the first character of path essentially.

@daxian-dbw Dongbo Wang (daxian-dbw) Nov 1, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually the following is better as path may be an empty string.

if (path.Length > 1 && path.IndexOfAny(new char[]{ '\\', '/' }, 0, 1) == 0)
{
    treatAsRelative = false;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the current check is simple to understand and serves the purpose.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, add more comments explaining what you are doing here. It will be hard to understand the original intention if you look at this code again after some time. This is required by Code comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added comments to explain the change

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please fix the indentations before It.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed the indentation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the following working the same with your fix?

PS /> New-PSDrive -Name Test -Root / -PSProvider FileSystem

Name           Used (GB)     Free (GB) Provider      Root                                                                        CurrentLocation
----           ---------     --------- --------      ----                                                                        ---------------
Test                7.59         21.22 FileSystem    /

PS /> Resolve-Path -LiteralPath  Test:/

Path
----
Test:/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@daxian-dbw Dongbo Wang (daxian-dbw) Nov 1, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please reverse the order of "path.Length > 1" and (path.StartsWith() || path.StartsWith()), so that we can avoid unnecessary string comparison under some circumstance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reversed the order.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

equal to "" or "/"

One more minor thing. This comment seems not right since the code suggesting "\" or "/".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed the comment.

Resolving PowerShell#2570.
Ignore drive paths in the check. If the input is a drive path,
then "\" is passed as the path to GetDriveQualifiedPath with drive info.
Since, the path here starts with "\", treatAsRelative is set to false
which prevents further formatting of this path.
The check is only valid for PSDrives with root path equals to UNC paths or the paths
in unix.
@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 85825e2 into PowerShell:master Nov 2, 2016
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Resolving PowerShell#2570.
Ignore drive paths in the check. If the input is a drive path,
then "\" is passed as the path to GetDriveQualifiedPath with drive info.
Since, the path here starts with "\", treatAsRelative is set to false
which prevents further formatting of this path.
The check is only valid for PSDrives with root path equals to UNC paths or the paths
in unix.
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.

6 participants