From f74269b27df3707bd05215fc40704477db85f654 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Wed, 13 Dec 2017 12:45:20 -0500 Subject: [PATCH] Gazelle: fix deletion of rules with deps (#1130) Fixes bazelbuild/bazel-gazelle#23 --- go/tools/gazelle/gazelle/integration_test.go | 76 ++++++++++++++++++++ go/tools/gazelle/gazelle/main.go | 3 +- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/go/tools/gazelle/gazelle/integration_test.go b/go/tools/gazelle/gazelle/integration_test.go index d94d638a74..0c4683d920 100644 --- a/go/tools/gazelle/gazelle/integration_test.go +++ b/go/tools/gazelle/gazelle/integration_test.go @@ -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) diff --git a/go/tools/gazelle/gazelle/main.go b/go/tools/gazelle/gazelle/main.go index 962c021ac5..aece703403 100644 --- a/go/tools/gazelle/gazelle/main.go +++ b/go/tools/gazelle/gazelle/main.go @@ -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, }) } @@ -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.