Skip to content

feat: Add Wasm platform by conditionally building without swift-nio - #3

Open
scottmarchant wants to merge 2 commits into
base/vapor-mainfrom
feat/wasi-nio-free
Open

feat: Add Wasm platform by conditionally building without swift-nio#3
scottmarchant wants to merge 2 commits into
base/vapor-mainfrom
feat/wasi-nio-free

Conversation

@scottmarchant

@scottmarchant scottmarchant commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Builds SQLKit on wasm32-unknown-wasip1 without SwiftNIO. Since vapor#190 and swift-nio's WASI support, SQLKit itself already builds there with NIOCore linked; SwiftNIO is elided anyway because the drivers beneath SQLKit cannot build it at all (NIOPosix needs the POSIX sockets and threads WASI preview 1 lacks), so nothing on that platform can implement an EventLoopFuture surface, and keeping canImport(NIOCore) uniformly false across the stack means one condition selects the async-only configuration everywhere. Compiling under Embedded Swift needs a further set of gates, including a change to how the bound-parameter constraint is spelled; that work is kept out of this PR and is not being proposed yet, which keeps this diff small.

Changes:

  • The legacy EventLoopFuture surface goes behind #if canImport(NIOCore): SQLDatabase.eventLoop, the future-returning execute(sql:_:) requirement and its default bridge, SQLQueryBuilder.run(), the three SQLQueryFetcher future families, the NIOCore re-exports, and SQLBenchmarker's two deprecated bridges. The async surface, already the recommended API at every one of those call sites, is untouched and is all that remains where SwiftNIO is absent.
  • Each #if encloses the doc comment of the declaration it gates rather than sitting between the two. A #if in that position detaches the comment: the declaration still compiles, but the symbol graph reports it as undocumented and the published docs lose the entry, with no build-time diagnostic.
  • The manifest gates NIOCore (and the test target's NIOCore/NIOEmbedded) with .when(platforms: nonWASIPlatforms), the same spelled-out-list idiom as Add support for WASILibc apple/swift-nio#2671 and swift-crypto, with a comment pinning the list to the manifest's tools version. The test suite's imports of those modules gate on the same condition, because swift build evaluates --explicit-target-dependency-import-check for every target in the graph, including the test target it does not build.

Where SwiftNIO is present, nothing changes: every gate is unconditionally true, and swift package diagnose-api-breaking-changes reports no breaking changes in SQLKit or SQLKitBenchmark. swift build -Xswiftc -emit-symbol-graph emits the same symbols with the same docComment line counts as before.

What WASI loses: the EventLoopFuture half of the API. SQLDatabase.eventLoop, the future-returning execute(sql:_:) and SQLQueryBuilder.run(), the future variants of the SQLQueryFetcher first/all/run families, the NIOCore re-exports, and SQLBenchmarker's deprecated bridges. The async surface, already the recommended API, is what remains. One driver-facing nuance: the default implementation of the async execute(sql:_:) forwards to the future-returning requirement, so where SwiftNIO is absent there is no future overload to forward to and drivers implement the async requirement directly, which is the primitive they should be implementing anyway.

Verification:

  • Native swift build and swift test: green, 169 tests, including with CI's --explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable.
  • WASI (--swift-sdk swift-6.3.1-RELEASE_wasm): green with the same flags, zero SwiftNIO object files, no NIO symbols in the built objects. SQLKitBenchmark still builds for regular WASI.
  • No CI change needed: sql-kit already passes with_wasm: true to vapor/ci.

On tests: no test cases are added. The change introduces no behavior on any platform that compiled before (every gate is unconditionally true where SwiftNIO is present, and the symbol graph is byte-identical), and the configuration it does add is exercised by the wasm CI lane.

Notes for review: every gate is a capability gate (canImport), in Sources/ only, never in a manifest; there is no os(WASI) anywhere. All three packages in this stack gate the same modules on the same condition, and a package that did build NIOCore for WASI would fail loudly at compile. No SwiftPM traits, no versioned manifest, no tools-version bump, no new dependencies.

@scottmarchant
scottmarchant force-pushed the feat/wasi-nio-free branch 2 times, most recently from f008acf to 8774c42 Compare July 30, 2026 02:49
@scottmarchant scottmarchant changed the title WASI support: build SQLKit without SwiftNIO feat: Add Wasm compilation support for sql-kit Jul 30, 2026
@scottmarchant

Copy link
Copy Markdown
Collaborator Author

I'm marking this as ready for review temporarily to run CI. Please ignore for a bit. I will either move this back to draft or formally present it for review soon

@scottmarchant
scottmarchant marked this pull request as ready for review July 30, 2026 03:06
@scottmarchant scottmarchant changed the title feat: Add Wasm compilation support for sql-kit feat: Add Wasm compilation support by conditionally building without SwiftNIO Jul 30, 2026
@scottmarchant scottmarchant changed the title feat: Add Wasm compilation support by conditionally building without SwiftNIO feat: Add Wasm platform by conditionally building without swift-nio Jul 30, 2026
Motivation:

The SQL drivers beneath SQLKit cannot build SwiftNIO for `wasm32-unknown-wasip1`:
NIOPosix is built on POSIX sockets and threads, neither of which WASI preview 1
provides. SQLKit itself needs very little of SwiftNIO, only `EventLoop` and
`EventLoopFuture`, and only for the legacy half of an API whose `async` half is
already the recommended one, so it can build without it, given somewhere to put
the differences.

The goal is one implementation with a few conditional declarations, not a second
copy of the package that has to be kept in step by hand.

Modifications:

Gate the legacy `EventLoopFuture` surface with `#if canImport(NIOCore)`. Keying
on `canImport` rather than on a platform keeps the sources in step with whatever
the manifest resolves for the target being built, and makes the gate
unconditionally true wherever SwiftNIO is present.

What drops out where it is absent: the `eventLoop` property, the
`execute(sql:_:) -> EventLoopFuture<Void>` requirement and its default bridge to
`async`, the `EventLoopFuture` variants of `SQLQueryBuilder.run()` and of the
`SQLQueryFetcher` `first`/`all`/`run` families, the NIOCore re-exports, and the
deprecated `SQLBenchmarker` future bridges.

Each `#if` encloses the doc comment of the declaration it gates rather than
sitting between the two. A `#if` in that position detaches the comment: the
declaration still compiles, but the symbol graph reports it as undocumented, so
the published API docs lose the entry with no build-time diagnostic. Placed
correctly, the doc comments are byte-identical to before.

Result:

Wherever SwiftNIO is available the public API, the documentation and the symbol
graph are unchanged. `swift build --target SQLKit -Xswiftc -emit-symbol-graph`
emits the same symbols with the same `docComment` line counts as before, and
`diagnose-api-breaking-changes` reports no differences.

Where it is absent the `async` surface, already the recommended API on every one
of those calls, is unaffected and becomes the only one.
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant