Skip to content

Get-ChildItem -Path $null returns current directory #17793

Closed

Description

Prerequisites

Steps to reproduce

Get-ChildItem -Path $null

Expected behavior

Some kind of error similar to other *Item CmdLets when Path is $null

Remove-Item -Path $null
Remove-Item: Cannot bind argument to parameter 'Path' because it is null.

Looking at the source this is not unexpected, but it can have some unexpected consequences.

Consider the following:
I have two folders setup like this:

C:\temp\saveeverythinghere> Get-ChildItem

    Directory: C:\temp\saveeverythinghere

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2022-07-28    15:08              0 SaveEverythingHere1.txt
-a---          2022-07-28    15:08              0 SaveEverythingHere2.txt
-a---          2022-07-28    15:08              0 SaveEverythingHere3.txt

C:\temp\saveeverythinghere> Get-ChildItem ..\deleteme\

    Directory: C:\temp\deleteme

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2022-07-28    15:08              0 DeleteMe1.txt
-a---          2022-07-28    15:08              0 DeleteMe2.txt
-a---          2022-07-28    15:08              0 DeleteMe3.txt

Now run this code:

$ScanExtensions = '.ps3'
$PathToDeleteFilesFrom = 'C:\temp\deleteme'

# This whill return 0 files
$ScanFiles = Get-ChildItem $PathToDeleteFilesFrom -File -Recurse | Where-Object -Property Extension -in $ScanExtensions

# This will now remove _all_ files in $PWD
Get-ChildItem $ScanFiles.FullName | Remove-Item

(In this very hypothetical scenario we can solve it using Set-StrictMode -Version 7.2 to get $ScanFiles.FullName to throw an error, but I still think it's unexpected.)

Actual behavior

Get-ChildItem -Path '.'

Environment data

PSVersion                      7.2.5
PSEdition                      Core
GitCommitId                    7.2.5
OS                             Microsoft Windows 10.0.22622

Visuals

image

Activity

  1. added
    Needs-Triage
    on Jul 28, 2022
  2. added
    WG-Engine-Providers
    Area-FileSystem-Provider
    on Jul 28, 2022
  3. jhoneill commented on Jul 28, 2022

    @jhoneill

    Unlike remove-item (rm / del) and copy-item (cp / copy) The Get-item and Get-ChildItem commands (dir / ls) work without any parameters so inside the command path being null/empty selects the current directory.

    This does not, of itself, prevent the Path parameter from rejecting null/empty input. However there will be many things out there which look like this

    function myfn  {
        param (
            #omit for current directory
            [String]$MyPathParmeter
        )
    
        # blah blah blah 
        Get-ChildItem $MyPathParameter
    # blah blah blah
    }

    By not rejecting null/empty values Get-ChildItem saves whoever writes that script from having to either default $MyPathParmeter to $pwd, or change how Get-ChildItem is called depending on whether the parameter is passed. I would expect many scripts to rely on this behaviour.
    Of course if the line is Get-ChildItem $MyPathParameter | remove-item the way of working we have requires the script author to check that the value isn't null or empty.

  4. fflaten commented on Jul 28, 2022

    @fflaten
    Contributor

    It's a breaking change. ls $nonExistingVar in bash and dir "" (not entirely the same) in cmd behaves the same.

  5. MartinGC94 commented on Jul 28, 2022

    @MartinGC94
    Contributor

    James O'Neill (@jhoneill) I'm sure someone has done it intentionally but I doubt it's common.
    Most built-in Get commands don't allow you to provide a null value to the "target" parameter, Get-Disk, Get-Process, Get-Service, Get-NetAdapter, Get-Vm all fail if you try to use null. I honestly can't think of any command besides Get-ChildItem that has this odd behavior.
    When basically every other command forces the user to validate, or set a default value in their function I would expect most users to develop a habit of doing it like this.
    IMO it's more likely that someone is negatively impacted by this due to a variable typo or whatever than someone intentionally wanting/expecting it to default to $pwd when given a null value.

  6. bjompen commented on Jul 28, 2022

    @bjompen
    Author

    It's a breaking change. ls $nonExistingVar in bash and dir "" (not entirely the same) in cmd behaves the same.

    I am very much aware of this as it changes a default behaviour. In fact, I do not really expect this behaviour to change or be fixed, but I am very curious as to why this decision was made.

    The Get-item and Get-ChildItem commands (dir / ls) work without any parameters so inside the command path being null/empty selects the current directory.

    James O'Neill (@jhoneill) Get-Item not only does not have the same behaviour.
    It also makes a difference between $null and [string]::Empty, something I think Get-ChildItem also could do (Assuming there is a valid reason to list folder contents in a folder with no name for example.)
    image

  7. jhoneill commented on Jul 28, 2022

    @jhoneill

    James O'Neill (@jhoneill) Get-Item not only does not have the same behaviour. It also makes a difference between $null

    Sorry brain fade on my part. Of course only Get-Childitem works with no path.

    Get-ChildItem could have been designed to reject null or empty, but it wasn't. And over the last 15 years a decent number of scripts have been written which depend on the present behaviour. So this is breaking change where the benefit is small. (MartinGC94 we can argue how big the number is but the example I gave of one natural way to write it - i.e. not splatting ps bound params will pass an empty string if the parameter is not specified, and so will be break if empty is rejected, so I think its a breaking change which would have quite a wide effect)

  8. added
    Breaking-Change
    on Aug 8, 2022
  9. vexx32 commented on Aug 9, 2022

    @vexx32
    Collaborator

    WG-Engine discussed this today.

    The original intent here (if there was any) is not fully remembered, but folks echoed the points raised before here around similarity to ls or dir commands that behave similarly.

    Given the change here would be a breaking one, and that it is generally a bit of an edge case, we aren't confident we should go ahead and fix this. It's also mostly a non-issue; it's fairly trivial to do one's own validation in a script before calling Get-ChildItem in most cases, either with an if statement or a Where-Object in a pipeline to discard or otherwise ignore null values.

    We understand the discomfort for inconsistency and occasional confusion this behaviour can sometimes cause, but our consensus is that the benefit from the change here isn't compelling enough for this breaking change to be acceptable.

  10. added
    Resolution-Won't Fix
    and removed
    Needs-Triage
    on Aug 9, 2022
  11. ghost closed this as completedon Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area-FileSystem-ProviderBreaking-ChangeResolution-Won't FixWG-Engine-Providers

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions