Skip to content

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

Open
@MaxDesiatov

Description

@MaxDesiatov

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions