Skip to content

Commit 572660a

Browse files
committed
Add timeouts to most subprocess calls in tests
Just a speculative fix to avoid timeouts during the `swift test` invocation
1 parent 1cd1102 commit 572660a

File tree

8 files changed

+86
-47
lines changed

8 files changed

+86
-47
lines changed

Sources/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@_spi(Testing) import BuildSystemIntegration
1414
package import Foundation
1515
package import LanguageServerProtocol
16+
import SKLogging
1617
import SKOptions
1718
import SourceKitLSP
1819
import SwiftExtensions
@@ -126,7 +127,11 @@ package struct IndexedSingleSwiftFileTestProject {
126127

127128
// Run swiftc to build the index store
128129
do {
129-
try await Process.checkNonZeroExit(arguments: [swiftc.filePath] + compilerArguments)
130+
let compilerArgumentsCopy = compilerArguments
131+
let output = try await withTimeout(defaultTimeoutDuration) {
132+
try await Process.checkNonZeroExit(arguments: [swiftc.filePath] + compilerArgumentsCopy)
133+
}
134+
logger.debug("swiftc output:\n\(output)")
130135
} catch {
131136
if !allowBuildFailure {
132137
throw error

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,22 @@ package actor SkipUnless {
222222
throw FailedToCrateInputFileError()
223223
}
224224
// If we can't compile for wasm, this fails complaining that it can't find the stdlib for wasm.
225-
let process = Process(
226-
args: try swiftFrontend.filePath,
227-
"-typecheck",
228-
try input.filePath,
229-
"-triple",
230-
"wasm32-unknown-none-wasm",
231-
"-enable-experimental-feature",
232-
"Embedded",
233-
"-Xcc",
234-
"-fdeclspec"
235-
)
236-
try process.launch()
237-
let result = try await process.waitUntilExit()
225+
let result = try await withTimeout(defaultTimeoutDuration) {
226+
try await Process.run(
227+
arguments: [
228+
try swiftFrontend.filePath,
229+
"-typecheck",
230+
try input.filePath,
231+
"-triple",
232+
"wasm32-unknown-none-wasm",
233+
"-enable-experimental-feature",
234+
"Embedded",
235+
"-Xcc",
236+
"-fdeclspec",
237+
],
238+
workingDirectory: nil
239+
)
240+
}
238241
if result.exitStatus == .terminated(code: 0) {
239242
return .featureSupported
240243
}

Sources/SKTestSupport/SwiftPMDependencyProject.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package import Foundation
1414
import LanguageServerProtocolExtensions
1515
import SwiftExtensions
16+
import TSCExtensions
1617
import XCTest
1718

1819
import struct TSCBasic.AbsolutePath
@@ -45,11 +46,12 @@ package class SwiftPMDependencyProject {
4546
}
4647
// We can't use `workingDirectory` because Amazon Linux doesn't support working directories (or at least
4748
// TSCBasic.Process doesn't support working directories on Amazon Linux)
48-
let process = TSCBasic.Process(
49-
arguments: [try git.filePath, "-C", try workingDirectory.filePath] + arguments
50-
)
51-
try process.launch()
52-
let processResult = try await process.waitUntilExit()
49+
let processResult = try await withTimeout(defaultTimeoutDuration) {
50+
try await TSCBasic.Process.run(
51+
arguments: [try git.filePath, "-C", try workingDirectory.filePath] + arguments,
52+
workingDirectory: nil
53+
)
54+
}
5355
guard processResult.exitStatus == .terminated(code: 0) else {
5456
throw Error.processedTerminatedWithNonZeroExitCode(processResult)
5557
}

Sources/SKTestSupport/SwiftPMTestProject.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
package import Foundation
1414
package import LanguageServerProtocol
15+
import SKLogging
1516
package import SKOptions
1617
package import SourceKitLSP
1718
import SwiftExtensions
@@ -260,7 +261,16 @@ package class SwiftPMTestProject: MultiFileTestProject {
260261
"-Xswiftc", "-module-cache-path", "-Xswiftc", try globalModuleCache.filePath,
261262
]
262263
}
263-
try await Process.checkNonZeroExit(arguments: arguments)
264+
let argumentsCopy = arguments
265+
let output = try await withTimeout(defaultTimeoutDuration) {
266+
try await Process.checkNonZeroExit(arguments: argumentsCopy)
267+
}
268+
logger.debug(
269+
"""
270+
'swift build' output:
271+
\(output)
272+
"""
273+
)
264274
}
265275

266276
/// Resolve package dependencies for the package at `path`.
@@ -274,6 +284,14 @@ package class SwiftPMTestProject: MultiFileTestProject {
274284
"resolve",
275285
"--package-path", try path.filePath,
276286
]
277-
try await Process.checkNonZeroExit(arguments: arguments)
287+
let output = try await withTimeout(defaultTimeoutDuration) {
288+
try await Process.checkNonZeroExit(arguments: arguments)
289+
}
290+
logger.debug(
291+
"""
292+
'swift package resolve' output:
293+
\(output)
294+
"""
295+
)
278296
}
279297
}

Sources/SourceKitLSP/Swift/DocumentFormatting.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ extension SwiftLanguageService {
196196
writeStream.send(snapshot.text)
197197
try writeStream.close()
198198

199-
let result = try await process.waitUntilExitStoppingProcessOnTaskCancellation()
199+
let result = try await withTimeout(.seconds(60)) {
200+
try await process.waitUntilExitStoppingProcessOnTaskCancellation()
201+
}
200202
guard result.exitStatus == .terminated(code: 0) else {
201203
let swiftFormatErrorMessage: String
202204
switch result.stderrOutput {

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,14 +1353,24 @@ final class BackgroundIndexingTests: XCTestCase {
13531353
// - The user runs `swift package update`
13541354
// - This updates `Package.resolved`, which we watch
13551355
// - We reload the package, which updates `Dependency.swift` in `.build/index-build/checkouts`, which we also watch.
1356-
try await Process.run(
1357-
arguments: [
1358-
unwrap(ToolchainRegistry.forTesting.default?.swift?.filePath),
1359-
"package", "update",
1360-
"--package-path", project.scratchDirectory.filePath,
1361-
],
1362-
workingDirectory: nil
1356+
let projectURL = project.scratchDirectory
1357+
let packageUpdateOutput = try await withTimeout(defaultTimeoutDuration) {
1358+
try await Process.run(
1359+
arguments: [
1360+
unwrap(ToolchainRegistry.forTesting.default?.swift?.filePath),
1361+
"package", "update",
1362+
"--package-path", projectURL.filePath,
1363+
],
1364+
workingDirectory: nil
1365+
)
1366+
}
1367+
logger.debug(
1368+
"""
1369+
'swift package update' output:
1370+
\(packageUpdateOutput)
1371+
"""
13631372
)
1373+
13641374
XCTAssertNotEqual(try String(contentsOf: packageResolvedURL, encoding: .utf8), originalPackageResolvedContents)
13651375
project.testClient.send(
13661376
DidChangeWatchedFilesNotification(changes: [

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -690,13 +690,7 @@ final class WorkspaceTests: XCTestCase {
690690

691691
let packageDir = try project.uri(for: "Package.swift").fileURL!.deletingLastPathComponent()
692692

693-
try await TSCBasic.Process.checkNonZeroExit(arguments: [
694-
ToolchainRegistry.forTesting.default!.swift!.filePath,
695-
"build",
696-
"--package-path", packageDir.filePath,
697-
"-Xswiftc", "-index-ignore-system-modules",
698-
"-Xcc", "-index-ignore-system-symbols",
699-
])
693+
try await SwiftPMTestProject.build(at: packageDir)
700694

701695
let (otherPackageUri, positions) = try project.openDocument("otherPackage.swift")
702696
let testPosition = positions["1️⃣"]
@@ -1356,13 +1350,16 @@ final class WorkspaceTests: XCTestCase {
13561350
)
13571351

13581352
let clang = try unwrap(await ToolchainRegistry.forTesting.default?.clang)
1359-
try await Process.checkNonZeroExit(
1360-
arguments: [
1361-
clang.filePath, "-index-store-path", scratchDirectory.appendingPathComponent("index").filePath,
1362-
scratchDirectory.appendingPathComponent("test.c").filePath,
1363-
"-fsyntax-only",
1364-
]
1365-
)
1353+
let clangOutput = try await withTimeout(defaultTimeoutDuration) {
1354+
try await Process.checkNonZeroExit(
1355+
arguments: [
1356+
clang.filePath, "-index-store-path", scratchDirectory.appendingPathComponent("index").filePath,
1357+
scratchDirectory.appendingPathComponent("test.c").filePath,
1358+
"-fsyntax-only",
1359+
]
1360+
)
1361+
}
1362+
logger.debug("Clang output:\n\(clangOutput)")
13661363

13671364
let testClient = try await TestSourceKitLSPClient(
13681365
options: .testDefault(experimentalFeatures: [.sourceKitOptionsRequest]),

Tests/TSCExtensionsTests/ProcessRunTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ final class ProcessRunTests: XCTestCase {
3838
print(os.getcwd(), end='')
3939
""".write(to: pythonFile, atomically: true, encoding: .utf8)
4040

41-
let result = try await Process.run(
42-
arguments: [python.filePath, pythonFile.filePath],
43-
workingDirectory: AbsolutePath(validating: workingDir.filePath)
44-
)
41+
let result = try await withTimeout(defaultTimeoutDuration) {
42+
try await Process.run(
43+
arguments: [python.filePath, pythonFile.filePath],
44+
workingDirectory: AbsolutePath(validating: workingDir.filePath)
45+
)
46+
}
4547
let stdout = try unwrap(String(bytes: result.output.get(), encoding: .utf8))
4648
XCTAssertEqual(stdout, try workingDir.filePath)
4749
}

0 commit comments

Comments
 (0)