Skip to content

WASI support: build SQLKit without SwiftNIO - #3

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

WASI support: build SQLKit without SwiftNIO#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. Embedded Swift is out of scope and stacked separately as #4 (including the SQLBindable typealias, the one part that needs real discussion), 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.
  • 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.

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.

Verification:

  • Native swift build and swift test: green, suite untouched at 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, 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 (the wasm check skips drafts).

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.

Base base/vapor-main is an exact snapshot of vapor/sql-kit@main at 11ed591 (PassiveLogic/sql-kit@main is byte-identical, so there is no fork drift). Rebasing onto the real vapor/main is a no-op.

Motivation:

SwiftNIO cannot be built for `wasm32-unknown-wasip1`: NIOPosix is built on POSIX
sockets and threads, neither of which WASI preview 1 provides. SQLKit 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:

SwiftNIO cannot be built for `wasm32-unknown-wasip1`, so SQLKit currently fails
to configure for that platform at all — the failure is in dependency resolution,
before any of the conditional sources in the previous commit get a chance to
matter.

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;
the suite exercises the `EventLoopFuture` API and is not run on WASI.

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