Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set-Location does not work with some Win32 short paths #21509

Closed
MatejKafka opened this issue Apr 20, 2024 · 12 comments
Closed

Set-Location does not work with some Win32 short paths #21509

MatejKafka opened this issue Apr 20, 2024 · 12 comments
Labels
Resolution-External The issue is caused by external component(s).

Comments

@MatejKafka
Copy link

MatejKafka commented Apr 20, 2024

Steps to reproduce

# create a directory containing a space (without space, the code works, even with the short path)
$Dir = mkdir "a b"
# get the win32 short name of the directory
$ShortName = (New-Object -ComObject "Scripting.FileSystemObject").GetFolder($Dir).ShortName
$ShortPath = Join-Path $Dir.Parent.FullName $ShortName

& {
    $ErrorActionPreference = "Continue"

    gi $ShortPath # returns the dir object, OK
    ls $ShortPath # no output, OK
    cd $ShortPath # fails
    rm $ShortPath # fails
}

The issue may also happen with other provider cmdlets, did not check anything else for now.

Expected behavior

gi C:\AB2761~1

    Directory: C:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024-04-20    22:43                a b

ls C:\AB2761~1

cd C:\AB2761~1

Actual behavior

gi C:\AB2761~1

    Directory: C:\

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024-04-20    22:43                a b

ls C:\AB2761~1

cd C:\AB2761~1
Set-Location:
Line |
  11 |  cd $ShortPath # throws an exception
     |  ~~~~~~~~~~~~~
     | An object at the specified path C:\AB2761~1 does not exist.

Error details

Error details
Type                        : System.Management.Automation.CmdletProviderInvocationException
ProviderInvocationException :
    Type           : System.Management.Automation.ProviderInvocationException
    ProviderInfo   : Microsoft.PowerShell.Core\FileSystem
    ErrorRecord    :
        Exception             :
            Type        : System.Management.Automation.PSArgumentException
            ErrorRecord :
                Exception             :
                    Type    : System.Management.Automation.ParentContainsErrorRecordException
                    Message : An object at the specified path C:\AB2761~1 does not exist.
                    HResult : -2146233087
                CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
                FullyQualifiedErrorId : Argument
            Message     : An object at the specified path C:\AB2761~1 does not exist.
            ParamName   : path
            TargetSite  :
                Name          : NormalizeThePath
                DeclaringType : [Microsoft.PowerShell.Commands.FileSystemProvider]
                MemberType    : Method
                Module        : System.Management.Automation.dll
            Source      : System.Management.Automation
            HResult     : -2147024809
            StackTrace  :
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String basepath, Stack`1 tokenizedPathStack)
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePathHelper(String path, String basePath)
        CategoryInfo          : InvalidArgument: (:) [], PSArgumentException
        FullyQualifiedErrorId : NormalizeRelativePathHelperArgumentError
    Message        : An object at the specified path C:\AB2761~1 does not exist.
    TargetSite     :
        Name          : ThrowFirstErrorOrDoNothing
        DeclaringType : [System.Management.Automation.CmdletProviderContext]
        MemberType    : Method
        Module        : System.Management.Automation.dll
    InnerException :
        Type        : System.Management.Automation.PSArgumentException
        ErrorRecord :
            Exception             :
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : An object at the specified path C:\AB2761~1 does not exist.
                HResult : -2146233087
            CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : Argument
        Message     : An object at the specified path C:\AB2761~1 does not exist.
        ParamName   : path
        TargetSite  :
            Name          : NormalizeThePath
            DeclaringType : [Microsoft.PowerShell.Commands.FileSystemProvider]
            MemberType    : Method
            Module        : System.Management.Automation.dll
        Source      : System.Management.Automation
        HResult     : -2147024809
        StackTrace  :
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String basepath, Stack`1 tokenizedPathStack)
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePathHelper(String path, String basePath)
    Source         : System.Management.Automation
    HResult        : -2146233087
    StackTrace     :
   at System.Management.Automation.CmdletProviderContext.ThrowFirstErrorOrDoNothing(Boolean wrapExceptionInProviderException)
   at System.Management.Automation.SessionStateInternal.SetLocation(String path, CmdletProviderContext context, Boolean literalPath)
   at Microsoft.PowerShell.Commands.SetLocationCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
