Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ kt_jvm_test(
runtime_deps = [":cli-test-lib"],
)

kt_jvm_test(
name = "BazelRuleTest",
test_class = "com.bazel_diff.bazel.BazelRuleTest",
runtime_deps = [":cli-test-lib"],
)

kt_jvm_test(
name = "E2ETest",
test_class = "com.bazel_diff.e2e.E2ETest",
Expand Down
8 changes: 7 additions & 1 deletion cli/src/main/kotlin/com/bazel_diff/bazel/BazelRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import com.bazel_diff.hash.safePutBytes
import com.bazel_diff.hash.sha256
import com.google.devtools.build.lib.query2.proto.proto2api.Build

// Ignore generator_location when computing a target's hash since it is likely to change and does not
// affect a target's generated actions. Internally, Bazel also does this when computing a target's hash:
// https://github.com/bazelbuild/bazel/blob/6971b016f1e258e3bb567a0f9fe7a88ad565d8f2/src/main/java/com/google/devtools/build/lib/query2/query/output/SyntheticAttributeHashCalculator.java#L78-L81
private val IGNORED_ATTRS = arrayOf("generator_location")

class BazelRule(private val rule: Build.Rule) {
val digest: ByteArray by lazy {
sha256 {
safePutBytes(rule.ruleClassBytes.toByteArray())
safePutBytes(rule.nameBytes.toByteArray())
safePutBytes(rule.skylarkEnvironmentHashCodeBytes.toByteArray())
for (attribute in rule.attributeList) {
safePutBytes(attribute.toByteArray())
if (!IGNORED_ATTRS.contains(attribute.name))
safePutBytes(attribute.toByteArray())
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions cli/src/test/kotlin/com/bazel_diff/bazel/BazelRuleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.bazel_diff.bazel

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotEqualTo
import com.google.devtools.build.lib.query2.proto.proto2api.Build.Rule
import com.google.devtools.build.lib.query2.proto.proto2api.Build.Attribute
import org.junit.Test

class BazelRuleTest {
@Test
fun testHashDiffers() {
val rule1Pb = Rule.newBuilder()
.setRuleClass("java_library")
.setName("libfoo")
.build()

val rule2Pb = Rule.newBuilder()
.setRuleClass("java_library")
.setName("libbar")
.build()
assertThat(BazelRule(rule1Pb).digest).isNotEqualTo(BazelRule(rule2Pb).digest)
}
@Test
fun testIgnoreAttributes() {
val rule1Pb = Rule.newBuilder()
.setRuleClass("java_library")
.setName("foo_library")
.addAttribute(0, Attribute.newBuilder()
.setType(Attribute.Discriminator.STRING)
.setName("generator_location")
.setStringValue("path/to/BUILD:107:12").build())
.build()

val rule2Pb = Rule.newBuilder()
.setRuleClass("java_library")
.setName("foo_library")
.addAttribute(0, Attribute.newBuilder()
.setType(Attribute.Discriminator.STRING)
.setName("generator_location")
.setStringValue("path/to/BUILD:111:1").build())
.build()

assertThat(BazelRule(rule1Pb).digest).isEqualTo(BazelRule(rule2Pb).digest)
}
}