Skip to content

Commit

Permalink
Revert "Allow MatchAttrs to be slices (bazel-contrib#730)" (bazel-con…
Browse files Browse the repository at this point in the history
…trib#743)

This reverts commit 8be1e16.
  • Loading branch information
Jay Conrod authored Mar 24, 2020
1 parent f38855f commit c7d8513
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 102 deletions.
69 changes: 0 additions & 69 deletions cmd/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2975,72 +2975,3 @@ go_repository(
},
})
}

func TestMatchProtoLibrary(t *testing.T) {
files := []testtools.FileSpec{
{
Path: "WORKSPACE",
},
{
Path: "proto/BUILD.bazel",
Content: `
load("@rules_proto//proto:defs.bzl", "proto_library")
# gazelle:prefix example.com/foo
some_rule(
name = "gen_proto",
out = "foo.proto",
)
proto_library(
name = "existing_proto",
srcs = ["foo.proto"],
)
`,
},
}
dir, cleanup := testtools.CreateFiles(t, files)
defer cleanup()

args := []string{"update"}
if err := runGazelle(dir, args); err != nil {
t.Fatal(err)
}

testtools.CheckFiles(t, dir, []testtools.FileSpec{
{
Path: "proto/BUILD.bazel",
Content: `
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
# gazelle:prefix example.com/foo
some_rule(
name = "gen_proto",
out = "foo.proto",
)
proto_library(
name = "existing_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "proto_go_proto",
importpath = "example.com/foo",
proto = ":existing_proto",
visibility = ["//visibility:public"],
)
go_library(
name = "go_default_library",
embed = [":proto_go_proto"],
importpath = "example.com/foo",
visibility = ["//visibility:public"],
)
`,
},
})
}
1 change: 0 additions & 1 deletion language/proto/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import "github.com/bazelbuild/bazel-gazelle/rule"

var protoKinds = map[string]rule.KindInfo{
"proto_library": {
MatchAttrs: []string{"srcs"},
NonEmptyAttrs: map[string]bool{"srcs": true},
MergeableAttrs: map[string]bool{
"srcs": true,
Expand Down
29 changes: 6 additions & 23 deletions merger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ package merger

import (
"fmt"
"sort"
"strings"

"github.com/bazelbuild/bazel-gazelle/rule"
Expand Down Expand Up @@ -223,15 +222,19 @@ func Match(rules []*rule.Rule, x *rule.Rule, info rule.KindInfo) (*rule.Rule, er

for _, key := range info.MatchAttrs {
var attrMatches []*rule.Rule
xvalue := x.AttrString(key)
if xvalue == "" {
continue
}
for _, y := range kindMatches {
if attrMatch(x, y, key) {
if xvalue == y.AttrString(key) {
attrMatches = append(attrMatches, y)
}
}
if len(attrMatches) == 1 {
return attrMatches[0], nil
} else if len(attrMatches) > 1 {
return nil, fmt.Errorf("could not merge %s(%s): multiple rules have the same attribute %s", xkind, xname, key)
return nil, fmt.Errorf("could not merge %s(%s): multiple rules have the same attribute %s = %q", xkind, xname, key, xvalue)
}
}

Expand All @@ -245,23 +248,3 @@ func Match(rules []*rule.Rule, x *rule.Rule, info rule.KindInfo) (*rule.Rule, er

return nil, nil
}

func attrMatch(x, y *rule.Rule, key string) bool {
xValue := x.AttrString(key)
if xValue != "" && xValue == y.AttrString(key) {
return true
}
xValues := x.AttrStrings(key)
yValues := y.AttrStrings(key)
if xValues == nil || yValues == nil || len(xValues) != len(yValues) {
return false
}
sort.Strings(xValues)
sort.Strings(yValues)
for i, v := range xValues {
if v != yValues[i] {
return false
}
}
return true
}
9 changes: 0 additions & 9 deletions merger/merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,15 +969,6 @@ go_binary(name = "y")
go_binary(name = "z")
`,
wantError: true,
}, {
desc: "srcs match",
gen: `proto_library(name = "proto1", srcs = ["foo.proto", "bar.proto"])`,
old: `proto_library(name = "proto2", srcs = ["bar.proto", "foo.proto"])`,
wantIndex: 0,
}, {
desc: "importpath match",
gen: `go_proto_library(name = "go_proto1", importpath="example.com/foo")`,
old: `go_proto_library(name = "go_proto2", importpath="example.com/foo")`,
},
} {
t.Run(tc.desc, func(t *testing.T) {
Expand Down

0 comments on commit c7d8513

Please sign in to comment.