question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing a provider-name-prefixed path to Set-Location makes $PWD retain the prefix

See original GitHub issue

Perhaps related: #9293

Steps to reproduce

Run the following on Windows (works analogously on Unix):

Set-Location Microsoft.PowerShell.Core\FileSystem::C:\Windows
"$PWD" | Should -Be C:\Windows

Note: A real-life scenario in which this could occur is if you pipe the output from a New-Item -Type Directory call to Set-Location, e.g.:

New-Item -Type Directory $HOME -Force | Set-Location

Expected behavior

The test should pass.

Actual behavior

The test fails:

Expected strings to be the same, but they were different.
Expected length: 10
Actual length:   48
Strings differ at index 0.
Expected: 'C:\Windows'
But was:  'Microsoft....'

That is, $PWD stringified (via .Path) to 'Microsoft.PowerShell.Core\FileSystem::C:\Windows', not the expected C:\Windows, as also reflected in the prompt string afterwards (via $executionContext.SessionState.Path.CurrentLocation).

While $PWD.ProviderPath does reflect the unprefixed (but always native) fileysystem path, relying on "$PWD" or $PWD.Path to do that too is very common.

Environment data

PowerShell Core 7.0.0-preview.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
SteveL-MSFTcommented, Dec 7, 2022

The @PowerShell/wg-powershell-cmdlets discussed this and since this is not a change in behavior and has existed for a long time, we recommend Won't Fix

2reactions
mklement0commented, Sep 13, 2019

Also it is not clear why path without prefix is expected behavior.

To quote from the OP, emphasis added:

While $PWD.ProviderPath does reflect the unprefixed (but always native) fileysystem path, relying on “$PWD” or $PWD.Path to do that [show the unprefixed full path] too is very common.

I don’t think anyone expects - nor should they - that the way they specify the location determines how that location is reported.

E.g, if I do Set-Location ./tmp in order to change to, say, C:\foo\tmp, I expect both the prompt string and Get-Location to reflect C:\foo\tmp, not ./tmp.

The same applies to passing a provider-prefixed path: it is just a way to identify the target item on input, which is unrelated to the presentation on output, which should be standardized.

This is a breaking change in the same way that any bug fix is a breaking change: it corrects unexpected behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found