Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix cquery targets filtering #251

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.

15 changes: 2 additions & 13 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
@@//src/main/java/com/integration:GuavaUserAndroid.java
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's surprising this source file appears here, but it is not filtered out by the starlark function. From my observation, it does have providers:

{"VisibilityProvider": <input file target //src/main/java/com/integration:GuavaUserAndroid.java>, "LicensesProvider": <input file target //src/main/java/com/integration:GuavaUserAndroid.java>, "FileProvider": , "FilesToRunProvider": <unknown object com.google.devtools.build.lib.analysis.FilesToRunProvider$SingleExecutableFilesToRunProvider>}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted that comment in a later commit

@@//src/main/java/com/integration:android
@@//src/main/java/com/integration:android-src.jar
@@//src/main/java/com/integration:android.jar
@@//src/main/java/com/integration:android_deploy-src.jar
@@//src/main/java/com/integration:android_deploy.jar
@@//src/main/java/com/integration:android_deploy.jar.unstripped
@@//src/main/java/com/integration:android_deployjars_internal_rule
@@//src/main/java/com/integration:guava-user
@@//src/main/java/com/integration:libguava-user-src.jar
@@//src/main/java/com/integration:libguava-user.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@@//src/main/java/com/integration:GuavaUserAndroid.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
//external:bazel_diff_maven_android
@@//src/main/java/com/integration:android
@@//src/main/java/com/integration:android-src.jar
@@//src/main/java/com/integration:android.jar
@@//src/main/java/com/integration:android_deploy-src.jar
@@//src/main/java/com/integration:android_deploy.jar
@@//src/main/java/com/integration:android_deploy.jar.unstripped
@@//src/main/java/com/integration:android_deployjars_internal_rule
@@//src/main/java/com/integration:guava-user
@@//src/main/java/com/integration:libguava-user-src.jar
@@//src/main/java/com/integration:libguava-user.jar
@@bazel_diff_maven_android//:com_google_errorprone_error_prone_annotations
@@bazel_diff_maven_android//:com_google_guava_guava
@@bazel_diff_maven_android//:com_google_j2objc_j2objc_annotations
@@bazel_diff_maven_android//:org_checkerframework_checker_qual
@@bazel_diff_maven_android//:v1/https/jcenter.bintray.com/com/google/errorprone/error_prone_annotations/2.18.0/error_prone_annotations-2.18.0.jar
@@bazel_diff_maven_android//:v1/https/jcenter.bintray.com/com/google/guava/guava/32.0.0-android/guava-32.0.0-android.jar
@@bazel_diff_maven_android//:v1/https/jcenter.bintray.com/com/google/j2objc/j2objc-annotations/2.8/j2objc-annotations-2.8.jar
@@bazel_diff_maven_android//:v1/https/jcenter.bintray.com/org/checkerframework/checker-qual/3.33.0/checker-qual-3.33.0.jar
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
@@//src/main/java/com/integration:guava-user
@@//src/main/java/com/integration:libguava-user-src.jar
@@//src/main/java/com/integration:libguava-user.jar
@@rules_jvm_external~~maven~com_google_errorprone_error_prone_annotations_2_18_0//file:file
@@rules_jvm_external~~maven~com_google_errorprone_error_prone_annotations_2_18_0//file:v1/com/google/errorprone/error_prone_annotations/2.18.0/error_prone_annotations-2.18.0.jar
@@rules_jvm_external~~maven~com_google_guava_guava_32_0_0_jre//file:file
@@rules_jvm_external~~maven~com_google_guava_guava_32_0_0_jre//file:v1/com/google/guava/guava/32.0.0-jre/guava-32.0.0-jre.jar
@@rules_jvm_external~~maven~com_google_j2objc_j2objc_annotations_2_8//file:file
@@rules_jvm_external~~maven~com_google_j2objc_j2objc_annotations_2_8//file:v1/com/google/j2objc/j2objc-annotations/2.8/j2objc-annotations-2.8.jar
@@rules_jvm_external~~maven~maven//:com/google/errorprone/error_prone_annotations/2.18.0/error_prone_annotations-2.18.0.jar
@@rules_jvm_external~~maven~maven//:com/google/guava/guava/32.0.0-jre/guava-32.0.0-jre.jar
@@rules_jvm_external~~maven~maven//:com/google/j2objc/j2objc-annotations/2.8/j2objc-annotations-2.8.jar
@@rules_jvm_external~~maven~maven//:com_google_errorprone_error_prone_annotations
@@rules_jvm_external~~maven~maven//:com_google_errorprone_error_prone_annotations_2_18_0_extension
@@rules_jvm_external~~maven~maven//:com_google_guava_guava
@@rules_jvm_external~~maven~maven//:com_google_guava_guava_32_0_0_jre_extension
@@rules_jvm_external~~maven~maven//:com_google_j2objc_j2objc_annotations
@@rules_jvm_external~~maven~maven//:com_google_j2objc_j2objc_annotations_2_8_extension
@@rules_jvm_external~~maven~maven//:org/checkerframework/checker-qual/3.33.0/checker-qual-3.33.0.jar
@@rules_jvm_external~~maven~maven//:org_checkerframework_checker_qual
@@rules_jvm_external~~maven~maven//:org_checkerframework_checker_qual_3_33_0_extension
@@rules_jvm_external~~maven~org_checkerframework_checker_qual_3_33_0//file:file
@@rules_jvm_external~~maven~org_checkerframework_checker_qual_3_33_0//file:v1/org/checkerframework/checker-qual/3.33.0/checker-qual-3.33.0.jar
Loading