SwiftPM fails to build swift-testing targeting Wasm

Building tests fails for WASI because SPM is generating a (empty) test discovery file with import XCTest.

Looking at the SPM code, it wasn't obvious if there is a configuration workaround, or how this should be fixed, and thus how soon it might be.

I'm using the SDK's and toolchains (e.g, from July 8) as documented in [1]. Swift-testing WASI docs in [2] say to append --build-tests, but the build fails

args=(--swift-sdk swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-08-a_wasm)
swift build "${args[@]}" --build-tests

Error message

[...]/.build/wasm32-unknown-wasi/debug/HelloPackageDiscoveredTests.derived/[...]
all-discovered-tests.swift:1:8: error: no such module 'XCTest'

HelloPackageDiscoveredTests.derived lists no tests and is likely not needed (I assume there's a separate file for the tests based on swift-testing). Same result in Linux as macOS, or when writing tests in XCTest.

Aside from SDK+wasi+swift-testing interactions, the code might still be transitioning to the lower-level build system.

I didn't see SPM bugs or PR's on point.

So:

  1. Am I using it wrong? Does it work for others?
  2. Is it expected/desired to work (or e.g., is the swift-testing WASI.md stale)?
  3. If so, is there a plan for how the code needs to change for it to work?

My goal is to ensure some base libraries remain WASI-compatible before building them out too much, so to get unblocked I could write my own test drivers (since swift test doesn't delegate now anyway), or with a clear plan I could perhaps help get this working.

TIA!

[1] https://www.swift.org/documentation/articles/wasm-getting-started.html

[2] https://github.com/swiftlang/swift-testing/blob/main/Documentation/WASI.md

Hi Wes,

This is a known issue related to the current state of Wasm support in the upstream. Here's what's happening:

Currently, there are two different SDK distributions:

  • The SwiftWasm SDK (from github.com/swiftwasm/swift)
  • The official Swift.org SDK

We're in the process of integrating all SwiftWasm work into the official Swift.org distribution.

Regarding XCTest support:

  • XCTest is already supported in the SwiftWasm SDK
  • The code has been integrated into swiftlang/swift-corelibs-xctest
  • However, the build scripts for bundling XCTest into the Swift.org SDK are still being ported (work in progress)

For immediate use: If you need to build tests targeting Wasm right now, I'd recommend using the SwiftWasm SDK from github.com/swiftwasm/swift, which already includes XCTest support.

We plan to start working on the remaining build script porting once the next snapshot is released.

3 Likes

Thank you for the quick reply and all the work!

It sounds then like tests using only swift-testing still require support for XCTest. (I had surmised that any wasm SDK might end up only supporting swift-testing instead of legacy XCTest.)

I tried the SwiftWasm SDK from the April release with released 6.1 toolchain from Xcode, but got a compiler crash; I likely should retry with swift.org toolchain and verify my config.

The work on SwiftWasm is much appreciated!

This won't work. Toolchains from Xcode are only meant for cross-compiling to Darwin platforms. When targeting non-Darwin platforms you always need a toolchain from swift.org.

1 Like

Swift Testing does not require XCTest. Swift Package Manager generates an entry point file that assumes you use both libraries because it can't reliably tell if you do or not (there may be transient/indirect dependencies it can't see, for example.)

Once swift-corelibs-xctest is fully integrated into the Wasm toolchain, this will all "just work".

1 Like

Just an update on good progress: using the 7/23 swift.org toolchain and SDK, it's possible to build tests and run on a (macOS) host.

sdk=( --swift-sdk swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-23-a_wasm )

swift build "${sdk[@]}" --build-tests

swift test  # but not "${sdk[@]}"

I couldn't run tests yet using the swift.org wasmkit runtime.

  • No {Module}PackageTests.wasm to run wasmkit directly (expected from wasmkit docs)
  • swift test "${sdk[@]}" errors:
    • traps initializing Foundation for XCTest observers
    • wasmkit complains about any swift test parameters like --swift-sdk or --disable-xctest

As mentioned up-thread, this is work in progress, i.e., not supported yet (though pilot error is also possible).

1 Like

The test runner configuration in SwiftPM seems not to be working well. For now, you can run the tests manually after --build-tests:

# XCTest
$ wasmkit run --dir . .build/debug/CheckPackageTests.xctest
# swift-testing
$ wasmkit run .build/debug/CheckPackageTests.xctest -- --testing-library swift-testing
1 Like