Skip to content

Commit bd71bc5

Browse files
authored
feat: Add flag to exclude external targets (#246)
1 parent 32c7288 commit bd71bc5

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Commands:
122122

123123
```terminal
124124
Usage: bazel-diff generate-hashes [-hkvV] [--[no-]useCquery] [-b=<bazelPath>]
125+
[--[no-]excludeExternalTargets]
125126
[--[no-]includeTargetType]
126127
[--contentHashPath=<contentHashPath>]
127128
[-s=<seedFilepaths>] -w=<workspacePath>
@@ -151,7 +152,11 @@ workspace.
151152
"//cli:bazel-diff_deploy.jar": "GeneratedFile#4ae310f8ad2bc728934e3509b6102ca658e828b9cd668f79990e95c6663f9633",
152153
...
153154
}
154-
----[no-]includeTargetType
155+
--[no-]excludeExternalTargets
156+
If true, exclude external targets. This must be
157+
switched on when using `--noenable_workspace` Bazel
158+
command line option. Defaults to `false`.
159+
--[no-]includeTargetType
155160
Whether include target type in the generated JSON or not.
156161
If false, the generate JSON schema is: {"<target>": "<sha256>"}
157162
If true, the generate JSON schema is: {"<target>": "<type>#<sha256>"

cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import org.koin.core.component.KoinComponent
66
import org.koin.core.component.inject
77
import java.util.Calendar
88

9-
class BazelClient(private val useCquery: Boolean, private val fineGrainedHashExternalRepos: Set<String>) : KoinComponent {
9+
class BazelClient(
10+
private val useCquery: Boolean,
11+
private val fineGrainedHashExternalRepos: Set<String>,
12+
private val excludeExternalTargets: Boolean
13+
) : KoinComponent {
1014
private val logger: Logger by inject()
1115
private val queryService: BazelQueryService by inject()
1216

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

16-
val repoTargetsQuery = listOf("//external:all-targets")
20+
val repoTargetsQuery = if (excludeExternalTargets) emptyList() else listOf("//external:all-targets")
1721
val targets = if (useCquery) {
1822
// Explicitly listing external repos here sometimes causes issues mentioned at
1923
// https://bazel.build/query/cquery#recursive-target-patterns. Hence, we query all dependencies with `deps`

cli/src/main/kotlin/com/bazel_diff/cli/GenerateHashesCommand.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class GenerateHashesCommand : Callable<Int> {
150150
)
151151
var modifiedFilepaths: File? = null
152152

153+
@CommandLine.Option(
154+
names = ["--excludeExternalTargets"],
155+
negatable = true,
156+
description = ["If true, exclude external targets. This must be switched on when using `--noenable_workspace` Bazel command line option. Defaults to `false`."],
157+
scope = CommandLine.ScopeType.INHERIT
158+
)
159+
var excludeExternalTargets = false
160+
153161
@CommandLine.Spec
154162
lateinit var spec: CommandLine.Model.CommandSpec
155163

@@ -169,6 +177,7 @@ class GenerateHashesCommand : Callable<Int> {
169177
keepGoing,
170178
depsMappingJSONPath != null,
171179
fineGrainedHashExternalRepos,
180+
excludeExternalTargets,
172181
),
173182
loggingModule(parent.verbose),
174183
serialisationModule(),

cli/src/main/kotlin/com/bazel_diff/di/Modules.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fun hasherModule(
3030
keepGoing: Boolean,
3131
trackDeps: Boolean,
3232
fineGrainedHashExternalRepos: Set<String>,
33+
excludeExternalTargets: Boolean,
3334
): Module = module {
3435
val cmd: MutableList<String> = ArrayList<String>().apply {
3536
add(bazelPath.toString())
@@ -59,7 +60,7 @@ fun hasherModule(
5960
debug
6061
)
6162
}
62-
single { BazelClient(useCquery, fineGrainedHashExternalRepos) }
63+
single { BazelClient(useCquery, fineGrainedHashExternalRepos, excludeExternalTargets) }
6364
single { BuildGraphHasher(get()) }
6465
single { TargetHasher() }
6566
single { RuleHasher(useCquery, trackDeps, fineGrainedHashExternalRepos) }

cli/src/test/kotlin/com/bazel_diff/Modules.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fun testModule(): Module = module {
1414
val outputBase = Paths.get("output-base")
1515
val workingDirectory = Paths.get("working-directory")
1616
single<Logger> { SilentLogger }
17-
single { BazelClient(false, emptySet()) }
17+
single { BazelClient(false, emptySet(), false) }
1818
single { BuildGraphHasher(get()) }
1919
single { TargetHasher() }
2020
single { RuleHasher(false, true, emptySet()) }

0 commit comments

Comments
 (0)