Skip to content

Commit 4536790

Browse files
Fixes for version command in query service (#318)
* Fixes for version command in query service This can lead to errors in some use cases * readme
1 parent 7eb7c1a commit 4536790

5 files changed

Lines changed: 60 additions & 9 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module(
22
name = "bazel-diff",
3-
version = "17.0.3",
3+
version = "17.1.0",
44
compatibility_level = 0,
55
)
66

MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ First, add the following snippet to your project:
341341
#### Bzlmod snippet
342342

343343
```bazel
344-
bazel_dep(name = "bazel-diff", version = "15.0.5")
344+
bazel_dep(name = "bazel-diff", version = "17.1.0")
345345
```
346346

347347
You can now run the tool with:

cli/src/main/kotlin/com/bazel_diff/bazel/BazelQueryService.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BazelQueryService(
3030

3131
@OptIn(ExperimentalCoroutinesApi::class)
3232
private suspend fun determineBazelVersion(): Triple<Int, Int, Int> {
33-
val cmd = arrayOf(bazelPath.toString(), "--version")
33+
val cmd = arrayOf(bazelPath.toString(), "version")
3434
logger.i { "Executing Bazel version command: ${cmd.joinToString()}" }
3535
val result =
3636
process(
@@ -45,12 +45,19 @@ class BazelQueryService(
4545
throw RuntimeException("Bazel version command failed, exit code ${result.resultCode}")
4646
}
4747

48-
if (result.output.size != 1 || !result.output.first().startsWith("bazel ")) {
49-
throw RuntimeException("Bazel version command returned unexpected output: ${result.output}")
50-
}
48+
// "bazel version" outputs "Build label: X.Y.Z" on one of the lines; accept that or legacy "bazel X.Y.Z".
49+
val versionString =
50+
result.output
51+
.firstOrNull { it.startsWith("Build label: ") }
52+
?.removePrefix("Build label: ")?.trim()
53+
?: result.output
54+
.firstOrNull { it.startsWith("bazel ") }
55+
?.removePrefix("bazel ")?.trim()
56+
?: throw RuntimeException(
57+
"Bazel version command returned unexpected output: ${result.output}")
5158
// Trim off any prerelease suffixes (e.g., 8.6.0-rc1 or 8.6.0rc1).
52-
val versionString = result.output.first().removePrefix("bazel ").trim().split('-')[0]
53-
val version = versionString.split('.').map { it.takeWhile { c -> c.isDigit() }.toInt() }.toTypedArray()
59+
val version =
60+
versionString.split('-')[0].split('.').map { it.takeWhile { c -> c.isDigit() }.toInt() }.toTypedArray()
5461
return Triple(version[0], version[1], version[2])
5562
}
5663

cli/src/test/kotlin/com/bazel_diff/e2e/E2ETest.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,37 @@ class E2ETest {
136136
testE2E(emptyList(), emptyList(), "/fixture/impacted_targets-1-2.txt")
137137
}
138138

139+
@Test
140+
fun testDetermineBazelVersion() {
141+
// E2E coverage for BazelQueryService.determineBazelVersion(): version is resolved lazily
142+
// when the first query runs. Running generate-hashes to completion validates that
143+
// "bazel version" is executed and parsed successfully (e.g. "Build label: X.Y.Z").
144+
val projectA = extractFixtureProject("/fixture/integration-test-1.zip")
145+
val workingDirectory = File(projectA, "integration")
146+
val outputDir = temp.newFolder()
147+
val outputPath = File(outputDir, "hashes.json")
148+
149+
val cli = CommandLine(BazelDiff())
150+
val exitCode = cli.execute(
151+
"generate-hashes",
152+
"-w",
153+
workingDirectory.absolutePath,
154+
"-b",
155+
"bazel",
156+
outputPath.absolutePath)
157+
158+
assertThat(exitCode).isEqualTo(0)
159+
assertThat(outputPath.readText().isNotEmpty()).isEqualTo(true)
160+
}
161+
162+
@Test
163+
fun testE2EWithNoKeepGoing() {
164+
testE2E(
165+
listOf("--no-keep_going"),
166+
emptyList(),
167+
"/fixture/impacted_targets-1-2.txt")
168+
}
169+
139170
@Test
140171
fun testE2EIncludingTargetType() {
141172
testE2E(
@@ -996,6 +1027,19 @@ class E2ETest {
9961027
// which is designed to fail during analysis
9971028
assertThat(exitCodeWithCquery).isEqualTo(1)
9981029

1030+
// Test with --no-keep_going: cquery should fail (no partial results, immediate failure)
1031+
val outputNoKeepGoing = File(outputDir, "hashes_no_keep_going.json")
1032+
val exitCodeWithNoKeepGoing = cli.execute(
1033+
"generate-hashes",
1034+
"-w",
1035+
workspace.absolutePath,
1036+
"-b",
1037+
"bazel",
1038+
"--useCquery",
1039+
"--no-keep_going",
1040+
outputNoKeepGoing.absolutePath)
1041+
assertThat(exitCodeWithNoKeepGoing).isEqualTo(1)
1042+
9991043
// Test with --keep_going enabled (default behavior)
10001044
// With keep_going, cquery returns partial results but still exits with code 1
10011045
// The current implementation allows exit codes 0 and 3, but cquery with keep_going

0 commit comments

Comments
 (0)