Skip to content

Commit

Permalink
Gazelle: fix deletion of rules with deps (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayconrod authored Dec 13, 2017
1 parent dfcdede commit f74269b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
76 changes: 76 additions & 0 deletions go/tools/gazelle/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,82 @@ go_library(
})
}

// TestDeleteProtoWithDeps checks that Gazelle will delete proto rules with
// dependencies after the proto sources are removed.
func TestDeleteProtoWithDeps(t *testing.T) {
files := []fileSpec{
{path: "WORKSPACE"},
{
path: "foo/BUILD.bazel",
content: `
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["extra.go"],
embed = [":scratch_go_proto"],
importpath = "example.com/repo/foo",
visibility = ["//visibility:public"],
)
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
deps = ["//foo/bar:bar_proto"],
)
go_proto_library(
name = "foo_go_proto",
importpath = "example.com/repo/foo",
proto = ":foo_proto",
visibility = ["//visibility:public"],
deps = ["//foo/bar:go_default_library"],
)
`,
}, {
path: "foo/extra.go",
content: "package foo",
}, {
path: "foo/bar/bar.proto",
content: `
syntax = "proto3";
option go_package = "example.com/repo/foo/bar";
message Bar {};
`,
},
}
dir, err := createFiles(files)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

args := []string{"-go_prefix", "example.com/repo"}
if err := runGazelle(dir, args); err != nil {
t.Fatal(err)
}

checkFiles(t, dir, []fileSpec{
{
path: "foo/BUILD.bazel",
content: `
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["extra.go"],
importpath = "example.com/repo/foo",
visibility = ["//visibility:public"],
)
`,
},
})
}

// TODO(jayconrod): more tests
// run in fix mode in testdata directories to create new files
// run in diff mode in testdata directories to update existing files (no change)
3 changes: 2 additions & 1 deletion go/tools/gazelle/gazelle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func run(c *config.Config, cmd command, emit emitFunc) {
visits = append(visits, visitRecord{
pkgRel: rel,
rules: rules,
empty: empty,
file: file,
})
}
Expand All @@ -145,7 +146,7 @@ func run(c *config.Config, cmd command, emit emitFunc) {
for j := range visits[i].rules {
visits[i].rules[j] = resolver.ResolveRule(visits[i].rules[j], visits[i].pkgRel)
}
visits[i].file, _ = merger.MergeFile(visits[i].rules, nil, visits[i].file, merger.MergeableResolvedAttrs)
visits[i].file, _ = merger.MergeFile(visits[i].rules, visits[i].empty, visits[i].file, merger.MergeableResolvedAttrs)
}

// Emit merged files.
Expand Down

0 comments on commit f74269b

Please sign in to comment.