-
Notifications
You must be signed in to change notification settings - Fork 159
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
URL.path(percentEncoded: false) is NOT Equivalent to URL.path property for File URLs #1011
Comments
Hi @swillits, I very much empathize with all the concerns you've listed here, and they're issues we're looking to fix. In fact, I'm in the process of writing a proposal to expose
In the future, I could imagine a set of
Of course, this is pending API review process and feedback from the community, so please let me know if you have suggestions. Along with good updated documentation, I think this would alleviate most of the issues you mentioned. Thank you for bringing this to our attention! And I'd be very happy to hear any feedback :) |
@jrflat What version are you talking about here? As @swillits says, |
@jrflat Those changes all sound good to me. |
@ole The |
@jrflat Thank you for the clarification. It would be nice to have all these details documented somewhere, i.e. a per-platform-per-version list of what APIs actually use the new Swift Foundation vs. the old Foundation (Darwin Foundation or swift-corelibs-foundation, respectively). I understand that the overall goal of Swift Foundation is for such a list to eventually become (largely) unnecessary, but I fear the transition period is going to be long, and the official communication from Apple isn't super helpful because it's too broad and optimistic IMO ("Look, there's a new unified version of Foundation and as of late 2024 it is actually the active Foundation on all platforms"). But I should probably raise this issue on the forums. Sorry for ranting at you. |
Description
Xcode suggests that
URL.path(percentEncoded: false)
should be used to replace the will-be-deprecatedURL.path
, but for relative file URLs, this will lead to bugs in existing code. (Ask me how I know; It already did in mine.)The documentation for .path notes: "This function resolves against the base URL." It also says: "New code should use .path(percentEncoded: false)"
The documentation for path(percentEncoded:) does not say anything about resolving against the base URL. Does it? Does it not? — It does not, which is the problem.
I raised this issue in the forums with no response: https://forums.swift.org/t/url-path-vs-path-percentencoded-for-file-urls/75358
Reproduction
The problem is something like this:
... which returns the expected file path (ex: "/Applications/MyApp.app/Contents/tool")
would be quickly rewritten as suggested to:
... which simply returns "tool", because the URL returned is relative to a base URL.
Widespread adoption of
path(percentEncoded: false)
in place ofpath
on file URLs will lead to bugs if this undocumented difference is not known.Expected behavior
Based on Xcode's deprecation warning, I expect
.path(percentEncoded: false)
to behave the same as.path
currently does.The fact is many users will just change
.path
into.path(percentEncoded: false)
because that's what Xcode and the documentation tell them to do, and they will probably introduce bugs.Environment
Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0
Additional information
I believe the best solution would be the addition of a new URL property specifically for getting the file path from file URLs.
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. (.path, .relativePath, .path(), .path(percentEncoded: false) ..... there are too many choices, and the behavior difference between them is not clear unless you really dig and familiarlize yourself with URLs.
For the sake of sanity where having to go from a URL to a file path string is still very common, I'd expect something along the lines of:
... which makes it very clear and obvious to anyone looking at URL and asking "how do I get the [full and complete, resolved, standardized] file path string from this URL?"
This is a common task, and yet it's not obvious. The alternative is that any time code needs to go from a URL to a file path, it needs to call
url.standardizedFileURL.path(percentEncoded: false)
to get the correct result, which is just not convenient or obvious.Or, since the FilePath type now exists, I'd even accept:
The text was updated successfully, but these errors were encountered: