Skip to content

Commit 414fed6

Browse files
Fix test runner build on WASI (#7341)
### Motivation: The test runner build was failing on WASI after the introduction of the experimental summary output. ### Modifications: This PR fixes several minor issues to pass `--build-tests` build on WASI: * Missing `WASILibc` import * The use of `flock` which is not available on WASI. * Signature incompatibility of `XCTMain` on WASI. ### Result: `swift build --build-tests --triple wasm32-unknown-wasi` will pass when compiler supports the target.
1 parent a1b4ad7 commit 414fed6

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,14 @@ final class TestEntryPointCommand: CustomLLBuildCommand, TestBuildCommand {
265265
struct Runner {
266266
static func main() {
267267
\#(testObservabilitySetup)
268+
#if os(WASI)
269+
// FIXME: On WASI, XCTest uses `Task` based waiting not to block the whole process, so
270+
// the `XCTMain` call can return the control and the process will exit by `exit(0)` later.
271+
// This is a workaround until we have WASI threads or swift-testing, which does not block threads.
272+
XCTMain(__allDiscoveredTests())
273+
#else
268274
XCTMain(__allDiscoveredTests()) as Never
275+
#endif
269276
}
270277
}
271278
"""#

Sources/Build/TestObservation.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
130130
#elseif os(Windows)
131131
@_exported import CRT
132132
@_exported import WinSDK
133+
#elseif os(WASI)
134+
@_exported import WASILibc
133135
#else
134136
@_exported import Darwin.C
135137
#endif
@@ -176,6 +178,8 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
176178
UInt32.max, UInt32.max, &overlapped) {
177179
throw ProcessLockError.unableToAquireLock(errno: Int32(GetLastError()))
178180
}
181+
#elseif os(WASI)
182+
// WASI doesn't support flock
179183
#else
180184
if fileDescriptor == nil {
181185
let fd = open(lockFile.path, O_WRONLY | O_CREAT | O_CLOEXEC, 0o666)
@@ -201,6 +205,8 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
201205
overlapped.OffsetHigh = 0
202206
overlapped.hEvent = nil
203207
UnlockFileEx(handle, 0, UInt32.max, UInt32.max, &overlapped)
208+
#elseif os(WASI)
209+
// WASI doesn't support flock
204210
#else
205211
guard let fd = fileDescriptor else { return }
206212
flock(fd, LOCK_UN)
@@ -211,6 +217,8 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
211217
#if os(Windows)
212218
guard let handle = handle else { return }
213219
CloseHandle(handle)
220+
#elseif os(WASI)
221+
// WASI doesn't support flock
214222
#else
215223
guard let fd = fileDescriptor else { return }
216224
close(fd)

0 commit comments

Comments
 (0)