Skip to content

Conversation

@MaxDesiatov
Copy link
Contributor

This trivial file when built with -static-stdlib is supposed to be statically linked with FoundationNetworking, but crashes on Ubuntu (and potentially all Linux distributions) with this error:

Foundation/NSSwiftRuntime.swift:409: Fatal error: You must link or load module FoundationNetworking to load non-file: URL content using String(contentsOf:…), Data(contentsOf:…), etc.
Trace/breakpoint trap

This was fixed in swiftlang/swift-corelibs-foundation#3135 for Swift 5.5, but regressed in Swift 5.6 and Swift 5.7. Having this test should help us prevent subsequent regressions.

This was fixed in swiftlang/swift-corelibs-foundation#3135 for Swift 5.5, but regressed in Swift 5.6 and Swift 5.7
@MaxDesiatov
Copy link
Contributor Author

@swift-ci please smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci please smoke test

@MaxDesiatov
Copy link
Contributor Author

MaxDesiatov commented Oct 11, 2022

Closing this, as it's not a regression.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant