Skip to content

Commit

Permalink
fix: Fix more canonical names
Browse files Browse the repository at this point in the history
  • Loading branch information
honnix committed Oct 2, 2024
1 parent 8d85ffd commit 372f1f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.bazel_diff.bazel

import com.bazel_diff.log.Logger
import com.google.devtools.build.lib.query2.proto.proto2api.Build
import java.nio.file.Path
import java.util.*
import org.koin.core.component.inject
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 {
private val logger: Logger by inject()
Expand Down
11 changes: 10 additions & 1 deletion cli/src/main/kotlin/com/bazel_diff/bazel/BazelQueryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BazelQueryService(
val targets = outputFile.inputStream().buffered().use { proto ->
if (useCquery) {
val cqueryResult = AnalysisProtosV2.CqueryResult.parseFrom(proto)
cqueryResult.resultsList.filter { it.target.rule.name in compatibleTargetSet }.map { it.target }
cqueryResult.resultsList.filter { inCompatibleTargetSet(it, compatibleTargetSet) }.map { it.target }
} else {
mutableListOf<Build.Target>().apply {
while (true) {
Expand All @@ -60,6 +60,15 @@ class BazelQueryService(
return targets
}

private fun inCompatibleTargetSet(
target: AnalysisProtosV2.ConfiguredTarget,
compatibleTargetSet: Set<String>
): Boolean {
val name = target.target.rule.name
return name in compatibleTargetSet ||
name.startsWith("@") && !name.startsWith("@@") && "@${name}" in compatibleTargetSet
}

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun runQuery(
query: String,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/kotlin/com/bazel_diff/bazel/BazelRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class BazelRule(private val rule: Build.Rule) {
val name: String = rule.name

private fun transformRuleInput(fineGrainedHashExternalRepos: Set<String>, ruleInput: String): String {
if (ruleInput.startsWith("@") && fineGrainedHashExternalRepos.none { ruleInput.startsWith("@$it") }) {
if (ruleInput.startsWith("@") && fineGrainedHashExternalRepos.none { ruleInput.startsWith("@$it") || ruleInput.startsWith("@@${it}") }) {
val splitRule = ruleInput.split("//".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (splitRule.size == 2) {
var externalRule = splitRule[0]
externalRule = externalRule.replaceFirst("@".toRegex(), "")
externalRule = externalRule.replaceFirst("@+".toRegex(), "")
return String.format("//external:%s", externalRule)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class ExternalRepoResolver(
return it.readLine()
}
}
}
}
6 changes: 5 additions & 1 deletion cli/src/main/kotlin/com/bazel_diff/hash/SourceFileHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ class SourceFileHasher : KoinComponent {
val filenameSubstring = name.substring(2)
Paths.get(filenameSubstring.removePrefix(":").replace(':', '/'))
} else if (name.startsWith("@")) {
val parts = name.substring(1).split("//")
val parts = if (name.startsWith("@@")) {
name.substring(2).split("//")
} else {
name.substring(1).split("//")
}
if (parts.size != 2) {
logger.w { "Invalid source label $name" }
return@sha256
Expand Down

0 comments on commit 372f1f2

Please sign in to comment.