Skip to content

Commit ce2b1f5

Browse files
authored
Fix empty query when using --excludeExternalTargets with --useCquery (#280)
1 parent 3e023b9 commit ce2b1f5

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,33 @@ 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 workingDirectory = extractFixtureProject("/fixture/cquery-test-base.zip")
595+
596+
val bazelPath = "bazel"
597+
val outputDir = temp.newFolder()
598+
val hashesJson = File(outputDir, "hashes.json")
599+
600+
val cli = CommandLine(BazelDiff())
601+
602+
val exitCode = cli.execute(
603+
"generate-hashes",
604+
"-w",
605+
workingDirectory.absolutePath,
606+
"-b",
607+
bazelPath,
608+
"--useCquery",
609+
// Platform is specified only to make the cquery succeed.
610+
"--cqueryCommandOptions",
611+
"--platforms=//:jre",
612+
"--excludeExternalTargets",
613+
hashesJson.absolutePath)
614+
assertThat(exitCode).isEqualTo(0)
615+
}
616+
590617
@Test
591618
fun testTargetDistanceMetrics() {
592619
val workspace = copyTestWorkspace("distance_metrics")

0 commit comments

Comments
 (0)