ProviderInfo                : Microsoft.PowerShell.Core\FileSystem
ErrorRecord                 :
    Exception             :
        Type        : System.Management.Automation.PSArgumentException
        ErrorRecord :
            Exception             :
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : An object at the specified path C:\AB2761~1 does not exist.
                HResult : -2146233087
            CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : Argument
        Message     : An object at the specified path C:\AB2761~1 does not exist.
        ParamName   : path
        TargetSite  :
            Name          : NormalizeThePath
            DeclaringType : [Microsoft.PowerShell.Commands.FileSystemProvider]
            MemberType    : Method
            Module        : System.Management.Automation.dll
        Source      : System.Management.Automation
        HResult     : -2147024809
        StackTrace  :
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String basepath, Stack`1 tokenizedPathStack)
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePathHelper(String path, String basePath)
    CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentException
    FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand
    InvocationInfo        :
        MyCommand        : Set-Location
        ScriptLineNumber : 11
        OffsetInLine     : 1
        HistoryId        : 83
        Line             : cd $ShortPath # throws an exception
        Statement        : cd $ShortPath
        PositionMessage  : At line:11 char:1
                           + cd $ShortPath # throws an exception
                           + ~~~~~~~~~~~~~
        InvocationName   : cd
        CommandOrigin    : Internal
    ScriptStackTrace      : at <ScriptBlock>, <No file>: line 11
    PipelineIterationInfo :
              0
              1
TargetSite                  :
    Name          : Invoke
    DeclaringType : [System.Management.Automation.Runspaces.PipelineBase]
    MemberType    : Method
    Module        : System.Management.Automation.dll
Message                     : An object at the specified path C:\AB2761~1 does not exist.
Data                        : System.Collections.ListDictionaryInternal
InnerException              :
    Type        : System.Management.Automation.PSArgumentException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : An object at the specified path C:\AB2761~1 does not exist.
            HResult : -2146233087
        CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : Argument
    Message     : An object at the specified path C:\AB2761~1 does not exist.
    ParamName   : path
    TargetSite  :
        Name          : NormalizeThePath
        DeclaringType : [Microsoft.PowerShell.Commands.FileSystemProvider]
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Source      : System.Management.Automation
    HResult     : -2147024809
    StackTrace  :
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeThePath(String basepath, Stack`1 tokenizedPathStack)
   at Microsoft.PowerShell.Commands.FileSystemProvider.NormalizeRelativePathHelper(String path, String basePath)
Source                      : System.Management.Automation
HResult                     : -2146233087
StackTrace                  :
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown, ExecutionOptions options)

Environment data

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

Visuals

No response

@MatejKafka MatejKafka added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 20, 2024
@MatejKafka
Copy link
Author

Root cause is probably the same as #17359.

@SteveL-MSFT
Copy link
Member

SteveL-MSFT commented Apr 22, 2024

cmd.exe also doesn't find the short name and explorer.exe also can't find it. Doesn't seem like a PS specific issue here. Using the .NET API directly to get the DirectoryInfo and then calling GetFiles() also can't find the path.

@SteveL-MSFT SteveL-MSFT added Resolution-External The issue is caused by external component(s). and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Apr 22, 2024
@batzen
Copy link

batzen commented Apr 22, 2024

It's the exact same issue as in #17359.

And i can repro this on my machine.
PS fails, cmd and explorer work.

@SteveL-MSFT
Copy link
Member

@batzen this one is NOT the same issue as producing a private that removes the length check doesn't fix this issue

@MatejKafka
Copy link
Author

cmd.exe also doesn't find the short name and explorer.exe also can't find it. Doesn't seem like a PS specific issue here. Using the .NET API directly to get the DirectoryInfo and then calling GetFiles() also can't find the path.

But both setting Environment.CurrentDirectory and calling the raw Win32 SetCurrentDirectory function works correctly, and actually changes the process working directory. What does PowerShell do differently from .NET?

@MatejKafka
Copy link
Author

MatejKafka commented Apr 22, 2024

Also, at least on my system, explorer.exe understands the path and expands it correctly. Not cmd.exe though, and from a quick trace in API Monitor, it does not even seem to call SetCurrentDirectory (but maybe it's calling some internal ntdll function directly, wouldn't surprise me), so it probably fails early on some internal check.

@SteveL-MSFT
Copy link
Member

Unfortunately, my system (Microsoft Windows 10.0.22631) doesn't repro what you are experiencing making it not possible to find root cause or verify any change fixes your problem.

@MatejKafka
Copy link
Author

MatejKafka commented Apr 24, 2024

Do you have short paths enabled on the partition you're testing on? (https://stackoverflow.com/questions/18479322/how-to-check-programatically-that-8-3-short-path-name-is-enabled-on-system)

Stack Overflow
I know manually we can enable or disable 8.3 short path name support by setting NtfsDisable8dot3NameCreation.

But how to read this system information through code? Actually I have to disable some

@MatejKafka
Copy link
Author

Oops, apologies, I tested the reproduction code in the root of a drive and forgot to properly insert a directory separator in the path. I fixed it now, please try it again.

Copy link
Contributor

This issue has been marked as external and has not had any activity for 1 day. It has been be closed for housekeeping purposes.

Copy link
Contributor

microsoft-github-policy-service bot commented Apr 25, 2024

📣 Hey @MatejKafka, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@MatejKafka
Copy link
Author

Uh, bad bot. Still relevant, please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-External The issue is caused by external component(s).
Projects
None yet
Development

No branches or pull requests

3 participants