Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ alias(
name = "bazel-diff",
actual = "//cli:bazel-diff",
)

alias(
name = "format",
actual = "//cli:format",
)
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 0,
)

bazel_dep(name = "aspect_rules_lint", version = "1.0.2")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2")
bazel_dep(name = "rules_java", version = "7.6.5")
Expand Down Expand Up @@ -31,3 +32,6 @@ use_repo(
maven,
bazel_diff_maven = "bazel_diff_maven",
)

non_module_repositories = use_extension("//:extensions.bzl", "non_module_repositories")
use_repo(non_module_repositories, "ktfmt")
1,258 changes: 1,223 additions & 35 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ maven_install(
load("@bazel_diff_maven//:defs.bzl", "pinned_maven_install")

pinned_maven_install()

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

load(
"@aspect_rules_lint//format:repositories.bzl",
"fetch_ktfmt",
"rules_lint_dependencies",
)

rules_lint_dependencies()

fetch_ktfmt()
16 changes: 14 additions & 2 deletions cli/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
load("@rules_java//java:defs.bzl", "java_binary")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")

Expand Down Expand Up @@ -58,7 +59,6 @@ kt_jvm_test(
runtime_deps = [":cli-test-lib"],
)


kt_jvm_test(
name = "SourceFileHasherTest",
data = [
Expand Down Expand Up @@ -106,10 +106,10 @@ kt_jvm_test(

kt_jvm_test(
name = "E2ETest",
data = [":workspaces"],
jvm_flags = ["-Djava.security.manager=allow"],
test_class = "com.bazel_diff.e2e.E2ETest",
runtime_deps = [":cli-test-lib"],
data = [":workspaces"],
)

kt_jvm_test(
Expand Down Expand Up @@ -146,3 +146,15 @@ filegroup(
"src/test/resources/workspaces",
],
)

java_binary(
name = "ktfmt",
main_class = "com.facebook.ktfmt.cli.Main",
runtime_deps = ["@ktfmt//jar"],
)

format_multirun(
name = "format",
kotlin = ":ktfmt",
visibility = ["//visibility:public"],
)
14 changes: 7 additions & 7 deletions cli/src/main/kotlin/com/bazel_diff/Main.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.bazel_diff

import com.bazel_diff.cli.BazelDiff
import picocli.CommandLine
import kotlin.system.exitProcess
import picocli.CommandLine

class Main {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val exitCode = CommandLine(BazelDiff()).execute(*args)
exitProcess(exitCode)
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
val exitCode = CommandLine(BazelDiff()).execute(*args)
exitProcess(exitCode)
}
}
}
94 changes: 50 additions & 44 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,65 @@ 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
import java.util.Calendar

class BazelClient(
private val useCquery: Boolean,
private val fineGrainedHashExternalRepos: Set<String>,
private val excludeExternalTargets: Boolean
private val useCquery: Boolean,
private val fineGrainedHashExternalRepos: Set<String>,
private val excludeExternalTargets: Boolean
) : KoinComponent {
private val logger: Logger by inject()
private val queryService: BazelQueryService by inject()
private val logger: Logger by inject()
private val queryService: BazelQueryService by inject()

suspend fun queryAllTargets(): List<BazelTarget> {
val queryEpoch = Calendar.getInstance().getTimeInMillis()
suspend fun queryAllTargets(): List<BazelTarget> {
val queryEpoch = Calendar.getInstance().getTimeInMillis()

val repoTargetsQuery = if (excludeExternalTargets) emptyList() else listOf("//external:all-targets")
val targets = if (useCquery) {
// Explicitly listing external repos here sometimes causes issues mentioned at
// https://bazel.build/query/cquery#recursive-target-patterns. Hence, we query all dependencies with `deps`
// instead. However, we still need to append all "//external:*" targets because fine-grained hash
// computation depends on hashing of source files in external repos as well, which is limited to repos
// explicitly mentioned in `fineGrainedHashExternalRepos` flag. Therefore, for any repos not mentioned there
// we are still relying on the repo-generation target under `//external` to compute the hash.
//
// In addition, we must include all source dependencies in this query in order for them to show up in
// `configuredRuleInput`. Hence, one must not filter them out with `kind(rule, deps(..))`. However, these
// source targets are omitted inside BazelQueryService with the custom starlark function used to print
// labels.
(queryService.query("deps(//...:all-targets)", useCquery = true) +
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
.distinctBy { it.rule.name }
val repoTargetsQuery =
if (excludeExternalTargets) emptyList() else listOf("//external:all-targets")
val targets =
if (useCquery) {
// Explicitly listing external repos here sometimes causes issues mentioned at
// https://bazel.build/query/cquery#recursive-target-patterns. Hence, we query all
// dependencies with `deps`
// instead. However, we still need to append all "//external:*" targets because
// fine-grained hash
// computation depends on hashing of source files in external repos as well, which is
// limited to repos
// explicitly mentioned in `fineGrainedHashExternalRepos` flag. Therefore, for any repos
// not mentioned there
// we are still relying on the repo-generation target under `//external` to compute the
// hash.
//
// In addition, we must include all source dependencies in this query in order for them to
// show up in
// `configuredRuleInput`. Hence, one must not filter them out with `kind(rule, deps(..))`.
// However, these
// source targets are omitted inside BazelQueryService with the custom starlark function
// used to print
// labels.
(queryService.query("deps(//...:all-targets)", useCquery = true) +
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
.distinctBy { it.rule.name }
} else {
val buildTargetsQuery = listOf("//...:all-targets") + fineGrainedHashExternalRepos.map { "@$it//...:all-targets" }
queryService.query((repoTargetsQuery + buildTargetsQuery).joinToString(" + ") { "'$it'" })
val buildTargetsQuery =
listOf("//...:all-targets") +
fineGrainedHashExternalRepos.map { "@$it//...:all-targets" }
queryService.query((repoTargetsQuery + buildTargetsQuery).joinToString(" + ") { "'$it'" })
}
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
}
}
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
}
}
}
}
}
Loading
Loading