PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.55k stars 7.06k forks source link

Split-Path can't resolve paths with [brackets] #21566

Closed danielzgtg closed 2 weeks ago

danielzgtg commented 2 weeks ago

Prerequisites

Steps to reproduce

Split-Path -Resolve -Path can't see any []-filenamed files while other commands like Get-Item can. This only affects -Path not -LiteralPath but -LiteralPath doesn't support -Leaf, etc.

Expected behavior

bash:~$ cd /tmp
bash:/tmp$ touch 'hi [123].txt'
PS /tmp> home@daniel-desktop3:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Get-Item 'hi*'

    Directory: /tmp

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-rw-r--       home home       01/05/2024 15:39            0 hi [123].txt

PS /tmp> Split-Path -Leaf -Resolve -Path 'hi*'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi `[123`].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ``[123``].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123```].txt'
Split-Path: Cannot find path '/tmp/hi `[123`].txt' because it does not exist.
PS /tmp> home@daniel-desktop3:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Get-Item 'hi*'

    Directory: /tmp

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-rw-r--       home home       01/05/2024 15:39            0 hi [123].txt

PS /tmp> # One or more should work
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi*'
hi [123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123].txt'
hi [123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi `[123`].txt'
hi [123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ``[123``].txt'
hi [123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ```[123```].txt'
hi [123].txt
PS /tmp> Get-Error

Actual behavior

bash:~$ cd /tmp
bash:/tmp$ touch 'hi [123].txt'
PS /tmp> home@daniel-desktop3:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Get-Item 'hi*'

    Directory: /tmp

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-rw-r--       home home       01/05/2024 15:39            0 hi [123].txt

PS /tmp> Split-Path -Leaf -Resolve -Path 'hi*'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi `[123`].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ``[123``].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123```].txt'
Split-Path: Cannot find path '/tmp/hi `[123`].txt' because it does not exist.
PS /tmp> home@daniel-desktop3:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Get-Item 'hi*'

    Directory: /tmp

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
-rw-rw-r--       home home       01/05/2024 15:39            0 hi [123].txt

PS /tmp> # None work
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi*'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi `[123`].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ``[123``].txt'
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ```[123```].txt'
Split-Path: Cannot find path '/tmp/hi `[123`].txt' because it does not exist.
PS /tmp> Get-Error

Error details

PS /tmp> Get-Error     

Exception             : 
    Type                 : System.Management.Automation.ItemNotFoundException
    ErrorRecord          : 
        Exception             : 
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot find path '/tmp/hi `[123`].txt' because it does not exist.
            HResult : -2146233087
        TargetObject          : /tmp/hi `[123`].txt
        CategoryInfo          : ObjectNotFound: (/tmp/hi `[123`].txt:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : PathNotFound
    ItemName             : /tmp/hi `[123`].txt
    SessionStateCategory : Drive
    TargetSite           : 
        Name          : ExpandMshGlobPath
        DeclaringType : System.Management.Automation.LocationGlobber, System.Management.Automation, Version=7.4.1.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message              : Cannot find path '/tmp/hi `[123`].txt' because it does not exist.
    Source               : System.Management.Automation
    HResult              : -2146233087
    StackTrace           : 
   at System.Management.Automation.LocationGlobber.ExpandMshGlobPath(String path, Boolean allowNonexistingPaths, PSDriveInfo drive, ContainerCmdletProvider provider, CmdletProviderContext 
context)
   at System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(String path, CmdletProviderContext context, Boolean allowNonexistingPaths, CmdletProvider& providerInstance)
   at System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String path, Boolean allowNonexistingPaths, CmdletProviderContext context, CmdletProvider& 
providerInstance)
   at System.Management.Automation.PathIntrinsics.GetResolvedPSPathFromPSPath(String path, CmdletProviderContext context)
   at Microsoft.PowerShell.Commands.SplitPathCommand.ProcessRecord()
TargetObject          : /tmp/hi `[123`].txt
CategoryInfo          : ObjectNotFound: (/tmp/hi `[123`].txt:String) [Split-Path], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SplitPathCommand
InvocationInfo        : 
    MyCommand        : Split-Path
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 6
    Line             : Split-Path -Leaf -Resolve -Path 'hi [123```].txt'
    Statement        : Split-Path -Leaf -Resolve -Path 'hi ```[123```].txt'
    PositionMessage  : At line:1 char:1
                       + Split-Path -Leaf -Resolve -Path 'hi ```[123```].txt'
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Split-Path
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : 

PS /tmp>

Environment data

PS /tmp> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Ubuntu 23.10
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

mklement0 commented 2 weeks ago

but -LiteralPath doesn't support -Leaf, etc.

That is an unfortunate bug, which - while having been acknowledged as such - still hasn't been fixed:

mklement0 commented 2 weeks ago

That said, there's still a separate bug here:

With a file literally named hi [123].txt present in the current directory, both the following commands should output hi [123].txt, but currently don't:

Split-Path -Leaf -Resolve -Path h*

Split-Path -Leaf -Resolve -Path ([WildcardPattern]::Escape('hi [123].txt'))

Note that Split-Path -Leaf -Resolve -Path h* does find items whose names start with h but do not also contain [ and ], say, a file named hi noon.

danielzgtg commented 2 weeks ago

It seems PowerShell is incorrectly expanding the wildcard twice:

bash:/tmp$ touch 'hi 123].txt'
bash:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Split-Path -Leaf -Resolve -Path h*
hi 123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi 123].txt'
hi 123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi 123`].txt'
hi 123].txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi 123``].txt'
hi 123].txt
PS /tmp> bash:/tmp$ touch 'hi [123.txt'
bash:/tmp$ pwsh
PowerShell 7.4.1
PS /tmp> Split-Path -Leaf -Resolve -Path h*
Split-Path: The specified wildcard character pattern is not valid: hi [123.txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi [123.txt'
Split-Path: The specified wildcard character pattern is not valid: hi [123.txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi `[123.txt'
Split-Path: The specified wildcard character pattern is not valid: hi [123.txt
PS /tmp> Split-Path -Leaf -Resolve -Path 'hi ``[123.txt'
mklement0 commented 2 weeks ago

See also:

danielzgtg commented 2 weeks ago

7999

That looks like the root cause. Closing in favor of it.

microsoft-github-policy-service[bot] commented 2 weeks ago

📣 Hey @danielzgtg, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Microsoft Forms