Skip to content

Commit 7fc819e

Browse files
committed
Use --preemptible if using only one output base
1 parent 33148f7 commit 7fc819e

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

Sources/SourceKitBazelBSP/RequestHandlers/PrepareHandler.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ final class PrepareHandler {
105105
nonisolated(unsafe) let completion = completion
106106
try currentTaskLock.withLock { [commandRunner, initializedConfig] currentTask in
107107
// Build the provided targets, on our special output base and taking into account special index flags.
108+
// If using only one output base, add --preemptible to allow the task to be overridden if needed.
109+
let preemptible = initializedConfig.baseConfig.useSeparateOutputBaseForAquery == false
108110
let process = try commandRunner.bazelIndexAction(
109111
baseConfig: initializedConfig.baseConfig,
110112
outputBase: initializedConfig.outputBase,
111113
cmd: "build \(labelsToBuild.joined(separator: " "))",
112-
rootUri: initializedConfig.rootUri
114+
rootUri: initializedConfig.rootUri,
115+
preemptible: preemptible
113116
)
114117
process.setTerminationHandler { code, stderr in
115118
if code == 0 {

Sources/SourceKitBazelBSP/SharedUtils/Shell/CommandRunner.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extension CommandRunner {
6464
outputBase: String,
6565
cmd: String,
6666
rootUri: String,
67+
preemptible: Bool = false,
6768
) throws -> RunningProcess {
6869
let indexFlags = baseConfig.indexFlags
6970
let additionalFlags: String
@@ -72,7 +73,8 @@ extension CommandRunner {
7273
} else {
7374
additionalFlags = indexFlags.map { " \($0)" }.joined(separator: "")
7475
}
75-
let cmd = "--output_base=\(outputBase) \(cmd)\(additionalFlags)"
76+
let preemptibleFlag = preemptible ? " --preemptible " : " "
77+
let cmd = "--output_base=\(outputBase)\(preemptibleFlag)\(cmd)\(additionalFlags)"
7678
return try bazel(baseConfig: baseConfig, rootUri: rootUri, cmd: cmd)
7779
}
7880

@@ -82,12 +84,14 @@ extension CommandRunner {
8284
outputBase: String,
8385
cmd: String,
8486
rootUri: String,
87+
preemptible: Bool = false,
8588
) throws -> T {
8689
let process = try bazelIndexAction(
8790
baseConfig: baseConfig,
8891
outputBase: outputBase,
8992
cmd: cmd,
90-
rootUri: rootUri
93+
rootUri: rootUri,
94+
preemptible: preemptible
9195
)
9296
return try process.result()
9397
}

Tests/SourceKitBazelBSPTests/PrepareHandlerTests.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct PrepareHandlerTests {
5353
sdkRootPaths: ["iphonesimulator": "bar"]
5454
)
5555

56-
let expectedCommand = "bazel --output_base=/tmp/output_base build //HelloWorld --config=index"
56+
let expectedCommand = "bazel --output_base=/tmp/output_base --preemptible build //HelloWorld --config=index"
5757
commandRunner.setResponse(for: expectedCommand, cwd: rootUri, response: "")
5858

5959
let handler = PrepareHandler(
@@ -101,7 +101,7 @@ struct PrepareHandlerTests {
101101
sdkRootPaths: ["iphonesimulator": "bar"]
102102
)
103103

104-
let expectedCommand = "bazel --output_base=/tmp/output_base build //HelloWorld //HelloWorld2 --config=index"
104+
let expectedCommand = "bazel --output_base=/tmp/output_base --preemptible build //HelloWorld //HelloWorld2 --config=index"
105105
commandRunner.setResponse(for: expectedCommand, response: "Build completed")
106106

107107
let handler = PrepareHandler(
@@ -124,4 +124,55 @@ struct PrepareHandlerTests {
124124
#expect(ranCommands[0].command == expectedCommand)
125125
#expect(ranCommands[0].cwd == "/path/to/project")
126126
}
127+
128+
@Test
129+
func skipsPreemptibleFlagIfUsingSeparateOutputBaseForAquery() throws {
130+
let commandRunner = CommandRunnerFake()
131+
let connection = LSPConnectionFake()
132+
133+
let rootUri = "/path/to/project"
134+
let baseConfig = BaseServerConfig(
135+
bazelWrapper: "bazel",
136+
targets: ["//HelloWorld"],
137+
indexFlags: ["--config=index"],
138+
buildTestSuffix: "_(PLAT)_skbsp",
139+
buildTestPlatformPlaceholder: "(PLAT)",
140+
filesToWatch: nil,
141+
useSeparateOutputBaseForAquery: true
142+
)
143+
144+
let initializedConfig = InitializedServerConfig(
145+
baseConfig: baseConfig,
146+
rootUri: rootUri,
147+
outputBase: "/tmp/output_base",
148+
outputPath: "/tmp/output_path",
149+
devDir: "/Applications/Xcode.app/Contents/Developer",
150+
devToolchainPath: "/a/b/XcodeDefault.xctoolchain/",
151+
executionRoot: "/tmp/output_path/execoot/_main",
152+
sdkRootPaths: ["iphonesimulator": "bar"]
153+
)
154+
155+
let expectedCommand = "bazel --output_base=/tmp/output_base build //HelloWorld --config=index"
156+
commandRunner.setResponse(for: expectedCommand, cwd: rootUri, response: "")
157+
158+
let handler = PrepareHandler(
159+
initializedConfig: initializedConfig,
160+
targetStore: BazelTargetStoreImpl(initializedConfig: initializedConfig),
161+
commandRunner: commandRunner,
162+
connection: connection
163+
)
164+
165+
let semaphore = DispatchSemaphore(value: 0)
166+
try handler.build(bazelLabels: baseConfig.targets, id: RequestID.number(1)) { error in
167+
#expect(error == nil)
168+
semaphore.signal()
169+
}
170+
171+
#expect(semaphore.wait(timeout: .now() + 1) == .success)
172+
173+
let ranCommands = commandRunner.commands
174+
#expect(ranCommands.count == 1)
175+
#expect(ranCommands[0].command == expectedCommand)
176+
#expect(ranCommands[0].cwd == rootUri)
177+
}
127178
}

0 commit comments

Comments
 (0)