Skip to content

Missing ServiceContextModule dependency causes linker error in Xcode projects #890

@stovak

Description

@stovak

Description

AsyncHTTPClient directly references ServiceContext.current (from swift-service-context) in HTTPClientRequest+Prepared.swift, but only declares a dependency on Tracing (from swift-distributed-tracing), not on ServiceContextModule directly.

When Xcode builds SPM packages as frameworks, transitive dependencies are not automatically propagated to the linker. This causes:

Undefined symbols for architecture arm64:
  "static ServiceContextModule.ServiceContext.current.getter", referenced from:
      implicit closure #1 () -> ServiceContextModule.ServiceContext in default argument 1 of
      (extension in Tracing):Tracing.Tracer.withSpan<A>(...) in AsyncHTTPClient.o

Root Cause

In Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest+Prepared.swift:

let context = ServiceContext.current

And in the default argument of Tracer.withSpan() calls — Swift compiles default arguments into the caller, so ServiceContext.current gets compiled into AsyncHTTPClient.o, creating a direct symbol reference to ServiceContextModule.

However, Package.swift only lists:

.product(name: "Tracing", package: "swift-distributed-tracing"),

It does not include:

.product(name: "ServiceContextModule", package: "swift-service-context"),

Fix

Add swift-service-context as an explicit package dependency and ServiceContextModule as a product dependency of the AsyncHTTPClient target. This is the same class of bug as #721 (missing NIOTLS).

 dependencies: [
     .package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.3.0"),
+    .package(url: "https://github.com/apple/swift-service-context.git", from: "1.1.0"),
 ],
 .target(
     name: "AsyncHTTPClient",
     dependencies: [
         .product(name: "Tracing", package: "swift-distributed-tracing"),
+        .product(name: "ServiceContextModule", package: "swift-service-context"),
     ],
 ),

Environment

  • Xcode 26.2 (macOS 26)
  • async-http-client 1.31.0
  • swift-distributed-tracing 1.4.0
  • swift-service-context 1.3.0
  • Consumed transitively via swift-xet → swift-huggingface in an Xcode project (not a Package.swift-based project)

Workaround

Manually patch the local DerivedData checkout of async-http-client/Package.swift to add the missing dependency. This must be re-applied after every xcodebuild clean or DerivedData deletion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerskind/bugFeature doesn't work as expected.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions