Skip to content

Conversation

@kwkam
Copy link
Contributor

@kwkam kwkam commented Mar 31, 2019

Reopen #7405

PR Summary

Unescape non-literal, non-glob path before calling
GetUnresolvedProviderPathFromPSPath since it only accepts literal path.

PR Context

This solve the issue where some commands depending on that method
will treat -Path as literal unintentionally. For example,

Out-File -Path './`[out`].txt' -Append

will create a new file named "`[out`].txt" instead of writing to "[out].txt".

PR Checklist

kwkam added 2 commits March 31, 2019 13:10
Unescape non-literal, non-glob path before calling
GetUnresolvedProviderPathFromPSPath since it only accepts literal path.
This solve the issue where some commands depending on that method
will treat -Path as literal unintentionally. For example,
> Out-File -Path './`[out`].txt' -Append
will create a new file named "`[out`].txt" instead of writing to "[out].txt".
@stale
Copy link

stale bot commented Apr 30, 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.

SteveL-MSFT
SteveL-MSFT previously approved these changes May 4, 2019
Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

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

LGTM

@daxian-dbw
Copy link
Member

daxian-dbw commented May 13, 2019

I think we need to think about the current behavior again.
Today, the escaped wildcard pattern is respected when it's used for searching an item with wildcard globbing. However, when creating a new item, the path is used as is (in fact, New-Item only accept literal path when -Name parameter is not used).
For example:

PS> New-Item [out].txt

    Directory: F:\tmp\modules\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/13/2019  9:07 AM              0 [out].txt

PS> Get-Item '.\`[out`].txt'

    Directory: F:\tmp\modules\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/13/2019  9:07 AM              0 [out].txt

PS> New-Item '.\`[out`].txt'

    Directory: F:\tmp\modules\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/13/2019  9:08 AM              0 `[out`].txt

Out-File follow this convention (well, partially) -- when searching for an existing file, the escaping of the wildcard pattern is respected (because it's used as a wildcard pattern), however, when no existing file is found and it's time to create a new file, -Path doesn't treat the provided path as a literal path like New-Item does.

# The provided path is resolved to "[out].txt" when searching for an existing file ("[out].txt" is an existing file)
PS> "abc" | Out-File -Path '.\`[out`].txt' -Append
PS> Get-Content '.\`[out`].txt'
abc
# Remove the existing file
PS>rm *
# When creating a new file, the path is used as is
PS:78> "abc" | Out-File -Path '.\`[out`].txt' -Append
PS:79> dir

    Directory: F:\tmp\modules\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/13/2019  9:17 AM              5 `[out`].txt

This does seem to be confusing -- Out-File would operate on files with different names depending on whether the file already exists or not ... But I think we need to think about the convention in general (e.g. should New-Item be changed too? what about Set-Content which only search for an existing file before creating a new file?) so that the behavior is not contradicting between different cmdlets.

@daxian-dbw
Copy link
Member

FYI, Set-Content and Add-Content don't work with paths that contains escaped wildcard characters. Issue opened: #9588

@daxian-dbw
Copy link
Member

Reference my comment on the related issue: #9475 (comment)
I think this is a general issue that should be well thought and fixed in a wider scope.

@TravisEz13 TravisEz13 added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jun 21, 2019
@ghost ghost added the Review - Needed The PR is being reviewed label May 27, 2020
@ghost
Copy link

ghost commented May 27, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@daxian-dbw daxian-dbw dismissed SteveL-MSFT’s stale review November 2, 2021 17:46

Need to rethink about the current behavior.

@daxian-dbw
Copy link
Member

As is stated in my comments, the wildcard pattern issue is a general issue that should be well thought and fixed in a wider scope. So I will close this PR.

@daxian-dbw daxian-dbw closed this Nov 2, 2021
@ghost ghost removed the Review - Needed The PR is being reviewed label Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants