bufbuild / rules_buf

Bazel rules for Buf.
Apache License 2.0
47 stars 17 forks source link

gazelle plugin always overwrites existing buf_breaking_test #41

Closed leungster closed 12 months ago

leungster commented 1 year ago

When in package mode, the gazelle plugin is always overwriting existing buf_breaking_test rules instead of merging them. In my case, I've set the size attribute on my rule but it gets removed on each invocation of gazelle.

There is a code path in generate.go to handle this case but the check is sensitive to the targets exactly matching the key in protoRuleMap.

The protoRuleMap is populated from a rule's name attribute, but the lookup is using another rule's targets attribute. The targets attribute is a set of labels so they are typically prepended with a colon (e.g. :protos vs protos). As a result, the targets never match a value in protoRuleMap and any existing buf_breaking_test rule is removed instead of merged.

Here's an example of my patch to work around this.

 gazelle/buf/generate.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gazelle/buf/generate.go b/gazelle/buf/generate.go
index 7766961..0f8aed9 100644
--- a/gazelle/buf/generate.go
+++ b/gazelle/buf/generate.go
@@ -133,7 +133,7 @@ func shouldRemoveSingleTargetBufRule(
        // Not generated by us
        return false
    }
-   if len(targets) == 1 && protoRuleMap[targets[0]] != nil {
+   if len(targets) == 1 && protoRuleMap[strings.TrimPrefix(targets[0], ":")] != nil {
        // Target is accurate so skip
        return false
    }