Skip to content

Data(contentsOf:) crash due to missing FoundationNetworking when using static linking #4644

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

Open
MaxDesiatov opened this issue Oct 12, 2022 · 0 comments

Comments

@MaxDesiatov
Copy link
Contributor

This code having no references to FoundationNetworking causes a crash when compiled with swiftc -static-stdlib:

import Foundation

let str = try String(data: Data(contentsOf: URL(string: "https://httpbin.org/get")!), encoding: .utf8)!
print(str)

Data(contentsOf:) weakly (indirectly) links FoundationNetworking, which makes the linker exclude that library from static linking as seemingly unused if that's the only reference to networking in your code using Foundation on Linux.

This snippet works perfectly fine when compiled with swiftc -static-stdlib:

import Foundation
import FoundationNetworking

_ = URLSession.shared

let str = try String(data: Data(contentsOf: URL(string: "https://httpbin.org/get")!), encoding: .utf8)!
print(str)

Having a direct reference to symbols in FoundationNetworking (such as URLSession) is a recommended workaround as of Swift 5.7 and 5.6, and is likely to work in Swift 5.5 as well.

We're investigating if other solutions can be implemented in a future version of Swift.

Originally posted by @MaxDesiatov in swiftlang/swift#61372 (comment)

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

1 participant