Skip to content

Commit

Permalink
fix: Fix cquery targets filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
honnix committed Oct 23, 2024
1 parent 3f3d99e commit bf9aaef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.bazel_diff.bazel

import com.bazel_diff.log.Logger
import com.google.devtools.build.lib.query2.proto.proto2api.Build
import java.util.Calendar
import java.util.*
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand Down Expand Up @@ -42,7 +41,7 @@ class BazelClient(
// labels.
(queryService.query("deps(//...:all-targets)", useCquery = true) +
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
.distinctBy { it.rule.name }
.distinctBy { it.name }
} else {
val buildTargetsQuery =
listOf("//...:all-targets") +
Expand All @@ -51,16 +50,6 @@ class BazelClient(
}
val queryDuration = Calendar.getInstance().getTimeInMillis() - queryEpoch
logger.i { "All targets queried in $queryDuration" }
return targets.mapNotNull { target: Build.Target ->
when (target.type) {
Build.Target.Discriminator.RULE -> BazelTarget.Rule(target)
Build.Target.Discriminator.SOURCE_FILE -> BazelTarget.SourceFile(target)
Build.Target.Discriminator.GENERATED_FILE -> BazelTarget.GeneratedFile(target)
else -> {
logger.w { "Unsupported target type in the build graph: ${target.type.name}" }
null
}
}
}
return targets
}
}
34 changes: 24 additions & 10 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelQueryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BazelQueryService(
) : KoinComponent {
private val logger: Logger by inject()

suspend fun query(query: String, useCquery: Boolean = false): List<Build.Target> {
suspend fun query(query: String, useCquery: Boolean = false): List<BazelTarget> {
// Unfortunately, there is still no direct way to tell if a target is compatible or not with the
// proto output
// by itself. So we do an extra cquery with the trick at
Expand All @@ -46,16 +46,18 @@ class BazelQueryService(
if (useCquery) {
val cqueryResult = AnalysisProtosV2.CqueryResult.parseFrom(proto)
cqueryResult.resultsList
.filter { it.target.rule.name in compatibleTargetSet }
.map { it.target }
.mapNotNull { toBazelTarget(it.target) }
.filter { it.name in compatibleTargetSet }
} else {
mutableListOf<Build.Target>().apply {
while (true) {
val target = Build.Target.parseDelimitedFrom(proto) ?: break
// EOF
add(target)
}
}
mutableListOf<Build.Target>()
.apply {
while (true) {
val target = Build.Target.parseDelimitedFrom(proto) ?: break
// EOF
add(target)
}
}
.mapNotNull { toBazelTarget(it) }
}
}

Expand Down Expand Up @@ -152,4 +154,16 @@ class BazelQueryService(
throw RuntimeException("Bazel query failed, exit code ${result.resultCode}")
return outputFile
}

private fun toBazelTarget(target: Build.Target): BazelTarget? {
return when (target.type) {
Build.Target.Discriminator.RULE -> BazelTarget.Rule(target)
Build.Target.Discriminator.SOURCE_FILE -> BazelTarget.SourceFile(target)
Build.Target.Discriminator.GENERATED_FILE -> BazelTarget.GeneratedFile(target)
else -> {
logger.w { "Unsupported target type in the build graph: ${target.type.name}" }
null
}
}
}
}

0 comments on commit bf9aaef

Please sign in to comment.