Skip to content

Get-ChildItem -path '/' breaks -exclude or -include parameters #15497

Open

Description

I believe there is a bug with Get-ChildItem for pwsh v7.1.3 on Ubuntu 20.04 (WSL2) when using the -exclude and -include parameters in conjunction with the -Path '/' parameter/argument combo.

This seems primarily be limited to Get-ChildItem -Path '/' as both -LiteralPath '/' and -PSPath (Get-Item -Path '/').PSPath return the data as expected when used with either -exclude or -include.

Additionally using -exclude and -path together works for directories outside /. However, I believe there may be another bug when using -Path and -Include other places as well.

Steps to reproduce

Get-ChildItem -Path '/' -Exclude 'mnt'

# Bad:
Get-ChildItem -Path '/' -Exclude 'mnt'

# Aligns with expected behavior:
Get-ChildItem -Path '/mnt' -Exclude 'c'
Get-ChildItem -LiteralPath '/' -Exclude 'mnt'
Get-ChildItem -PSPath (Get-Item -Path '/').PSPath -Exclude 'mnt'

image

Get-ChildItem -Path '/' -Include 'mnt'

# Bad:
Get-ChildItem -Path '/' -Include 'mnt' # Produces error
Get-ChildItem -Path '/mnt' -Include 'c' # Produces no output when output is expected.

# Aligns with expected behavior:
Get-ChildItem -LiteralPath '/' -Include 'mnt'
Get-ChildItem -LiteralPath '/mnt' -Include 'c'
Get-ChildItem -PSPath (Get-Item -Path '/').PSPath -Include 'mnt'
Get-ChildItem -PSPath (Get-Item -Path '/mnt').PSPath -Include 'c'

image

I'm unsure if the behavior of -Include deserves its own bug report but would be happy to split it up if needed.

Environment data

image

StackTrace:
   at Microsoft.PowerShell.Commands.FileSystemProvider.GetChildName(String path) in /PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs:line 5683
   at System.Management.Automation.Provider.NavigationCmdletProvider.GetChildName(String path, CmdletProviderContext context) in /PowerShell/src/System.Management.Automation/namespaces/NavigationProviderBase.cs:line 174
   at System.Management.Automation.LocationGlobber.ExpandMshGlobPath(String path, Boolean allowNonexistingPaths, PSDriveInfo drive, ContainerCmdletProvider provider, CmdletProviderContext context) in /PowerShell/src/System.Management.Automation/namespaces/LocationGlobber.cs:line 2763
   at System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(String path, CmdletProviderContext context, Boolean allowNonexistingPaths, CmdletProvider& providerInstance) in /PowerShell/src/System.Management.Automation/namespaces/LocationGlobber.cs:line 516
   at System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String path, Boolean allowNonexistingPaths, CmdletProviderContext context, CmdletProvider& providerInstance) in /PowerShell/src/System.Management.Automation/namespaces/LocationGlobber.cs:line 218
   at System.Management.Automation.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(String path, Boolean allowNonexistingPaths, CmdletProviderContext context, ProviderInfo& provider, CmdletProvider& providerInstance) in /PowerShell/src/System.Management.Automation/namespaces/LocationGlobber.cs:line 779
   at System.Management.Automation.SessionStateInternal.GetChildItems(String path, Boolean recurse, UInt32 depth, CmdletProviderContext context) in /PowerShell/src/System.Management.Automation/engine/SessionStateContainer.cs:line 1393
   at System.Management.Automation.ChildItemCmdletProviderIntrinsics.Get(String path, Boolean recurse, UInt32 depth, CmdletProviderContext context) in /PowerShell/src/System.Management.Automation/engine/ChildrenCmdletProviderInterfaces.cs:line 278
   at Microsoft.PowerShell.Commands.GetChildItemCommand.ProcessRecord() in /PowerShell/src/Microsoft.PowerShell.Commands.Management/commands/management/GetChildrenCommand.cs:line 318
   at System.Management.Automation.Cmdlet.DoProcessRecord() in /PowerShell/src/System.Management.Automation/engine/cmdlet.cs:line 173
   at System.Management.Automation.CommandProcessor.ProcessRecord() in /PowerShell/src/System.Management.Automation/engine/CommandProcessor.cs:line 388

Activity

  1. added
    Needs-Triage
    on Jun 1, 2021
  2. iSazonov commented on Jun 1, 2021

    @iSazonov
    Collaborator

    I guess it is dup #3304

  3. added
    WG-Engine-Providers
    on Jun 1, 2021
  4. ninmonkey commented on Jun 1, 2021

    @ninmonkey

    Taylor Marvin (@tsmarvin) What do you get running these?

    > Resolve-Path /
    
    # and
    > set-location -LiteralPath '/'
    > Get-ChildItem -path '/'

    Maybe it's related to to windows root paths. The syntax c: refers to the current directory, not the root directory.

    Output

    current path: D:\test

    PathParam FullName
    . D:\test\image.png
    c: C:\Temp\export.tsv
    c:\ C:\a.log
    d: D:\test\image.png
    d:\ D:\stuff.md

    To reproduce

    # Ensure both drives have a non-root `cwd` set before testing
    Set-Location -Path C:\Temp\
    Set-Location -Path D:\test\
    
    'current path: '
    Set-Location cd 'D:\test\'
    Get-Item . | ForEach-Object FullName
    
    $paths = '.', 'c:', 'c:\', 'd:','d:\'
    $paths | ForEach-Object {
        $p = Get-ChildItem -File -Path $_ | Select-Object -First 1 | ForEach-Object FullName
        [pscustomobject]@{
           'PathParam' = $_
           'FullName' = $p
        }
    }
  5. tsmarvin commented on Jun 6, 2021

    @tsmarvin
    Author

    Apologies - its been a busy week and I haven't had time to get back to this until now.

    Jake Bolton (@ninmonkey) The behavior seems to be consistent between both windows and linux. The main difference is that linux returns an error for Get-ChildItem -Path '/' -exclude 'mnt' but windows does not.

    Windows:
    image

    Linux:
    image

    Ilya (@iSazonov) I'm unsure if this issue is the same as #3304 as the exclude/include behavior for this issue is inconsistent between the root directories and non-root directories but 3304 seems to be more about the implicit wildcard for include/exclude.
    Also - for this particular issue Linux now throws an error where Windows does not.

  6. added
    Area-FileSystem-Provider
    Issue-Bug
    on Nov 30, 2021
  7. mklement0 commented on Jun 14, 2023

    @mklement0
    Contributor

    Ilya (@iSazonov), it isn't a duplicate of #3304, but the workaround for the latter works here too:

    # Note the "*"
    Get-Item -Path /* -Exclude mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-ProviderIssue-BugNeeds-TriageWG-CmdletsWG-Engine-Providers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions