Skip to content

Commit

Permalink
feat: Add flag to exclude external targets (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
honnix authored Oct 22, 2024
1 parent 32c7288 commit bd71bc5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Commands:

```terminal
Usage: bazel-diff generate-hashes [-hkvV] [--[no-]useCquery] [-b=<bazelPath>]
[--[no-]excludeExternalTargets]
[--[no-]includeTargetType]
[--contentHashPath=<contentHashPath>]
[-s=<seedFilepaths>] -w=<workspacePath>
Expand Down Expand Up @@ -151,7 +152,11 @@ workspace.
"//cli:bazel-diff_deploy.jar": "GeneratedFile#4ae310f8ad2bc728934e3509b6102ca658e828b9cd668f79990e95c6663f9633",
...
}
----[no-]includeTargetType
--[no-]excludeExternalTargets
If true, exclude external targets. This must be
switched on when using `--noenable_workspace` Bazel
command line option. Defaults to `false`.
--[no-]includeTargetType
Whether include target type in the generated JSON or not.
If false, the generate JSON schema is: {"<target>": "<sha256>"}
If true, the generate JSON schema is: {"<target>": "<type>#<sha256>"
Expand Down
8 changes: 6 additions & 2 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ 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>) : KoinComponent {
class BazelClient(
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()

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

val repoTargetsQuery = listOf("//external:all-targets")
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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ class GenerateHashesCommand : Callable<Int> {
)
var modifiedFilepaths: File? = null

@CommandLine.Option(
names = ["--excludeExternalTargets"],
negatable = true,
description = ["If true, exclude external targets. This must be switched on when using `--noenable_workspace` Bazel command line option. Defaults to `false`."],
scope = CommandLine.ScopeType.INHERIT
)
var excludeExternalTargets = false

@CommandLine.Spec
lateinit var spec: CommandLine.Model.CommandSpec

Expand All @@ -169,6 +177,7 @@ class GenerateHashesCommand : Callable<Int> {
keepGoing,
depsMappingJSONPath != null,
fineGrainedHashExternalRepos,
excludeExternalTargets,
),
loggingModule(parent.verbose),
serialisationModule(),
Expand Down
3 changes: 2 additions & 1 deletion cli/src/main/kotlin/com/bazel_diff/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fun hasherModule(
keepGoing: Boolean,
trackDeps: Boolean,
fineGrainedHashExternalRepos: Set<String>,
excludeExternalTargets: Boolean,
): Module = module {
val cmd: MutableList<String> = ArrayList<String>().apply {
add(bazelPath.toString())
Expand Down Expand Up @@ -59,7 +60,7 @@ fun hasherModule(
debug
)
}
single { BazelClient(useCquery, fineGrainedHashExternalRepos) }
single { BazelClient(useCquery, fineGrainedHashExternalRepos, excludeExternalTargets) }
single { BuildGraphHasher(get()) }
single { TargetHasher() }
single { RuleHasher(useCquery, trackDeps, fineGrainedHashExternalRepos) }
Expand Down
2 changes: 1 addition & 1 deletion cli/src/test/kotlin/com/bazel_diff/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun testModule(): Module = module {
val outputBase = Paths.get("output-base")
val workingDirectory = Paths.get("working-directory")
single<Logger> { SilentLogger }
single { BazelClient(false, emptySet()) }
single { BazelClient(false, emptySet(), false) }
single { BuildGraphHasher(get()) }
single { TargetHasher() }
single { RuleHasher(false, true, emptySet()) }
Expand Down

0 comments on commit bd71bc5

Please sign in to comment.