Process.currentDirectoryURL strange behaviour

Here’s what’s going on…

The currentDirectoryURL property is a relatively recent addition. Historically, NSTask (and hence Process) only supported currentDirectoryPath.

The new currentDirectoryURL property is a wrapper around the old currentDirectoryPath property. Internally the setter converts the URL to a path and the getter does the reverse.

The conversion process in the setter uses url.standardizedURL.path.

The standardizedURL property is documented to return A copy of the URL with any instances of ".." or "." removed from its path. The tricky part here is that it looks just at the URL’s path. If the URL is relative, it ignores the path in the base URL.

In your case the URL is relative:

print(directoryURL)
// prints: ../usr/ -- file:///Users/

and thus standardized returns unhelpful results:

print(directoryURL.standardized)
usr/ -- file:///Users/

You can avoid the problem with a judicious application of absoluteURL.

`process.currentDirectoryURL = directoryURL.absoluteURL

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple