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

[SR-6527] Linux: Empty URLComponents does not generate valid URL #3774

Open
mbrandonw opened this issue Dec 4, 2017 · 5 comments
Open

[SR-6527] Linux: Empty URLComponents does not generate valid URL #3774

mbrandonw opened this issue Dec 4, 2017 · 5 comments

Comments

@mbrandonw
Copy link

Previous ID SR-6527
Radar None
Original Reporter @mbrandonw
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux
Assignee None
Priority Medium

md5: 22fd63926de4684cc07b905a8d223d78

Issue Description:

This code:

    let baseUrl = URL(string: "https://www.pointfree.co")
    let urlComponents = URLComponents()
    let url = urlComponents.url(relativeTo: baseUrl)

generates a valid `URL` on Darwin, but generates `nil` on Linux.

I am using Xcode 9.2 beta 9C34b, Swift 4.0.3

@pushkarnk
Copy link
Member

This is interesting.

`urlComponents.url(relativeTo: baseUrl)` uses `urlComponents.string` which evaluates to an empty string (on both Linux and Darwin) and passes it to `URL(string:relativeTo)` which is expected to return nil if `string` is empty.

So, the `URLComponents.url(relativeTo)` implementation is a composition of `urlComponents.string` and `URL(string:relativeTo)` both of which are behaving as expected. May be it needs an alternate implementation?

@pushkarnk
Copy link
Member

https://developer.apple.com/documentation/foundation/urlcomponents/1780048-url does not discuss the case of URLComponents value where nothing is defined. May be we should simply add a special case to make the behaviour same as Darwin.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from swiftlang/swift May 5, 2022
@ShikiSuen
Copy link

I wonder whether there's anyone still looking at this issue.
This brings platform inconsistencies between macOS and other OSes.

@ShikiSuen
Copy link

ShikiSuen commented Feb 27, 2025

For anyone who wants platform consistencies with this or similar API, I suggest using this one instead:

      #if canImport(Darwin)
      let filename = url.pathComponents.last
      #else
      let filename = url.pathComponents?.last
      #endif

The last is always nullable whatever the OS is.

@ShikiSuen
Copy link

ShikiSuen commented Feb 27, 2025

Update: I found that the urls(forResourcesWithExtension returns NSURL in lieu of URL on non-Darwin platforms.

NSURL on non-Darwin platforms have API inconsitencies comparing to URL, hence manual conversion required:

let urls = nsURLs.compactMap { URL(string: $0.absoluteString) }

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

No branches or pull requests

4 participants