Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I am trying to script to recursively search file systems, and 7.2 preview 9 aborts the Get-ChildItem command with file paths>MAX_PATH (260) chars. This issue does not happen with prior versions.
Example folder seen in Win 10 that will cause this:
"C:\ProgramData\Microsoft\Windows\Containers\BaseImages\61be50b1-e59b-45c7-9460-020d3f6bc848\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\Diagnostics\Simple"
- Found initially when searching for files using: gci -Path C:\ -Recurse -Force -ErrorAction SilentlyContinue
- The "Simple" child folder exceeds MAX_PATH (260) chars and is where it bombs out
- It seems to ignore -ErrorAction SilentlyContinue
To rule out permission issues, I created a separate test folder structure on my D: drive with the same length and had the same results with it:
B:\Batch>mkdir "\\?\D:\DrogramTest\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\Diagnostics\Simple"
B:\Batch>dir "\\?\D:\DrogramTest\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\Diagnostics\Simple"
Volume in drive \\?\D: is Toshiba5TB
Volume Serial Number is 625A-B5E5
Directory of \\?\D:\DrogramTest\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\Diagnostics\Simple
2021.09.19 18:23 <DIR> .
2021.09.19 18:23 <DIR> ..
0 File(s) 0 bytes
Simply then attempting gci D:\DrogramTest -Recurse -Force will result in it aborting with the error.
Expected behavior
Behavior With 7.2.0 Preview 8 (and prior versions)
PowerShell 7.2.0-preview.8
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS B:\PSH\PowerShell-7.2.0-preview.8-win-x64> Get-ChildItem -Path "C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\" -Recurse -ErrorAction SilentlyContinue -Force
Directory:
C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program
Files
(x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2021.05.22 09:54 Diagnostics
Directory:
C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program
Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics
\1.0.1\Diagnostics
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2021.05.22 09:54 Simple
PS B:\PSH\PowerShell-7.2.0-preview.8-win-x64>Actual behavior
Behavior With 7.2.0 Preview 9
PowerShell 7.2.0-preview.9
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS B:\PSH\PowerShell-7.2.0-preview.9-win-x64> Get-ChildItem -Path "C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1\" -Recurse -ErrorAction SilentlyContinue -Force
Directory:
C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program
Files
(x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics\1.0.1
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2021.05.22 09:54 Diagnostics
Directory:
C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program
Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics
\1.0.1\Diagnostics
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2021.05.22 09:54
Get-ChildItem: The system cannot find the path specified.
PS B:\PSH\PowerShell-7.2.0-preview.9-win-x64>Error details
PS B:\> Get-Error
Exception :
Type : System.ComponentModel.Win32Exception
NativeErrorCode : 3
ErrorCode : -2147467259
TargetSite :
Name : IsReparsePointLikeSymlink
DeclaringType : Microsoft.PowerShell.Commands.InternalSymbolicLinkLinkCodeMethods
MemberType : Method
Module : System.Management.Automation.dll
Message : The system cannot find the path specified.
Source : System.Management.Automation
HResult : -2147467259
StackTrace :
at Microsoft.PowerShell.Commands.InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(FileSystemInfo
fileInfo) in System.Management.Automation.dll:token 0x60016fa+0x6d
at Microsoft.PowerShell.Commands.FileSystemProvider.Dir(DirectoryInfo directory, Boolean recurse, UInt32 depth,
Boolean nameOnly, ReturnContainers returnContainers, InodeTracker tracker) in System.Management.Automation.dll:token
0x6001657+0x31b
at Microsoft.PowerShell.Commands.FileSystemProvider.Dir(DirectoryInfo directory, Boolean recurse, UInt32 depth,
Boolean nameOnly, ReturnContainers returnContainers, InodeTracker tracker) in System.Management.Automation.dll:token
0x6001657+0x34f
at Microsoft.PowerShell.Commands.FileSystemProvider.GetPathItems(String path, Boolean recurse, UInt32 depth,
Boolean nameOnly, ReturnContainers returnContainers) in System.Management.Automation.dll:token 0x6001656+0x5a
at Microsoft.PowerShell.Commands.FileSystemProvider.GetChildItems(String path, Boolean recurse, UInt32 depth) in
System.Management.Automation.dll:token 0x6001653+0x0
at System.Management.Automation.SessionStateInternal.GetChildItems(CmdletProvider providerInstance, String path,
Boolean recurse, UInt32 depth, CmdletProviderContext context) in System.Management.Automation.dll:token 0x6003a8e+0x7
CategoryInfo : NotSpecified: (:) [Get-ChildItem], Win32Exception
FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo :
MyCommand : Get-ChildItem
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 1
Line : Get-ChildItem -Path
"C:\ProgramData\Microsoft\Windows\Containers\BaseImages\283a08b6-ccbf-4c2d-a79e-cab35a294e78\BaseLayer\Files\Program
Files (x86)\WindowsPowerShell\Modules\Microsoft.PowerShell.Operation.Validation\1.0.1\Test\Modules\Example2.Diagnostics
\1.0.1\" -Recurse -ErrorAction SilentlyContinue -Force
PositionMessage : At line:1 char:1
+ Get-ChildItem -Path "C:\ProgramData\Microsoft\Windows\Containers\Base …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ChildItem
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1Environment data
Name Value
---- -----
PSVersion 7.2.0-preview.9
PSEdition Core
GitCommitId 7.2.0-preview.9
OS Microsoft Windows 10.0.21390
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response
Activity
iSazonov commentedon Sep 20, 2021
Please load latest daily build from https://github.com/PowerShell/PowerShell and check again.
[-]Get-ChildItem -Recurse -Force Aborts With File Paths Longer Than MAX_PATH And Ignores ErrorAction Options / Introduced With PowerShell 7.2.0-preview.9[/-][+]Get-ChildItem -Recurse -Force Aborts With File Paths When Directory Structure Has At Least One Level Exactly Equal To MAX_PATH For Length - And Ignores ErrorAction Options / Introduced With PowerShell 7.2.0-preview.9[/+]BouwenMA commentedon Sep 25, 2021
I tested with the latest daily as suggested. The results were the same when using the commands I showed above for recreating the condition.
With more testing today, I found that this only occurs if the length of one of the directories is exactly equal to MAX_PATH. Anything less or greater does not result in this issue. It just so happens that the C:\ProgramData structure I first found this with, as well as the example folder I created above, had a path where one of the child folders came to exactly 260 chars. I updated the title today of this issue to better indicate the problem.
I created a quick script that can be used to show this in action, and allow easily running from different PS versions called "ShowPSH_Bug.ps1" with the contents listed below:
"ShowPSH_Bug.ps1"
I ran that script above in several PS versions, and pasted results below to show how the latest preview 9 and newer seem to have introduced an issue when this scenario occurs.
Output for PowerShell 7.2 preview 8 (I also validated PowerShell 7.1.4 matched this)
Output for PowerShell 7.2 preview 9 and newer including latest daily
Above it can be seen that preview 9 and newer abort when attempting to enumerate the "Fold04Bad" folder structure for the simple gci recurse operation.
Notice also in the fist part of the gci output above, where it lists the first set of folders before it starts recursing, that the listing has the placeholder with date/time stamp for the "Fold04Bad" folder, but the name is missing from the list... as seen in this excerpt here:
When attempting to pipe the gci to ForEach-Object, the failure completely aborts seemingly ignoring the specified ErrorAction, making the error more of a terminating error vs non-terminating.
PS 5.1 Behavior
It should be noted that PS 5.1 does not enumerate the folders longer than MAX_PATH. However, as long as I use the ErrorAction shown, it at least continues with the operation and does not abort it:
To confirm I removed the -ErrorAction and ran again in 5.1.. Where I saw an error displayed for each folder as it tried to enumerate... However it still didn't completely abort the operation trying either as seen with preview 9 and newer of 7.2.
iSazonov commentedon Sep 27, 2021
Mathias (@BouwenMA) Thanks for your investigations!
The fix is easy. In:
PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs
Lines 8270 to 8273 in 3ed7571
we should replace the condition with
if (fullPath.Length >= MAX_PATH).