Skip to content

Commit 8c6cbf7

Browse files
Fixes for external repo resolver and sorting in impatcted set
These changes make sure we solve for bzlModRelativePath correctly and also improves output of the tool by sorting outputs
1 parent aaea306 commit 8c6cbf7

5 files changed

Lines changed: 58 additions & 3 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module(
22
name = "bazel-diff",
3-
version = "18.1.0",
3+
version = "19.0.0",
44
compatibility_level = 0,
55
)
66

MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/main/kotlin/com/bazel_diff/hash/ExternalRepoResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ExternalRepoResolver(
6666
return externalRoot.resolve(repoName)
6767
}
6868
val path = Paths.get(queryResultLine.split(": ", limit = 2)[0])
69-
val bzlModRelativePath = path.relativize(externalRoot).first()
69+
val bzlModRelativePath = externalRoot.relativize(path).first()
7070
return externalRoot.resolve(bzlModRelativePath)
7171
}
7272

cli/src/main/kotlin/com/bazel_diff/interactor/CalculateImpactedTargetsInteractor.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CalculateImpactedTargetsInteractor : KoinComponent {
5959

6060
impactedTargets
6161
.filter { typeFilter.accepts(it) }
62+
.sortedWith(impactedTargetOrdering(to, from))
6263
.let { filtered ->
6364
outputWriter.use { writer -> filtered.forEach { writer.write("$it\n") } }
6465
}
@@ -110,8 +111,10 @@ class CalculateImpactedTargetsInteractor : KoinComponent {
110111
computeAllDistances(from, to, depEdges)
111112
}
112113

114+
val ordering = impactedTargetOrdering(to, from)
113115
impactedTargets
114116
.filterKeys { typeFilter.accepts(it) }
117+
.toSortedMap(ordering)
115118
.let { filtered ->
116119
outputWriter.use { writer ->
117120
writer.write(
@@ -126,6 +129,24 @@ class CalculateImpactedTargetsInteractor : KoinComponent {
126129
}
127130
}
128131

132+
private fun impactedTargetOrdering(
133+
to: Map<String, TargetHash>,
134+
from: Map<String, TargetHash>
135+
): Comparator<String> {
136+
fun kindRank(label: String): Int {
137+
val type = to[label]?.type?.takeIf { it.isNotEmpty() }
138+
?: from[label]?.type?.takeIf { it.isNotEmpty() }
139+
return when (type) {
140+
"SourceFile" -> 0
141+
"GeneratedFile" -> 1
142+
"Rule" -> 2
143+
null -> 4
144+
else -> 3
145+
}
146+
}
147+
return compareBy<String>({ kindRank(it) }, { it })
148+
}
149+
129150
fun computeAllDistances(
130151
from: Map<String, TargetHash>,
131152
to: Map<String, TargetHash>,

cli/src/test/kotlin/com/bazel_diff/interactor/CalculateImpactedTargetsInteractorTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ class CalculateImpactedTargetsInteractorTest : KoinTest {
3434
assertThat(impacted).containsExactlyInAnyOrder("1", "3")
3535
}
3636

37+
@Test
38+
fun testExecuteSortsByKindThenLabel() {
39+
val startHashes =
40+
mapOf(
41+
"//pkg:rule_a" to TargetHash("Rule", "r_a", "r_a"),
42+
"//pkg:rule_b" to TargetHash("Rule", "r_b", "r_b"),
43+
"//pkg:gen_a" to TargetHash("GeneratedFile", "g_a", "g_a"),
44+
"//pkg:gen_b" to TargetHash("GeneratedFile", "g_b", "g_b"),
45+
"//pkg:src_a" to TargetHash("SourceFile", "s_a", "s_a"),
46+
"//pkg:src_b" to TargetHash("SourceFile", "s_b", "s_b"),
47+
)
48+
val endHashes = startHashes.mapValues { (_, v) -> v.copy(hash = v.hash + "-changed") }
49+
50+
val outputWriter = StringWriter()
51+
CalculateImpactedTargetsInteractor()
52+
.execute(
53+
from = startHashes,
54+
to = endHashes,
55+
outputWriter = outputWriter,
56+
targetTypes = null,
57+
)
58+
59+
val lines = outputWriter.toString().trimEnd('\n').split("\n")
60+
assertThat(lines)
61+
.containsExactly(
62+
"//pkg:src_a",
63+
"//pkg:src_b",
64+
"//pkg:gen_a",
65+
"//pkg:gen_b",
66+
"//pkg:rule_a",
67+
"//pkg:rule_b",
68+
)
69+
}
70+
3771
@Test
3872
fun testOmitsUnchangedTargets() {
3973
val (depEdges, startHashes) =

0 commit comments

Comments
 (0)