You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tab completion regression in ordering (does not match Linux shells, Windows explorer, or cmd) #14757
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
}
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.
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
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
microsoft-github-policy-service commented on Aug 5, 2023
Activity
mklement0 commentedon Feb 18, 2021
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).
However, even 7.0.5 didn't sort the same as
cmd.exewith 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:
(b) Try to emulate the native shells platform-specifically, which would mean:
and macOS].(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
jessey-git commentedon Oct 3, 2021
Any movement here? I don't see why, in any universe, '_' should come before '.' for file listing or tab-completion...
ckrueger1979 commentedon Nov 10, 2021
Jesse Yurkovich (@jessey-git)
It's at least consistent with the output of get-childitem.
jessey-git commentedon Nov 10, 2021
@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.
Here is what 7.2.0 believes is the way to sort for no good reason... hilariously bad:
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.theJasonHelmick commentedon Aug 2, 2023
Thank you for the issue -- The WG reviewed this issue and believes it is a change to .NET (as stated by Michael Klement (@mklement0))
PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs
Line 1633 in 7b3e544
As noted, it is not a goal of PowerShell to be consistent with other shells or file managers.
jessey-git commentedon Aug 3, 2023
For anyone in the future coming across this utter nonsense for file listing and tab completion, there's a workaround available in: #19736
mklement0 commentedon Aug 3, 2023
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:
DOTNET_SYSTEM_GLOBALIZATION_USENLSto1(ortrue), which must be done before starting a PowerShell session.The findings below are based on the test code in my previous comment (expandable "Details" section).
Get-ChildItemin 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)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 will become clear below, the platform-native behaviors all differ, along the dimensions of:
ösort alongside their base letter,o?_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?.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:
_before.); in v7.0-, sorting was based on NLS rules: (.before_)Get-ChildItem, globbing (wildcard expansion), as well as tab-completion order.Platform-native / Windows PowerShell behavior:
Note:
_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)_before.)Windows:
cmd'sdir: ordinal (.before_, but not adjacent)Get-ChildItem, globbing: ordinal (.before_, but not adjacent)Linux:
ls: modified linguistic: symbols sort together (.before_) , but after numbers and before lettersScreen 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 sortand-- Lexical sort (current culture) are just for reference and show .NET API behaviors:microsoft-github-policy-service commentedon Aug 5, 2023
This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.