Skip to content

Commit b4e1a07

Browse files
authored
Ignore generator_location, add test (#142)
1 parent ce8cc08 commit b4e1a07

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

cli/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ kt_jvm_test(
9292
runtime_deps = [":cli-test-lib"],
9393
)
9494

95+
kt_jvm_test(
96+
name = "BazelRuleTest",
97+
test_class = "com.bazel_diff.bazel.BazelRuleTest",
98+
runtime_deps = [":cli-test-lib"],
99+
)
100+
95101
kt_jvm_test(
96102
name = "E2ETest",
97103
test_class = "com.bazel_diff.e2e.E2ETest",

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import com.bazel_diff.hash.safePutBytes
44
import com.bazel_diff.hash.sha256
55
import com.google.devtools.build.lib.query2.proto.proto2api.Build
66

7+
// Ignore generator_location when computing a target's hash since it is likely to change and does not
8+
// affect a target's generated actions. Internally, Bazel also does this when computing a target's hash:
9+
// https://github.com/bazelbuild/bazel/blob/6971b016f1e258e3bb567a0f9fe7a88ad565d8f2/src/main/java/com/google/devtools/build/lib/query2/query/output/SyntheticAttributeHashCalculator.java#L78-L81
10+
private val IGNORED_ATTRS = arrayOf("generator_location")
11+
712
class BazelRule(private val rule: Build.Rule) {
813
val digest: ByteArray by lazy {
914
sha256 {
1015
safePutBytes(rule.ruleClassBytes.toByteArray())
1116
safePutBytes(rule.nameBytes.toByteArray())
1217
safePutBytes(rule.skylarkEnvironmentHashCodeBytes.toByteArray())
1318
for (attribute in rule.attributeList) {
14-
safePutBytes(attribute.toByteArray())
19+
if (!IGNORED_ATTRS.contains(attribute.name))
20+
safePutBytes(attribute.toByteArray())
1521
}
1622
}
1723
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.bazel_diff.bazel
2+
3+
import assertk.assertThat
4+
import assertk.assertions.isEqualTo
5+
import assertk.assertions.isNotEqualTo
6+
import com.google.devtools.build.lib.query2.proto.proto2api.Build.Rule
7+
import com.google.devtools.build.lib.query2.proto.proto2api.Build.Attribute
8+
import org.junit.Test
9+
10+
class BazelRuleTest {
11+
@Test
12+
fun testHashDiffers() {
13+
val rule1Pb = Rule.newBuilder()
14+
.setRuleClass("java_library")
15+
.setName("libfoo")
16+
.build()
17+
18+
val rule2Pb = Rule.newBuilder()
19+
.setRuleClass("java_library")
20+
.setName("libbar")
21+
.build()
22+
assertThat(BazelRule(rule1Pb).digest).isNotEqualTo(BazelRule(rule2Pb).digest)
23+
}
24+
@Test
25+
fun testIgnoreAttributes() {
26+
val rule1Pb = Rule.newBuilder()
27+
.setRuleClass("java_library")
28+
.setName("foo_library")
29+
.addAttribute(0, Attribute.newBuilder()
30+
.setType(Attribute.Discriminator.STRING)
31+
.setName("generator_location")
32+
.setStringValue("path/to/BUILD:107:12").build())
33+
.build()
34+
35+
val rule2Pb = Rule.newBuilder()
36+
.setRuleClass("java_library")
37+
.setName("foo_library")
38+
.addAttribute(0, Attribute.newBuilder()
39+
.setType(Attribute.Discriminator.STRING)
40+
.setName("generator_location")
41+
.setStringValue("path/to/BUILD:111:1").build())
42+
.build()
43+
44+
assertThat(BazelRule(rule1Pb).digest).isEqualTo(BazelRule(rule2Pb).digest)
45+
}
46+
}

0 commit comments

Comments
 (0)