Skip to content

On Windows and macOS, Get-Item and Get-ChildItem report file names as specified, not their actual case #13190

Closed

Description

Note: The problem:

  • affects only files, not also directories

  • for file paths, affects all path components.

Simple demonstration (macOS and Windows):

PS> (Get-Item $PSHOME/POWERSHELL.config.json).Name
POWERSHELL.config.json

Note how POWERSHELL.config.json was reported via .Name - exactly as specified - even though the actual casing of the filename is powershell.config.json

Steps to reproduce

Windows and macOS only (platforms with case-insensitive (but case-preserving) file-systems).

# Note: The problem occurs only with *files*, not directories.
#       With files, the problem occurs in all path components.
Describe "Get-Item / Get-ChildItem: non-wildcard file-name case fidelity test" {
  BeforeAll {
    Push-Location (Get-Item testdrive:/).FullName
    $nameActual = 'AB'
    $nameCaseVariant = 'aB'
    New-Item $nameActual # create as all-uppercase

    $testCases = 
      @{ Cmdlet = 'Get-Item'; Parameter = 'Path'; Property = 'Name' },
      @{ Cmdlet = 'Get-Item'; Parameter = 'Path'; Property = 'FullName' },
      @{ Cmdlet = 'Get-Item'; Parameter = 'LiteralPath'; Property = 'Name' },
      @{ Cmdlet = 'Get-Item'; Parameter = 'LiteralPath'; Property = 'FullName' },
      @{ Cmdlet = 'Get-ChildItem'; Parameter = 'Path'; Property = 'Name' },
      @{ Cmdlet = 'Get-ChildItem'; Parameter = 'Path'; Property = 'FullName' },
      @{ Cmdlet = 'Get-ChildItem'; Parameter = 'LiteralPath'; Property = 'Name' },
      @{ Cmdlet = 'Get-ChildItem'; Parameter = 'LiteralPath'; Property = 'FullName' }
  }
    
  It "<Cmdlet>: .<Property> should report '$nameActual' as the file name when literal case variant '$nameCaseVariant' is passed to <Parameter>" -Skip:$IsLinux -TestCase $testCases {
    param($Cmdlet, $Parameter, $Property) 
    $htArgs = @{ $Parameter = $nameCaseVariant }
    Split-Path -Leaf (& $Cmdlet @htArgs).$Property | Should -BeExactly $nameActual
  }
  AfterAll {
    Pop-Location
  }
}

Expected behavior

The tests should succeed.

Actual behavior

All tests fail.

Environment data

PowerShell Core 7.1.0-preview.5

