diff --git a/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Package.swift b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Package.swift new file mode 100644 index 00000000..84130401 --- /dev/null +++ b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Package.swift @@ -0,0 +1,17 @@ +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "Check", + dependencies: [.package(name: "JavaScriptKit", path: "../../../../../")], + targets: [ + .testTarget( + name: "CheckTests", + dependencies: [ + "JavaScriptKit", + .product(name: "JavaScriptEventLoopTestSupport", package: "JavaScriptKit"), + ], + path: "Tests" + ) + ] +) diff --git a/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Tests/CheckTests.swift b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Tests/CheckTests.swift new file mode 100644 index 00000000..9ed73b7c --- /dev/null +++ b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting/Tests/CheckTests.swift @@ -0,0 +1,5 @@ +import Testing + +@Test func never() async throws { + let _: Void = await withUnsafeContinuation { _ in } +} diff --git a/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Package.swift b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Package.swift new file mode 100644 index 00000000..84130401 --- /dev/null +++ b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Package.swift @@ -0,0 +1,17 @@ +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "Check", + dependencies: [.package(name: "JavaScriptKit", path: "../../../../../")], + targets: [ + .testTarget( + name: "CheckTests", + dependencies: [ + "JavaScriptKit", + .product(name: "JavaScriptEventLoopTestSupport", package: "JavaScriptKit"), + ], + path: "Tests" + ) + ] +) diff --git a/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Tests/CheckTests.swift b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Tests/CheckTests.swift new file mode 100644 index 00000000..324df370 --- /dev/null +++ b/Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest/Tests/CheckTests.swift @@ -0,0 +1,7 @@ +import XCTest + +final class CheckTests: XCTestCase { + func testNever() async throws { + let _: Void = await withUnsafeContinuation { _ in } + } +} diff --git a/Plugins/PackageToJS/Templates/bin/test.js b/Plugins/PackageToJS/Templates/bin/test.js index f4aad4b8..34031628 100644 --- a/Plugins/PackageToJS/Templates/bin/test.js +++ b/Plugins/PackageToJS/Templates/bin/test.js @@ -68,6 +68,19 @@ const harnesses = { options = prelude.setupOptions(options, { isMainThread: true }) } } + process.on("beforeExit", () => { + // NOTE: "beforeExit" is fired when the process exits gracefully without calling `process.exit` + // Either XCTest or swift-testing should always call `process.exit` through `proc_exit` even + // if the test succeeds. So exiting gracefully means something went wrong (e.g. withUnsafeContinuation is leaked) + // Therefore, we exit with code 1 to indicate that the test execution failed. + console.error(` + +================================================================================================= +Detected that the test execution ended without a termination signal from the testing framework. +Hint: This typically means that a continuation leak occurred. +=================================================================================================`) + process.exit(1) + }) await instantiate(options) } catch (e) { if (e instanceof WebAssembly.CompileError) { diff --git a/Plugins/PackageToJS/Tests/ExampleTests.swift b/Plugins/PackageToJS/Tests/ExampleTests.swift index ab0d1d79..d860a685 100644 --- a/Plugins/PackageToJS/Tests/ExampleTests.swift +++ b/Plugins/PackageToJS/Tests/ExampleTests.swift @@ -88,7 +88,6 @@ extension Trait where Self == ConditionTrait { atPath: destinationPath.path, withDestinationPath: linkDestination ) - enumerator.skipDescendants() continue } @@ -117,8 +116,11 @@ extension Trait where Self == ConditionTrait { typealias RunProcess = (_ executableURL: URL, _ args: [String], _ env: [String: String]) throws -> Void typealias RunSwift = (_ args: [String], _ env: [String: String]) throws -> Void - func withPackage(at path: String, body: (URL, _ runProcess: RunProcess, _ runSwift: RunSwift) throws -> Void) throws - { + func withPackage( + at path: String, + assertTerminationStatus: (Int32) -> Bool = { $0 == 0 }, + body: @escaping (URL, _ runProcess: RunProcess, _ runSwift: RunSwift) throws -> Void + ) throws { try withTemporaryDirectory { tempDir, retain in let destination = tempDir.appending(path: Self.repoPath.lastPathComponent) try Self.copyRepository(to: destination) @@ -139,11 +141,11 @@ extension Trait where Self == ConditionTrait { try process.run() process.waitUntilExit() - if process.terminationStatus != 0 { + if !assertTerminationStatus(process.terminationStatus) { retain = true } try #require( - process.terminationStatus == 0, + assertTerminationStatus(process.terminationStatus), """ Swift package should build successfully, check \(destination.appending(path: path).path) for details stdout: \(stdoutPath.path) @@ -275,4 +277,29 @@ extension Trait where Self == ConditionTrait { ) } } + + @Test(.requireSwiftSDK) + func continuationLeakInTest_XCTest() throws { + let swiftSDKID = try #require(Self.getSwiftSDKID()) + try withPackage( + at: "Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest", + assertTerminationStatus: { $0 != 0 } + ) { packageDir, _, runSwift in + try runSwift(["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test"], [:]) + } + } + + #if compiler(>=6.1) + // TODO: Remove triple restriction once swift-testing is shipped in p1-threads SDK + @Test(.requireSwiftSDK(triple: "wasm32-unknown-wasi")) + func continuationLeakInTest_SwiftTesting() throws { + let swiftSDKID = try #require(Self.getSwiftSDKID()) + try withPackage( + at: "Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting", + assertTerminationStatus: { $0 != 0 } + ) { packageDir, _, runSwift in + try runSwift(["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test"], [:]) + } + } + #endif }