Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ final class PrepareHandler {
nonisolated(unsafe) let completion = completion
try currentTaskLock.withLock { [commandRunner, initializedConfig] currentTask in
// Build the provided targets, on our special output base and taking into account special index flags.
// If using only one output base, add --preemptible to allow the task to be overridden if needed.
let preemptible = initializedConfig.baseConfig.useSeparateOutputBaseForAquery == false
let process = try commandRunner.bazelIndexAction(
baseConfig: initializedConfig.baseConfig,
outputBase: initializedConfig.outputBase,
cmd: "build \(labelsToBuild.joined(separator: " "))",
rootUri: initializedConfig.rootUri
rootUri: initializedConfig.rootUri,
preemptible: preemptible
)
process.setTerminationHandler { code, stderr in
if code == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extension CommandRunner {
outputBase: String,
cmd: String,
rootUri: String,
preemptible: Bool = false,
) throws -> RunningProcess {
let indexFlags = baseConfig.indexFlags
let additionalFlags: String
Expand All @@ -72,7 +73,8 @@ extension CommandRunner {
} else {
additionalFlags = indexFlags.map { " \($0)" }.joined(separator: "")
}
let cmd = "--output_base=\(outputBase) \(cmd)\(additionalFlags)"
let preemptibleFlag = preemptible ? " --preemptible " : " "
let cmd = "--output_base=\(outputBase)\(preemptibleFlag)\(cmd)\(additionalFlags)"
return try bazel(baseConfig: baseConfig, rootUri: rootUri, cmd: cmd)
}

Expand All @@ -82,12 +84,14 @@ extension CommandRunner {
outputBase: String,
cmd: String,
rootUri: String,
preemptible: Bool = false,
) throws -> T {
let process = try bazelIndexAction(
baseConfig: baseConfig,
outputBase: outputBase,
cmd: cmd,
rootUri: rootUri
rootUri: rootUri,
preemptible: preemptible
)
return try process.result()
}
Expand Down
56 changes: 54 additions & 2 deletions Tests/SourceKitBazelBSPTests/PrepareHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct PrepareHandlerTests {
sdkRootPaths: ["iphonesimulator": "bar"]
)

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

let handler = PrepareHandler(
Expand Down Expand Up @@ -101,7 +101,8 @@ struct PrepareHandlerTests {
sdkRootPaths: ["iphonesimulator": "bar"]
)

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

let handler = PrepareHandler(
Expand All @@ -124,4 +125,55 @@ struct PrepareHandlerTests {
#expect(ranCommands[0].command == expectedCommand)
#expect(ranCommands[0].cwd == "/path/to/project")
}

@Test
func skipsPreemptibleFlagIfUsingSeparateOutputBaseForAquery() throws {
let commandRunner = CommandRunnerFake()
let connection = LSPConnectionFake()

let rootUri = "/path/to/project"
let baseConfig = BaseServerConfig(
bazelWrapper: "bazel",
targets: ["//HelloWorld"],
indexFlags: ["--config=index"],
buildTestSuffix: "_(PLAT)_skbsp",
buildTestPlatformPlaceholder: "(PLAT)",
filesToWatch: nil,
useSeparateOutputBaseForAquery: true
)

let initializedConfig = InitializedServerConfig(
baseConfig: baseConfig,
rootUri: rootUri,
outputBase: "/tmp/output_base",
outputPath: "/tmp/output_path",
devDir: "/Applications/Xcode.app/Contents/Developer",
devToolchainPath: "/a/b/XcodeDefault.xctoolchain/",
executionRoot: "/tmp/output_path/execoot/_main",
sdkRootPaths: ["iphonesimulator": "bar"]
)

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

let handler = PrepareHandler(
initializedConfig: initializedConfig,
targetStore: BazelTargetStoreImpl(initializedConfig: initializedConfig),
commandRunner: commandRunner,
connection: connection
)

let semaphore = DispatchSemaphore(value: 0)
try handler.build(bazelLabels: baseConfig.targets, id: RequestID.number(1)) { error in
#expect(error == nil)
semaphore.signal()
}

#expect(semaphore.wait(timeout: .now() + 1) == .success)

let ranCommands = commandRunner.commands
#expect(ranCommands.count == 1)
#expect(ranCommands[0].command == expectedCommand)
#expect(ranCommands[0].cwd == rootUri)
}
}