Activity

  1. added
    Issue-Question
    on Jul 16, 2020
  2. vexx32 commented on Jul 16, 2020

    @vexx32
    Collaborator

    Michael Klement (@mklement0) does directly calling a .NET API to retrieve the name report it with the correct casing?

  3. mklement0 commented on Jul 16, 2020

    @mklement0
    ContributorAuthor

    Good point, Rain Sallow (/u/ta11ow) (@vexx32) - indeed, the underlying .NET Core API - as well as .NET Framework - does the same:

    PS> [System.IO.FileInfo]::new("$pshome/POWERSHELL.config.json").Name
    POWERSHELL.config.json  # !! name as specified, not the actual casing

    However, it does so for directories too - whereas PowerShell (Core only!) exhibits the desired behavior:

    # .NET
    PSonWin> [System.IO.DirectoryInfo]::new('c:\wINdows').Name
    wINdows  # !! name as specified, not the actual casing
    
    # PowerShell Core only (WinPS exhibits the behavior above).
    PSonWin> (Get-Item C:\wINdows).Name
    Windows  # OK, true casing

    So the question is:

    • Is reporting the true names for directories just a happy implementation accident, or is it by design?

    • If the latter, do we want to provide the same behavior for files too?

  4. vexx32 commented on Jul 16, 2020

    @vexx32
    Collaborator

    I'd imagine since we're interested in supporting Unix that we'd want casing to be accurate, aye.

    I'm surprised .NET Core hasn't got that ironed out yet, it'll make things very complicated for Unix developers wanting to use those APIs 😬

  5. mklement0 commented on Jul 16, 2020

    @mklement0
    ContributorAuthor

    Well, it's not strictly a functional problem, because it only applies to platforms with case-insensitive file systems, notably macOS and Windows - on Linux, with its case-sensitive file system you have to supply the case-accurate representation to begin with, otherwise you won't find the file / directory.

    Still, I imagine that users consistently expect to get the true casing of file-system items when they call Get-Item or Get-ChildItem - after all, they get objects back that describe the item's own properties, which should not depend on the particular path form you chose to locate that item.

    I'm surprised that no one (to my knowledge, based on searching through the issues) has complained in the .NET Core repo about this.

  6. iSazonov commented on Jul 20, 2020

    @iSazonov
    Collaborator

    I'd speculate that it is because performance reasons - re-combine all path is very expensive. See #9250 as sample.

  7. added
    Resolution-By Design
    on Jul 20, 2020
  8. mklement0 commented on Jul 20, 2020

    @mklement0
    ContributorAuthor

    So there's no single system call on all platforms with case-insensitive file-systems that would give you the case-exact form of a path (e.g., returning C:\Windows\System32\APHostRes.dll for c:\windows\system32\aphostres.dll)?

    Given that we currently do it - but for directories only - how do we do it?

  9. iSazonov commented on Jul 20, 2020

    @iSazonov
    Collaborator

    there's no single system call

    Yes, only way to get an original value is to read from system.

    how do we do it?

    See #9250. GetCorrectCasedPath() explicitly does this for directories.

  10. mklement0 commented on Jul 20, 2020

    @mklement0
    ContributorAuthor

    Thanks for the link, Ilya (@iSazonov).

    I think it's more important for us to make Get-Item and Get-ChildItem exhibit consistent behavior in this respect than to worry about performance.

    Performance is more likely to be a concern when enumerating file-system items, but here we're talking about targeting a given file (pattern), and wanting to know its true name.

    Note that there are more inconsistencies:

    # Get-ChildItem with -Filter: Exact case of the *full path*
    PS> gci c:\windows\system32 -filter aphost*.dll | % FullName
    C:\Windows\System32\APHostClient.dll
    C:\Windows\System32\APHostRes.dll
    C:\Windows\System32\APHostService.dll
    
    # Get-ChildItem with -Path: Exact case of the *file name only*
    PS> gci c:\windows\system32\aphost*.dll | % FullName
    C:\windows\system32\APHostClient.dll
    C:\windows\system32\APHostRes.dll
    C:\windows\system32\APHostService.dll

    Again: If I use Get-Item and Get-ChildItem, I expect to get information objects that reflect the item's true name and path, not whatever case variation I happen to have used to identify the item.

  11. iSazonov commented on Jul 20, 2020

    @iSazonov
    Collaborator

    Personally I do not like strongly #9250 and GetCorrectCasedPath(). I think if a system is case-insensitive we should follow this - accept a path as user typed and expose an enumerated path in case as it saved in the system. It is less expensive and more predictable. cmd.exe does so:

    dir c:\windows
     Volume in drive C has no label.
     Volume Serial Number is 8861-77AF
    
     Directory of c:\windows
    

    but PowerShell does extra work:

    dir c:\windows
    
        Directory: C:\Windows
  12. mklement0 commented on Jul 20, 2020

    @mklement0
    ContributorAuthor

    I think if a system is case-insensitive we should follow

    We already do honor this for referring to paths and would continue to do so, but since these file systems are also case-preserving, we should honor that too, by reporting the actual name/path when explicitly requesting information about an item.

    Yes, cmd.exe and .NET act differently - but we can do better:

    The extra work that PowerShell already does for directories is helpful - let's do the work for files too.

    Again: The incidental form of a path I use to refer to an item of interest (case variations, relative vs. absolute path) should not change how its innate properties are being reported.

  13. iSazonov commented on Jul 20, 2020

    @iSazonov
    Collaborator

    The extra work that PowerShell already does for directories is helpful

    Helpful?
    If I type "c:\WinDowS" it is my strong intention to work exactly with "c:\WinDowS". And it works! Why should the system change this to "C:\Windows" for me?
    It seems we even have a case(-s) about this. Like why expanded:

    Get-Item temp:\
    
        Directory: C:\Users\1\AppData\Local
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d----          20.07.2020    22:45                Temp
  14. removed
    Resolution-By Design
    on Jul 20, 2020
  15. mklement0 commented on Jul 20, 2020

    @mklement0
    Author
  16. 26 remaining items

  17. iSazonov commented on Aug 19, 2020

    @iSazonov
    Collaborator

    Proposed API in .Net Runtime dotnet/runtime#14321

    For Windows we could use more fast a workaround

    /cc Steve Lee (@SteveL-MSFT) for tracking .Net issue

    Retrieves the final path for the specified file.
  18. added
    WG-Engine-Providers
    Waiting - DotNetCore
    on Aug 19, 2020
  19. removed their assignment
    on Aug 19, 2020
  20. mklement0 commented on Aug 19, 2020

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov), thanks for the link.

    As for the workaround: GetFinalPathNameByHandle() resolves symlinks to their ultimate targets, which we don't want to do by default; asking for the properties of a symlink with Get-Item should return the symlink's name in .Name; ditto with parent paths that have symlink components (even if the target item itself is not a reparse point).

  21. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  22. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  23. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  24. added
    Resolution-No Activity
    on Nov 16, 2023
  25. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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-QuestionResolution-No ActivityWG-CmdletsWG-Engine-ProvidersWaiting - DotNetCore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions