Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overwrite attribute values when not mergeable #1952

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 21 additions & 4 deletions merger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ const UnstableInsertIndexKey = "_gazelle_insert_index"
// be modified.
func MergeFile(oldFile *rule.File, emptyRules, genRules []*rule.Rule, phase Phase, kinds map[string]rule.KindInfo, aliasedKinds map[string]string) {
getMergeAttrs := func(r *rule.Rule) map[string]bool {
if phase == PreResolve {
return kinds[r.Kind()].MergeableAttrs
} else {
return kinds[r.Kind()].ResolveAttrs
kind := kinds[r.Kind()]

mergeableAttrs := map[string]bool{
// Name and visibility are uniquely managed by gazelle.
"name": false,
"visibility": false,
}

// All attributes managed by gazelle, marking only the current merge attributes
// with a truthy value.
combineMergeAttrs(mergeableAttrs, kind.MergeableAttrs, phase == PreResolve)
combineMergeAttrs(mergeableAttrs, kind.ResolveAttrs, phase == PostResolve)
combineMergeAttrs(mergeableAttrs, kind.NonEmptyAttrs, false)
combineMergeAttrs(mergeableAttrs, kind.SubstituteAttrs, false)

return mergeableAttrs
}

// Merge empty rules into the file and delete any rules which become empty.
Expand Down Expand Up @@ -174,6 +185,12 @@ func MergeFile(oldFile *rule.File, emptyRules, genRules []*rule.Rule, phase Phas
}
}

func combineMergeAttrs(dst map[string]bool, src map[string]bool, value bool) {
for k, _ := range src {
dst[k] = value || dst[k]
}
}

// substituteRule replaces local labels (those beginning with ":", referring to
// targets in the same package) according to a substitution map. This is used
// to update generated rules before merging when the corresponding existing
Expand Down
22 changes: 20 additions & 2 deletions rule/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import (
// marked with a "# keep" comment, values in the attribute not marked with
// a "# keep" comment will be dropped. If the attribute is empty afterward,
// it will be deleted.
//
// If src has an attribute not present in 'mergeable' and the attribute
// does not exist or is not marked with a "# keep" comment, values in the
// dst attribute will be overwritten.
func MergeRules(src, dst *Rule, mergeable map[string]bool, filename string) {
if dst.ShouldKeep() {
return
Expand All @@ -63,9 +67,20 @@ func MergeRules(src, dst *Rule, mergeable map[string]bool, filename string) {

// Merge attributes from src into dst.
for key, srcAttr := range src.attrs {
if dstAttr, ok := dst.attrs[key]; !ok {
// Always set properties that are not yet set.
dstAttr, dstAttrExists := dst.attrs[key]
if !dstAttrExists {
dst.SetAttr(key, srcAttr.expr.RHS)
} else if mergeable[key] && !ShouldKeep(dstAttr.expr) {
continue
}

// Do not overwrite attributes marked with "# keep".
if ShouldKeep(dstAttr.expr) {
continue
}

if attrMergeable, attrKnown := mergeable[key]; attrMergeable {
// Merge mergeable attributes.
if mergedValue, err := mergeAttrValues(&srcAttr, &dstAttr); err != nil {
start, end := dstAttr.expr.RHS.Span()
log.Printf("%s:%d.%d-%d.%d: could not merge expression", filename, start.Line, start.LineRune, end.Line, end.LineRune)
Expand All @@ -74,6 +89,9 @@ func MergeRules(src, dst *Rule, mergeable map[string]bool, filename string) {
} else {
dst.SetAttr(key, mergedValue)
}
} else if !attrKnown {
// Overwrite unknown attributes.
dst.SetAttr(key, srcAttr.expr.RHS)
}
}

Expand Down
65 changes: 63 additions & 2 deletions rule/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package rule_test

import (
"reflect"
"testing"

"github.com/bazelbuild/bazel-gazelle/rule"
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestMergeRules_WithSortedStringAttr(t *testing.T) {
sortedStringAttrVal := rule.SortedStrings{"@qux", "//foo:bar", "//foo:baz"}
src.SetAttr(sortedStringAttrKey, sortedStringAttrVal)
dst := rule.NewRule("go_library", "go_default_library")
rule.MergeRules(src, dst, map[string]bool{}, "")
rule.MergeRules(src, dst, map[string]bool{"deps": true}, "")

valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr)
if !ok {
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestMergeRules_WithUnsortedStringAttr(t *testing.T) {
sortedStringAttrVal := rule.UnsortedStrings{"@qux", "//foo:bar", "//foo:baz"}
src.SetAttr(sortedStringAttrKey, sortedStringAttrVal)
dst := rule.NewRule("go_library", "go_default_library")
rule.MergeRules(src, dst, map[string]bool{}, "")
rule.MergeRules(src, dst, map[string]bool{"deps": true}, "")

valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr)
if !ok {
Expand Down Expand Up @@ -147,3 +148,63 @@ func TestMergeRules_WithUnsortedStringAttr(t *testing.T) {
}
})
}

func TestMergeRules_WithUnmanagedAttr(t *testing.T) {
t.Run("unknown string attributes overwrite attributes in non-empty rule", func(t *testing.T) {
src := rule.NewRule("go_library", "go_default_library")
unknownStringAttrKey := "unknown_attr"
overwrittenAttrVal := rule.UnsortedStrings{"@qux", "//foo:bar", "//foo:baz"}
src.SetAttr(unknownStringAttrKey, "foobar")
dst := rule.NewRule("go_library", "go_default_library")
dst.SetAttr(unknownStringAttrKey, overwrittenAttrVal)
rule.MergeRules(src, dst, map[string]bool{}, "")

valExpr, ok := dst.Attr(unknownStringAttrKey).(*bzl.StringExpr)
if !ok {
t.Fatalf("unknown attributes (%q) are overwritten: got %v; want *bzl.StringExpr",
unknownStringAttrKey, reflect.TypeOf(dst.Attr(unknownStringAttrKey)))
}

expected := "foobar"
if valExpr.Value != expected {
t.Fatalf("unknown attributes (%q) are overwritten: got %v; want %v",
unknownStringAttrKey, valExpr.Value, expected)
}
})

t.Run("known string attributes do NOT overwrite attributes in non-empty rule", func(t *testing.T) {
src := rule.NewRule("go_library", "go_default_library")
unknownStringAttrKey := "unknown_attr"
knownStringAttrKey := "knownAttrName"
overwrittenAttrVal := "original"
src.SetAttr(unknownStringAttrKey, "foobar")
src.SetAttr(knownStringAttrKey, "foobar")
dst := rule.NewRule("go_library", "go_default_library")
dst.SetAttr(unknownStringAttrKey, overwrittenAttrVal)
dst.SetAttr(knownStringAttrKey, overwrittenAttrVal)
rule.MergeRules(src, dst, map[string]bool{knownStringAttrKey: false}, "")

// The unknown + overwritten attribute
unknownValExpr, ok := dst.Attr(unknownStringAttrKey).(*bzl.StringExpr)
if !ok {
t.Fatalf("unknown attributes (%q) are overwritten: got %v; want *bzl.StringExpr",
unknownStringAttrKey, reflect.TypeOf(dst.Attr(unknownStringAttrKey)))
}
expected := "foobar"
if unknownValExpr.Value != expected {
t.Fatalf("unknown attributes (%q) are overwritten: got %v; want %v",
unknownStringAttrKey, unknownValExpr.Value, expected)
}

// The known but non-mergeable attributes
knownValExpr, ok := dst.Attr(knownStringAttrKey).(*bzl.StringExpr)
if !ok {
t.Fatalf("known attributes (%q) are NOT overwritten: got %v; want *bzl.StringExpr",
knownStringAttrKey, reflect.TypeOf(dst.Attr(knownStringAttrKey)))
}
if knownValExpr.Value != overwrittenAttrVal {
t.Fatalf("known attributes (%q) are NOT overwritten: got %v; want %v",
knownStringAttrKey, knownValExpr.Value, overwrittenAttrVal)
}
})
}