swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.29k stars 1.13k forks source link

URLSession not usable when compiled with Static Linux SDK #5092

Open ffried opened 1 month ago

ffried commented 1 month ago

Using the following Package.swift:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let swiftSettings: Array<SwiftSetting> = [
    .swiftLanguageMode(.v6),
    .enableUpcomingFeature("ExistentialAny"),
    .enableUpcomingFeature("InternalImportsByDefault"),
]

let package = Package(
    name: "StaticNetworking",
    platforms: [.macOS(.v15)],
    targets: [
        .executableTarget(
            name: "StaticNetworking",
            swiftSettings: swiftSettings,
            // Workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/5089
            linkerSettings: [
                .linkedLibrary("crypto", .when(platforms: [.linux])),
                .linkedLibrary("icudata", .when(platforms: [.linux])),
                .linkedLibrary("icuuc", .when(platforms: [.linux])),
                .linkedLibrary("ssl", .when(platforms: [.linux])),
                .linkedLibrary("z", .when(platforms: [.linux])),
            ]),
    ]
)

With the following Sources/main.swift:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

struct Post: Sendable, Codable {
    let id: Int
    let userId: Int
    let title: String
    let body: String
}

let request = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/posts")!)
print("Fetching data...")
let (data, response) = try await URLSession.shared.data(for: request)
print("Response: \(response)")
let posts = try JSONDecoder().decode(Array<Post>.self, from: data)
print("Decoded \(posts.count) post(s)...")

Which is then built using the Swift Static Linux SDK (swift build -c release --swift-sdk "x86_64-swift-linux-musl"), leads to the following error when run (on e.g. an Ubuntu 24.04 machine):

Fetching data...
Swift/ErrorType.swift:253: Fatal error: Error raised at top level: Error Domain=NSURLErrorDomain Code=-1 "(null)"
Current stack trace:
0    <unknown>                          0x0000000000cb54a1
1    <unknown>                          0x00000000008c09d7
2    <unknown>                          0x00000000008c07e3
3    <unknown>                          0x00000000008c0c39
4    <unknown>                          0x0000000000a7fa46
5    <unknown>                          0x0000000000adc914
6    <unknown>                          0x00000000006e600e
7    <unknown>                          0x0000000000cb9a33
8    <unknown>                          0x0000000000cba655
9    <unknown>                          0x0000000000db400b
10   <unknown>                          0x0000000000db4928
11   <unknown>                          0x0000000000dba506
Illegal instruction

Note the NSURLErrorDomain Code=-1 which is NSURLErrorUnknown. It doesn't matter which url is used, the result is always the same.

Tested using Swift 6.0.1 and Swift Static Linux SDK 0.0.1. It's also irrelevant whether the executable is built on macOS or Linux.

MaxDesiatov commented 1 month ago

cc @al45tair