-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBazelClient.kt
More file actions
80 lines (74 loc) · 3.47 KB
/
Copy pathBazelClient.kt
File metadata and controls
80 lines (74 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.bazel_diff.bazel
import com.bazel_diff.log.Logger
import java.util.Calendar
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class BazelClient(
private val useCquery: Boolean,
private val cqueryExpression: String?,
private val fineGrainedHashExternalRepos: Set<String>,
private val excludeExternalTargets: Boolean,
) : KoinComponent {
private val logger: Logger by inject()
private val queryService: BazelQueryService by inject()
private val bazelModService: BazelModService by inject()
suspend fun queryAllTargets(): List<BazelTarget> {
val queryEpoch = Calendar.getInstance().getTimeInMillis()
// Skip //external:all-targets when explicitly excluded or when Bzlmod is enabled (//external
// not
// available).
val repoTargetsQuery =
if (excludeExternalTargets || bazelModService.isBzlmodEnabled) {
emptyList()
} else {
listOf("//external:all-targets")
}
// When Bzlmod is enabled and Bazel 8.6.0+ is available, query bzlmod-managed external repo
// definitions via `bazel mod show_repo --output=streamed_proto`. This creates synthetic
// //external:* targets from Build.Repository protos, enabling fine-grained hashing of
// bzlmod dependencies.
val bzlmodRepoTargets =
if (bazelModService.isBzlmodEnabled && queryService.canUseBzlmodShowRepo) {
logger.i { "Querying bzlmod-managed external repos via mod show_repo" }
queryService.queryBzlmodRepos()
} else {
emptyList()
}
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(..))`.
val expression = cqueryExpression ?: "deps(//...:all-targets)"
val mainTargets = queryService.query(expression, useCquery = true)
val repoTargets =
if (repoTargetsQuery.isNotEmpty()) {
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" })
} else {
emptyList()
}
(mainTargets + repoTargets).distinctBy { it.name }
} else {
val buildTargetsQuery =
listOf("//...:all-targets") +
fineGrainedHashExternalRepos.map { "$it//...:all-targets" }
queryService.query((repoTargetsQuery + buildTargetsQuery).joinToString(" + ") { "'$it'" })
}
val allTargets = (targets + bzlmodRepoTargets).distinctBy { it.name }
val queryDuration = Calendar.getInstance().getTimeInMillis() - queryEpoch
logger.i { "All targets queried in $queryDuration" }
return allTargets
}
}