Skip to content

Resolve issues breaking Swift Wasm builds for sql-kit - #190

Merged
gwynne merged 6 commits into
vapor:mainfrom
PassiveLogic:feat/swift-wasm-support
Jul 28, 2025
Merged

Resolve issues breaking Swift Wasm builds for sql-kit#190
gwynne merged 6 commits into
vapor:mainfrom
PassiveLogic:feat/swift-wasm-support

Conversation

@scottmarchant

@scottmarchant scottmarchant commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

These changes are now available in 3.33.1

Summary

This PR adds support for compiling sql-kit to wasm using the Swift SDK for WebAssembly.

This PR is part of a larger effort by a company called PassiveLogic to enable broad support for Swift WebAssembly.

Details

  • Removed unused NIO dependency in Package.swift. This repo compiles fine without that, but having it breaks wasm compilation.
  • Refined scope of exports to be NIOCore instead of the broader NIO.

Notes

  • Compiling to wasm requires specific versions of swift-nio. Because swift-nio still doesn't have wasm targets in it's CI, the wasm build breaks often. That issue will be resolved soon. Regardless, we don't want to bump the minimum nio requirement in the manifest, because that would be a breaking change for many using this repo. It is fine to leave the minimum nio requirement as-is. Developers trying to compile for wasm will almost certainly be aware of the issues compiling nio, and will soon have many wasm-compatible versions of nio to select.

Testing done

  • Verified unit tests still pass with these changes
  • Verified swift build completes without errors
  • Verified no new compiler warnings are added with these changes
  • Verified swift build --swift-sdk wasm32-unknown-wasi completes without errors, when using a compatible nio version.
  • Verified swift build --swift-sdk wasm32-unknown-wasip1-threads completes without errors, when using a compatible nio version.
  • Verified a third-party executable can build this library as part of a larger wasm executable using the command swift package --swift-sdk wasm32-unknown-wasip1-threads js --use-cdn
  • Verified these changes compile in CI, using yet-to-be merged additions to vapor's CI. See See https://github.com/PassiveLogic/sql-kit/actions/runs/15886122859/job/44798410586?pr=1

Impact Risk

There is a chance the changes to the manifest and exports could be a breaking change somewhere, if downstream dependencies relied on those pieces for their one and only intrinsic inclusion of the entire NIO dependency beyond NIOCore.

There are solutions to narrow the scope to just wasm compilation for those changes, if that is an issue. But I chose to leave them this way since it is a little cleaner solution.

… a newer version of swift-nio that supports wasm, and needed to narrow scope from the broad NIO dependency to the true dependency of just NIOCore.
Those compiling for wasm will need to find their own version of nio that compiles to wasm. In the near future, most latest versions of nio will compile to wasm, so this shouldn't be an issue.
@scottmarchant
scottmarchant marked this pull request as ready for review June 25, 2025 02:43
@scottmarchant
scottmarchant requested a review from gwynne as a code owner June 25, 2025 02:43
Comment thread Package.swift
Comment thread Sources/SQLKit/Exports.swift
Comment thread Package.swift Outdated
…atform. Otherwise, leave exports and dependencies unchanged to minimize any disruption to downstream dependencies.
…is a release containing the latest wasm compilation fixes.
Comment thread .github/workflows/test.yml
Comment thread Sources/SQLKit/Exports.swift
…dependencies. Latest testing indicates this won't be breaking to scope down to just NIOCore.
@gwynne gwynne added the semver-patch Internal changes only label Jul 28, 2025
@gwynne gwynne changed the title fix: Resolves issues breaking Swift Wasm builds for sql-kit. Resolve issues breaking Swift Wasm builds for sql-kit Jul 28, 2025
@gwynne
gwynne merged commit 0169d06 into vapor:main Jul 28, 2025
19 checks passed
@scottmarchant

scottmarchant commented Jul 29, 2025

Copy link
Copy Markdown
Contributor Author

Note, if anyone encounters issues with this change due to reliance on the sql-kit export of NIO, then after consuming this change, you may need to add the following import if previously relying on the implicit import.

import NIOCore

scottmarchant added a commit to PassiveLogic/sql-kit that referenced this pull request Jul 30, 2026
Motivation:

Since vapor#190 and swift-nio's WASI support, SQLKit itself already builds
for `wasm32-unknown-wasip1` with NIOCore linked. The drivers beneath it do not:
NIOPosix needs the POSIX sockets and threads WASI preview 1 lacks, so a driver on
that platform has no SwiftNIO at all, and a SQLKit that kept its `EventLoopFuture`
surface there would impose requirements no driver could implement. Gating NIOCore
out keeps `canImport(NIOCore)` uniformly false across the stack on WASI, so one
condition selects the async-only configuration everywhere.

Modifications:

Gate the NIOCore product on `.when(platforms: nonWASIPlatforms)`. Target
dependency conditions are evaluated per platform, so on WASI the product is
simply not linked and the `canImport(NIOCore)` gates select the `async` API.

`.when(platforms:)` can only include, never exclude, so excluding one platform
means enumerating the others; the list is the set SPM 6.1 knows about, noted as
such so it is not extended without also raising the manifest's tools version.

The test target's NIOCore and NIOEmbedded dependencies are gated the same way, as
are the suite's imports of them. The suite exercises the `EventLoopFuture` API and
is not run on WASI, and `swift build` evaluates
`--explicit-target-dependency-import-check` for every target in the graph,
including the test target it does not build.

Result:

On every other platform the resolved dependency set is byte-identical to before.
On WASI the build graph contains no SwiftNIO module: not NIOPosix, not NIOCore,
not NIOConcurrencyHelpers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-patch Internal changes only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants