Skip to content

Tab completion regression in ordering (does not match Linux shells, Windows explorer, or cmd) #14757

Closed

Description

Steps to reproduce

mkdir test
cd test

echo "" > foo.exe
echo "" > foo_bar.cmd
echo "" > foo_bar_zar.cmd
mkdir foo

.\f <tab> <tab> <tab> <tab> ...

Expected behavior

Expected tab completion order as follows -- matches pwsh 7.0.x, windows explorer, cmd, AND linux Bash
.\foo\  .\foo.exe  .\foo_bar.cmd  .\foo_bar_zar.cmd

Actual behavior

Regression in ordering
.\foo\  .\foo_bar_zar.cmd  .\foo_bar.cmd  .\foo.exe

Do the right thing and fix the regression.

Activity

  1. added
    Needs-Triage
    on Feb 11, 2021
  2. mklement0 commented on Feb 18, 2021

    @mklement0
    Contributor

    There was indeed a change in behavior between Windows PowerShell and PowerShell (Core), in support of cross-platform consistency, so I don't think it's a regression (though I cannot speak to what extent there was awareness that the behaviors diverged as a result).

    The PowerShell (Core)-internal change between 7.0.5 and 7.1 is owed to a change in .NET itself outside PowerShell's control (presumably, just moving to a new .NET version did it - I have no information on what prompted the change in .NET).

    # Reports "! . _" in 7.0.5 vs. "_ ! ." in 7.1 and later on a US-English machine.
    [string[]] $a = '!', '_', '.'; [Array]::Sort($a, [StringComparer]::CurrentCultureIgnoreCase); $a -join ' '

    However, even 7.0.5 didn't sort the same as cmd.exe with respect to non-ASCII characters:

    It is also important to note that there is no consistent behavior across the other shells and GUI file managers such as File Explorer.

    The summary is:

    • Each platform individually exhibits different behaviors in the (native-shell) terminal vs. in the GUI file manager (notably, natural sorting of trailing numbers).

    • Different Unix-like platforms behave differently (macOS vs. Linux).

    • Windows PowerShell exhibits different behavior with Get-ChildItem (ordinal sorting (by code-point value), as reported by the .NET file-enumeration APIs) vs. with tab-completion (lexical) - an unfortunate inconsistency.

    • PowerShell (Core) now uses lexical sorting consistently, on all platforms, which means case-insensitive, current-culture-sensitive sorting [Update: based on ICU rules in v7.1+]; on Unix-like platforms it must do its own sorting, because the underlying system APIs enumerate files unsorted.


    I see the following options:

    • (a) Leave everything as is, which has two benefits:

      • Simple implementation.
      • Consistent behavior across all platforms - even if it doesn't match any platform's native sorting.
    • (b) Try to emulate the native shells platform-specifically, which would mean:

      • Increased implementation complexity.
      • Ordinal sorting on Windows [Update: and macOS].
      • [Update: Modified ordinal sorting on macOS: ordinal with respect to symbols, but lexical with respect to letters. (emulating the specifics may be tricky, unless it is provided by a system call)]
      • Modified lexical sorting on Linux (emulating the specifics may be tricky, unless it is provided by a system call).
    • (c) Stick with consistent cross-platform behavior, [Update: but switch to the "majority" sorting, which would be ordinal, because 2 out of 3 platforms - Windows and macOS - use it, but in terms of actual PowerShell usage Linux is probably the more important platform. The only shared aspect of the platform-native behaviors is that . sorts before _, for instance]

    From a PowerShell (Core) perspective (alone), (b) and (c) would be breaking changes, given that a change wouldn't just affect the interactive experience (tab-completion) but also the order in which files are listed with Get-Item / Get-ChildItem.


    For anyone who wants to experiment with the sort orders, here's test code:

    Details
    & {
      Push-Location ($td = New-Item -Force -ItemType Directory (Join-Path ([io.path]::GetTempPath()) $PID))
    
      $names = 'foo', 'foo_bar', 'foo.bar', 'föo', 'foo10', 'foo2'
    
      $null = New-Item -Force $names
    
      '-- Ordinal sort'
      [Array]::Sort($names, [StringComparer]::OrdinalIgnoreCase); $names
    
      '-- Lexical sort (current culture)'
      [Array]::Sort($names, [StringComparer]::CurrentCultureIgnoreCase); $names
    
      '-- Tab-completion'
      (TabExpansion2 -inputScript './' -cursorColumn 2).CompletionMatches.CompletionText -replace '^\.[/\\]'
    
      '-- Get-ChildItem' # also applies to -Path and -Filter wildcards
      (Get-ChildItem).Name
    
      if ($env:OS -eq 'Windows_NT') {
        "-- cmd /c dir (Windows)"
        cmd /c dir /b
    
      } else {
        "-- ls ($(uname))"
        ls -1
      }
    
      Invoke-Item . # Show the folder in the platform's GUI file manager.
    
      Read-Host '-- GUI File Manager launched; press Enter to clean up and exit; to test tab-completion in the native shell, press Ctrl-C now, and enter a native shell; rerun the script without aborting later to clean up'
    
      Pop-Location; Remove-Item $td -Recurse
    }
  3. jessey-git commented on Oct 3, 2021

    @jessey-git
    Author

    Any movement here? I don't see why, in any universe, '_' should come before '.' for file listing or tab-completion...

  4. ckrueger1979 commented on Nov 10, 2021

    @ckrueger1979

    Jesse Yurkovich (@jessey-git)
    It's at least consistent with the output of get-childitem.

  5. jessey-git commented on Nov 10, 2021

    @jessey-git
    Author

    @ckruegerkpmg Not really. Get-ChildItem always used to match.

    Here's the output from 7.0.6 in a real directory and which matches tab-completion behavior as well as more of typical expectations.

    Get-ChildItem *blender*
    
        Directory: F:\source\blender-git\blender-20211022-asan\bin\Debug
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d----          10/22/2021  2:39 PM                blender.crt
    -a---           11/5/2021  4:30 PM      529417728 blender.exe
    -a---           11/5/2021  4:30 PM           2026 blender.exp
    -a---          10/22/2021  2:43 PM           3740 blender.lib
    -a---           11/5/2021  4:30 PM      286814208 blender.pdb
    -a---           3/11/2021  1:43 PM            832 blender_debug_gpu.cmd
    -a---           4/19/2020  2:48 PM            871 blender_debug_gpu_glitchworkaround.cmd
    -a---           4/19/2020  2:48 PM            821 blender_debug_log.cmd
    -a---           4/19/2020  2:48 PM            806 blender_factory_startup.cmd
    -a---           4/19/2020  2:48 PM            554 blender_oculus.cmd
    -a---           11/5/2021  4:29 PM           2040 blender_test.exp
    -a---           11/4/2021  2:59 PM           3924 blender_test.lib
    -a---          10/30/2021 12:02 AM        1103360 blender-launcher.exe
    -a---          10/30/2021 12:02 AM        1003520 blender-launcher.pdb
    

    Here is what 7.2.0 believes is the way to sort for no good reason... hilariously bad:

    Get-ChildItem *blender* 
    
        Directory: C:\Users\fxgltf\test
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d----          11/10/2021  9:28 PM                blender.crt
    -a---           4/19/2020  9:48 PM            871 blender_debug_gpu_glitchworkaround.cmd
    -a---           3/11/2021  9:43 PM            832 blender_debug_gpu.cmd
    -a---           4/19/2020  9:48 PM            821 blender_debug_log.cmd
    -a---           4/19/2020  9:48 PM            806 blender_factory_startup.cmd
    -a---           4/19/2020  9:48 PM            554 blender_oculus.cmd
    -a---           11/5/2021 11:29 PM           2040 blender_test.exp
    -a---           11/4/2021  9:59 PM           3924 blender_test.lib
    -a---          10/30/2021  7:02 AM        1103360 blender-launcher.exe
    -a---          10/30/2021  7:02 AM        1003520 blender-launcher.pdb
    -a---           11/5/2021 11:30 PM      529417728 blender.exe
    -a---           11/5/2021 11:30 PM           2026 blender.exp
    -a---          10/22/2021  9:43 PM           3740 blender.lib
    -a---           11/5/2021 11:30 PM      286814208 blender.pdb
    

    Why is _ sorted first before - or .? It is possible to be consistent across the platforms without this stupid sort order (and match much more closely everyone else). The proper order should be "-", ".", and then "_" minimally.

  6. added
    Resolution-By Design
    and removed
    Needs-Triage
    on Aug 2, 2023
  7. theJasonHelmick commented on Aug 2, 2023

    @theJasonHelmick
    Collaborator

    Thank you for the issue -- The WG reviewed this issue and believes it is a change to .NET (as stated by Michael Klement (@mklement0))

    IEnumerable<FileSystemInfo> sortedChildList = childList.OrderBy(static c => c.Name, StringComparer.CurrentCultureIgnoreCase);

    As noted, it is not a goal of PowerShell to be consistent with other shells or file managers.

  8. jessey-git commented on Aug 3, 2023

    @jessey-git
    Author

    For anyone in the future coming across this utter nonsense for file listing and tab completion, there's a workaround available in: #19736

  9. mklement0 commented on Aug 3, 2023

    @mklement0
    Contributor

    Note:

    • I'm not weighing in on whether the decision was the right one, I just want to provide some more background information and to correct some inaccuracies in my previous comment (since amended).

    • To summarize the opt-in to the old PowerShell Core behavior on Windows (based on the Windows-native NLS APIs), from .NET globalization and ICU:

      • Set the environment variable DOTNET_SYSTEM_GLOBALIZATION_USENLS to 1 (or true), which must be done before starting a PowerShell session.
      • Caveat: By configuring PowerShell with the NLS opt-in you'll fundamentally change how string sorting works in your session, along with other behaviors (the areas are affected are listed near the top of the linked help topic).
    • The findings below are based on the test code in my previous comment (expandable "Details" section).


    At least it must be consistent with get-childitem

    • Tab-completion is consistent with Get-ChildItem in PowerShell Core (using linguistic (lexical; current-culture, case-insensitive) sorting based on ICU on all platforms in v7.1+, and in v7.0- on Windows based on NLS)
    • Is inconsistent in Windows PowerShell (ordinal sorting in Get-ChildItem, linguistic NLS sorting with tab-completion)

    Summary:

    • .NET 5 switched its globalization APIs on Windows from the native NLS APIs to the cross-platform ICU APIs (as previously already used on Unix-like platforms), as described in .NET globalization and ICU

    • PowerShell 7.1+ is based on .NET 5+, and therefore implicitly started using ICU rules, which notably differ in that _ now sorts before ., which is what Incorrect sort order when using tab completion and listing items #19736 is focused on.

      • As discussed at the top, an NLS opt-in is available, but it affects all code in the session (except applications that explicitly control this behavior as part of their configuration).

    As will become clear below, the platform-native behaviors all differ, along the dimensions of:

    • Do accented characters such as ö sort alongside their base letter, o?
    • Do symbols (e.g., _ and .) sort as a group (linguistic) or purely based on their Unicode code point (ordinal), in which case . sorts before digits (e.g., 1) and letters (with case-insensitive sorting also for lowercase letters), but _ sorts after them?
    • What is the relative sort order among symbols? (. before _ vs. _ before .)

    The only thing the platform-native behaviors have in common is that . sorts before _, for instance, which PowerShell v7.1+ now deviates from.

    Behavior of PowerShell (Core), on all platforms:

    • Linguistic sorting (current culture, case-insensitive) based on ICU rules in v7.1+ (_ before .); in v7.0-, sorting was based on NLS rules: (. before _)
    • Applies to Get-ChildItem, globbing (wildcard expansion), as well as tab-completion order.

    Platform-native / Windows PowerShell behavior:

    • Note:

      • The descriptions below are in part inferred from the observed behaviors; I have not tried to dig deeper.
      • Among the native utilities, the macOS Finder (GUI file manager) is the only one that sorts _ before .; with the exception of the natural number sorting, its sort oder seems to match that of ICU (i.e. .NET 5+ / PowerShell 7.1+)
    • macOS:

      • ls: modified ordinal sorting: ordinal with respect to symbols, linguistic with respect to letters (. before _, but not adjacent)
      • GUI file manager (Finder): linguistic, modified with natural number sorting (_ before .)
    • Windows:

      • cmd's dir: ordinal (. before _, but not adjacent)
      • Windows PowerShell:
        • Get-ChildItem, globbing: ordinal (. before _, but not adjacent)
        • Tab-completion: linguistic (NLS)
      • GUI file manager (File Explorer): linguistic (NLS), modified with natural number sorting
    • Linux:

      • ls: modified linguistic: symbols sort together (. before _) , but after numbers and before letters
      • GUI file manager: n/a (there is no single file manager across Linux distros)

    Screen shots from running the test code with culture en-US (Windows) / en_US.UTF-8 (macOS) / en_US.utf8 (Linux) in effect, from left to right - the top two sections (-- Ordinal sort and -- Lexical sort (current culture) are just for reference and show .NET API behaviors:

    • PowerShell 7.3 on Windows 11
    • PowerShell 7.3 on Windows 11 with NLS opt-in
    • Windows PowerShell on Windows 11
    • PowerShell 7.3 on Linux (WSL, Ubuntu 20.04)
    • PowerShell 7.3 on macOS Ventura
    7__#$!@%!#__Pasted Graphic
  10. microsoft-github-policy-service commented on Aug 5, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as by-design and has not had any activity for 1 day. 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

    Resolution-By DesignWG-Interactive-Console

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions