Skip to content

Commit 6499dc8

Browse files
committed
Fix empty query when using --excludeExternalTargets with --useCquery
1 parent 3e023b9 commit 6499dc8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ class BazelClient(
3535
// In addition, we must include all source dependencies in this query in order for them to
3636
// show up in
3737
// `configuredRuleInput`. Hence, one must not filter them out with `kind(rule, deps(..))`.
38-
(queryService.query("deps(//...:all-targets)", useCquery = true) +
39-
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
40-
.distinctBy { it.name }
38+
val mainTargets = queryService.query("deps(//...:all-targets)", useCquery = true)
39+
val repoTargets = if (repoTargetsQuery.isNotEmpty()) {
40+
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" })
41+
} else {
42+
emptyList()
43+
}
44+
(mainTargets + repoTargets).distinctBy { it.name }
4145
} else {
4246
val buildTargetsQuery =
4347
listOf("//...:all-targets") +

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,31 @@ class E2ETest {
587587
assertThat(actual).isEqualTo(expected)
588588
}
589589

590+
@Test
591+
fun testUseCqueryWithExcludeExternalTargets() {
592+
// This test verifies the fix for the issue where using --excludeExternalTargets with --useCquery
593+
// would cause an empty query string to be passed to Bazel, resulting in exit code 2.
594+
val project = extractFixtureProject("/fixture/integration-test-1.zip")
595+
596+
val workingDirectory = File(project, "integration")
597+
val bazelPath = "bazel"
598+
val outputDir = temp.newFolder()
599+
val hashesJson = File(outputDir, "hashes.json")
600+
601+
val cli = CommandLine(BazelDiff())
602+
603+
val exitCodeFrom = cli.execute(
604+
"generate-hashes",
605+
"-w",
606+
workingDirectory.absolutePath,
607+
"-b",
608+
bazelPath,
609+
"--useCquery",
610+
"--excludeExternalTargets",
611+
hashesJson.absolutePath)
612+
assertThat(exitCodeFrom).isEqualTo(0)
613+
}
614+
590615
@Test
591616
fun testTargetDistanceMetrics() {
592617
val workspace = copyTestWorkspace("distance_metrics")

0 commit comments

Comments
 (0)