Getting a File Path From a Swift URL
Seth Willits (2024, bug):
There’s a deprecation warning in Xcode for
URL.paththat reads:WARNING:
pathwill be deprecated in a future version of macOS: Usepath(percentEncoded:)insteadThe problem is something like this:
Bundle.main.url(forAuxiliaryExecutable: "tool")!.path… which returns the expected file path (ex:
/Applications/MyApp.app/Contents/tool)would be quickly rewritten as suggested to:
Bundle.main.url(forAuxiliaryExecutable: "tool")!.path(percentEncoded: false)… which simply returns tool, because the URL returned is relative to a base URL.
[…]
I’ve always found it not-completely-obvious to newcomers how to get a standard file path string from a file URL and this only makes it more confusing.
There are at least three issues here:
url.pathreturns an absolute path, buturl.path(percentEncoded: false)returned a relative path as of his writing. I tested this on macOS 26, and now it returns an absolute path. The documentation now says, “This function will resolve against the baseURL.” It seems the behavior of the method changed without it being documented when, so be careful if you’re deploying to a mix of OS versions.percentEncodedhas a default value oftrue, whereaspathdid not return an encoded string. In other words,url.path()andurl.pathare completely different. So this is another case where Apple updated the API but flipped the default—a good way to introduce bugs.It’s not obvious how to get the full file system path out of a
URL. WithNSURL,url.pathwas natural and did the right thing. Now, theURLequivalent is deprecated.The natural replacement,
url.path(), is wrong. So is Xcode’s recommendation ofurl.(percentEncoded: false).url.absoluteURL.path(percentEncoded: false)is correct but verbose.url.absoluteURL.standardizedFileURL.path(percentEncoded: false)is possibly better but even more verbose.FilePath(url)?.stringuses a newer API but gives you an optional, even though for a file URL it should never fail.This seems like such basic functionality, but there’s no API that’s the obvious right choice, concise, and ergonomic.
Previously:
- The Switch From File Paths to URLs
- Swift URL Improvements at WWDC 2022
- Proposed Foundation URL Improvements
- URL Path Retrieval Cheat Sheet
- NSURL Path Handling
4 Comments RSS · Twitter · Mastodon
"It seems the behavior of the method changed without it being documented when, so be careful if you’re deploying to a mix of OS versions."
I don't understand how anyone at Apple lets something like this happen. This is incredibly irresponsible.
I used to work on a product with a public API that had about three orders of magnitude fewer users than Apple, and we spent so fucking much time making sure we didn't break anything and documented things properly. We had test coverage out the wazoo to keep the APIs stable. And Apple is just yoloing it.
I genuinely don't get it.
> Foundation is a mess since the Swift rewrite.
What are particularly bad messes you've noticed? Or is it the general cascade issue where a bugfix changes behavior that creates bugs elsewhere?
A lot of PRs I look at generally claim improvements, but rarely because of Swift, it's usually "rewriting it with [edge case in mind] is better".
> So this is another case where Apple updated the API but flipped the default
There's so many and there's no header annotation like API_AVAILABLE to keep track. NSView.clipsToBounds comes to mind.
I've debated if I should hard-code explicit parameters to whatever the documentation claims versus letting Apple's implicit defaults / SDK rebuilds control behavior.
@Plume I've noticed smaller companies care so much more than big corps in all aspects (getting details right and update responsiveness namely).