Skip to content

Conversation

@kwkam
Copy link
Contributor

@kwkam kwkam commented Mar 31, 2019

Reopen #7404

PR Summary

Unescape non-literal path when -Name is not set.
Fix failure with symlink/junction target which contains special character.

PR Context

This works around for New-Item treating -Path as literal path while
it can also be globbable. For example, assuming there is "[file]1"
in current directory, and tab completion suggests './`[file`]1'
for the -Path argument, but

New-Item -Path './`[file`]2'

will create a file named "`[file`]2" instead of "[file]2".

Also fixes an issue that New-Item does not handle -Target properly
when it contains escaped wildcard character. For example, there is
a directory "[dir]1" in current directory, and tab completion
suggests './`[dir`]1' for the -Target argument, but

New-Item -Type Junction -Path './`[dir`]2' -Target './`[dir`]1'

will complain that it cannot find the item.

PR Checklist

kwkam added 4 commits March 31, 2019 13:09
Unescape non-literal path when -Name is not set.
This works around for New-Item treating -Path as literal path while
it can also be globbable. For example, assuming there is "[file]1"
in current directory, and tab completion suggests './`[file`]1'
for the -Path argument, but
> New-Item -Path './`[file`]2'
will create a file named "`[file`]2" instead of "[file]2".
Co-Authored-By: kwkam <kwkam@users.noreply.github.com>
@stale
Copy link

stale bot commented May 1, 2019

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label May 1, 2019
@stale stale bot removed the Stale label May 4, 2019
$expectedTarget = Join-Path -Path $tmpDirectory -ChildPath $testlinkSrcSpName

$fileInfo = Get-ChildItem -Path $FullyQualifiedLinkSp
$fileInfo.Target | Should -Match ([regex]::Escape($expectedTarget))
Copy link
Member

Choose a reason for hiding this comment

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

Why can't we do an -BeExactly assertion here instead of a -Match?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we can.
Actually this was created using the tests in the same file as reference:

$fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFile))

$fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFolder))

Perhaps we have to review the New-Item test sometime.

@adityapatwardhan
Copy link
Member

I believe we should not change this behavior. To workaround your problem you could simply use the following:

PS D:\temp\newitem> New-Item -Name `[file`]2


    Directory: D:\temp\newitem

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/10/2019 10:32 AM              0 [file]2

@kwkam kwkam changed the title Fix New-Item -Path with wildcard char Fix New-Item -Path/-Target with wildcard char May 11, 2019
@kwkam
Copy link
Contributor Author

kwkam commented May 11, 2019

The problem is the inconsistency of -Path behaviour in the *-Item cmdlets.

# to get '[file]1'
Get-Item -Path './`[file`]1'

# to move '[file]1'
Move-Item -Path './`[file`]1' -Destination ./dir

# to copy '[file]1'
Copy-Item -Path './dir/`[file`]1' -Destination ..

# to remove '[file]1'
Remove-Item -Path './dir/`[file`]1'

All above commands behave correctly with tab completion, but when we do

# what if we try to overwrite '[file]1'?
New-Item -Path './`[file`]1' -Value 'This is [file]1'

A new file `[file`]1 is created instead, which is unexpected, and in my opinion, is a flaw in the cmdlet since -Path is supposed to handle wildcard path and escaped path.

Of course, we can use -Name to workaround the issue (because there is no -LiteralPath in New-Item), but I believe -Name is not the proper replacement, as @mklement0 commented in #6232 (comment), -Name is not supposed to be path. Anyway, even if we have the workaround, a bug needs a fix.

@TravisEz13 TravisEz13 added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Waiting on Author labels Jun 21, 2019
@ghost ghost added the Stale label Jun 21, 2019
@ghost
Copy link

ghost commented Jun 21, 2019

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 30 days. It will be closed if no further activity occurs within 10 days of this comment.

@iSazonov
Copy link
Collaborator

@adityapatwardhan Could you please update your review?

@ghost ghost removed the Stale label Jun 21, 2019
@adityapatwardhan adityapatwardhan added Review - Committee The PR/Issue needs a review from the PowerShell Committee and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Jun 24, 2019
@adityapatwardhan
Copy link
Member

Changing the behavior of Path is a breaking change and hence I would like the committee to review it.

@adityapatwardhan adityapatwardhan added the Breaking-Change breaking change that may affect users label Jun 24, 2019
@joeyaiello
Copy link
Contributor

@PowerShell/powershell-committee discussed this one.

The Issue at hand is that tab completion uses single quotes and escaped characterss in what amounts to a literal path, and you only need one or the other to work properly. We agreed not to fix it in the New-Item behavior, which would be a significant breaking change, but we should fix it in the tab completion by not adding backticks (because double quotes will break where $ is in the filename).

We'd welcome a PR to fix the tab completion, but given that we're not taking this one, closing it out.

@joeyaiello joeyaiello closed this Jul 3, 2019
@iSazonov
Copy link
Collaborator

iSazonov commented Jul 4, 2019

@kwkam Have you plans to fix the tab completion?

@iSazonov iSazonov added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Jul 4, 2019
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 Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants