Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Remove-Item does not work correctly with long paths #15466

Copy link
Copy link
@rjmholt

Description

@rjmholt
Issue body actions

From #15260 (comment).

Rename-Item and Set-Location are also affected by this issue.

Steps to reproduce

$testdrive = "$env:TEMP/test"
$longDir = 'a' * 250
$longSubDir = 'b' * 250
$fileName = "file1.txt"
$topPath = Join-Path $TestDrive $longDir
$longDirPath = Join-Path $topPath $longSubDir
$longFilePath = Join-Path $longDirPath $fileName
new-item -itemtype file -path $longFilePath -force
remove-item -Path $longFilePath

Expected behavior

file is removed successfully

Actual behavior

> remove-item -Path $longFilePath
Remove-Item: The system cannot find the path specified.
C:\Users\rjmho
> 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
    StackTrace      :
   at Microsoft.PowerShell.Commands.InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(FileSystemInfo
fileInfo) in System.Management.Automation.dll:token 0x60016f5+0x57
   at Microsoft.PowerShell.Commands.RemoveItemCommand.ProcessRecord() in
Microsoft.PowerShell.Commands.Management.dll:token 0x60004dd+0x2e2
   at System.Management.Automation.CommandProcessor.ProcessRecord() in System.Management.Automation.dll:token
0x60020f6+0x1ae
    Message         : The system cannot find the path specified.
    Source          : System.Management.Automation
    HResult         : -2147467259
CategoryInfo          : NotSpecified: (:) [Remove-Item], Win32Exception
FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand
InvocationInfo        :
    MyCommand        : Remove-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 4
    Line             : remove-item -Path $longFilePath
    PositionMessage  : At line:1 char:1
                       + remove-item -Path $longFilePath
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : remove-item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data


Name                           Value
----                           -----
PSVersion                      7.2.0-preview.6
PSEdition                      Core
GitCommitId                    7.2.0-preview.6
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Proposed solution

Implement long paths:

diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
index b37b4d10c..da0e4cb2d 100644
--- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs
+++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
@@ -8243,6 +8243,12 @@ namespace Microsoft.PowerShell.Commands

             WIN32_FIND_DATA data = default;
             string fullPath = Path.TrimEndingDirectorySeparator(fileInfo.FullName);
+
+            if (fullPath.Length > MAX_PATH)
+            {
+                fullPath = "\\\\?\\" + fullPath;
+            }
+
             using (var handle = FindFirstFileEx(fullPath, FINDEX_INFO_LEVELS.FindExInfoBasic, ref data, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, 0))
             {

Also ensure that the actual file exists before calling the Windows API on it so that the proper error can be thrown if not:

System.IO.DirectoryInfo di = new(providerPath);
if (di != null && InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(di))

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-FixedThe issue is fixed.The issue is fixed.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.