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

Process current directory handling might be wrong on Windows #5071

Open
jakepetroules opened this issue Aug 16, 2024 · 2 comments
Open

Process current directory handling might be wrong on Windows #5071

jakepetroules opened this issue Aug 16, 2024 · 2 comments
Assignees
Labels

Comments

@jakepetroules
Copy link
Contributor

See https://github.com/apple/swift-corelibs-foundation/blob/951f3532bdb9605ea487e919fff853f69d26fffd/Sources/Foundation/Process.swift#L664

Here we access the path property of currentDirectoryURL and then attempt to convert this to filesystem representation. That might end up having a leading slash in it -- should this directly use currentDirectoryURL.withUnsafeFileSystemRepresentation instead?

@jmschonfeld
Copy link
Contributor

The FileManager.default._fileSystemRepresentation(withPath: workingDirectory) below this line should remove any leading slash because it will eventually call down into NSString._getFileSystemRepresentation which will remove the leading slash:

https://github.com/apple/swift-corelibs-foundation/blob/951f3532bdb9605ea487e919fff853f69d26fffd/Sources/Foundation/NSPathUtilities.swift#L548-L559

Are you experiencing that it doesn't do this in practice?

@jakepetroules
Copy link
Contributor Author

I'm at least seeing that setting the working directory to the root of a drive doesn't appear to work.

import Foundation
import WinSDK

func runWithDir(_ dir: String) {
    let process = Process()
    process.executableURL = URL(fileURLWithPath: "C:\\Windows\\SysWOW64\\cmd.exe")
    process.arguments = ["/c", "echo %CD%"]
    process.currentDirectoryURL = URL(fileURLWithPath: dir)
    let pipe = Pipe()
    process.standardOutput = pipe
    try! process.run()
    let out = try! pipe.fileHandleForReading.readToEnd()!
    print(String(decoding: out, as: UTF8.self))
}

runWithDir("C:\\Windows")
runWithDir("C:\\")
runWithDir("C:\\Users")

Prints:

C:\Windows

C:\Users\jakepetroules

C:\Users

So I guess it's likely trimming the leading slash properly, but it seems like it may be incorrectly trimming trailing slashes based on the observed behavior.

On Windows, there is an important distinction between C:\ and C: -- the former is an absolute path which refers exactly to what it says, while the latter is actually a relative path which refers to "the current working directly of the specified drive", in my case, C:\Users\jakepetroules.

If I say that I want the working directory of C:\, Process must not alter that input prior to passing to CreateProcessW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants