Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Jan 10, 2018

PR Summary

When InitialSessionState initializes it tries to SetLocation to current working directory,
but if the directory name contains PowerShell wildcard characters, it fails and reverts
to $PSHOME.
The change affects Set-Location in that if the path exists (even if containing wildcard characters), just use it.

Fix #5752

PR Checklist

Note: Please mark anything not applicable to this PR NA.

…nt working directory,

but if the directory name contains PowerShell wildcard characters, it fails and reverts
to $PSHOME.  Fix is to suppress wildcard expansion in this case.

It "Can start in directory where name contains wildcard characters" {
Set-Location -LiteralPath $testPath.FullName
& $powershell -noprofile -c get-location | Should BeExactly $testPath.FullName
Copy link
Collaborator

Choose a reason for hiding this comment

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

$powershell don't defined in the Describe block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix. Broke when I made this test it's own Describe.

Copy link
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

The code change is surprising given the PR title. Based on the code, I would have thought that Set-Location was broken. Given that it's not, I'm guessing similar code exists elsewhere and either:
a) that code is not necessary
b) that code is subtly different than this fix

So I'd ask for a little more digging. You might also look for other ways this code is reachable - that might suggest the need for more tests.

@SteveL-MSFT SteveL-MSFT changed the title Allow startup directory for runspace to contain wildcard characters Allow startup directory for initialsessionsate to contain wildcard characters Jan 10, 2018
@SteveL-MSFT
Copy link
Member Author

@lzybkr Changed title a bit to clarify where the issue is. This internal SetLocation method doesn't differentiate between path and literalpath (it's always path). The *-Location cmdlets do end up using this same path but sets SuppressWildcardExpansion to true when -LiteralPath is used.

@lzybkr
Copy link
Contributor

lzybkr commented Jan 10, 2018

With your change - I think Set-Location -Path has a change in behavior. Consider:

mkdir aa
mkdir ba
mkdir [ab]a
Set-Location [ab]a

Before your change, this reports an error, after it works without error.

I think this is desirable - it almost seems like Set-Location never should have had the -LiteralPath parameter in the first place, but I've only thought about this for like a second.

@SteveL-MSFT SteveL-MSFT added the Breaking-Change breaking change that may affect users label Jan 10, 2018
@SteveL-MSFT
Copy link
Member Author

@lzybkr you are correct regarding the behavior change in set-location -path and agree it's probably the right thing. I'll add breaking change although I don't think anyone will be impacted.

@iSazonov
Copy link
Collaborator

If [ab]a is allowed path I'd said that it is not a breaking change, but bug fix.
Also I'd add new explicit test for Set-Location -Path [ab]a.

@SteveL-MSFT SteveL-MSFT requested a review from anmenaga as a code owner January 11, 2018 07:20
@SteveL-MSFT
Copy link
Member Author

SteveL-MSFT commented Jan 11, 2018

After adding test for Set-Location, it didn't actually do what I thought it would. Apparently the check I had wasn't working as the path was relative and thus didn't "exist". Put the change in the LocationGlobber where the path is resolved to an absolute path first so now Set-Location works as discussed. Appreciate the feedback.


if (!context.SuppressWildcardExpansion)
// if the directory contains wildcard characters, but exists, just return it
if (!context.SuppressWildcardExpansion && !Utils.NativeDirectoryExists(userPath))
Copy link
Collaborator

@iSazonov iSazonov Jan 11, 2018

Choose a reason for hiding this comment

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

Maybe the comment and logic should be "if the directory exists, just return it"? And wildcard characters should be a next check.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm leveraging falling into the else clause which is the same as if wildcard expansion is suppressed. Alternatively, I could rearrange the code so that the suppression/existing happens first which may make it a bit easier to read:

if (context.SuppressWildcardExpansion || Utils.NativeDirectoryExists(userPath))
...

Copy link
Member Author

Choose a reason for hiding this comment

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

Need to change this code to handle exceptions during check

handle exceptions during directory exists check
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

LGTM

@iSazonov
Copy link
Collaborator

@lzybkr Could you please continue the review?

Copy link
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

I sign off.

You should squash the commits and write a good commit message with an explanation of the breaking change so that appears in the git history.

@iSazonov
Copy link
Collaborator

@SteveL-MSFT Could you please edit PR header and description to address Jason request?

@SteveL-MSFT SteveL-MSFT changed the title Allow startup directory for initialsessionsate to contain wildcard characters Set-Location should use path with wildcard characters if it exists instead of globbing Jan 17, 2018
@SteveL-MSFT
Copy link
Member Author

Done

@KillyMXI
Copy link

I'm a bit late to the party, but I hope it's OK now, while 6.1.0 still in preview.

Something with implemented fix still bothers me.
When started playing with v6.1.0-preview.2, I realized that Get-ChildItem is also affected by this change.
And everything else using wildcard expansion now has added conditional branch with try-catch (what kind of performance overhead it might have, btw?)

PS C:\ps_test> Get-ChildItem


    Directory: C:\ps_test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24.05.2018      2:03                [ab]a
d-----       24.05.2018      2:03                aa
d-----       24.05.2018      2:03                ba


PS C:\ps_test> Get-ChildItem [ab]a


    Directory: C:\ps_test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24.05.2018      2:03                [ab]a


PS C:\ps_test> Get-ChildItem [abc]a


    Directory: C:\ps_test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       24.05.2018      2:03                aa
d-----       24.05.2018      2:03                ba

While this might even make it work as expected more times than before, it has an increased level of surprise. It might make issues caused by the use of -Path instead of -LiteralPath to show up less frequently but will make them harder to trace when they finally show up.
Using wildcards by default and literal path as an option was a major design mistake, imo. This patch makes a step back, but in a way that makes it less predictable. Is it OK to have more "magic"?

I also wonder why it was made in this way instead of just calling Set-Location with -LiteralPath when initializing a session? Working folder is by definition a literal path. It would've kept behavior changes to minimum.

@SteveL-MSFT SteveL-MSFT deleted the pwsh-cwd-wildcards branch May 24, 2018 01:19
@SteveL-MSFT
Copy link
Member Author

@KillyMXI you bring up some good points, can you open a new issue to discuss this?

@KillyMXI
Copy link

Done #6927

SteveL-MSFT added a commit to SteveL-MSFT/PowerShell that referenced this pull request Jun 25, 2018
Revert "Set-Location should use path with wildcard characters if it exists instead of globbing (PowerShell#5839)"

This reverts commit 7459b54.
iSazonov pushed a commit that referenced this pull request Jun 27, 2018
Revert "Set-Location should use path with wildcard characters if it exists instead of globbing (#5839)"

This reverts commit 7459b54.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking-Change breaking change that may affect users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Powershell does not open in current directory if its name contains a left square bracket (wildcard)

4 participants