Skip to content

Commit

Permalink
Fix map_kind with empty rules (#1441)
Browse files Browse the repository at this point in the history
* fix: language/go: fileContainsGoBinary() doesn't interprete correctly KindMap

With this directive `# gazelle:map_kind OLD NEW NEW_LOAD`,
c.KindMap should be equal:

  "OLD": {
    FromKind: "OLD",
    KindName: "NEW",
    KindLoad: "NEW_LOAD",
  }

`fileContainsGoBinary()` should search directly the mapping
for the `go_binary` kind rule.

Issue #1440

* fix: map_kind should also be applied on empty rules

If `map_kind` is not applied on empty rules returned
by GenerateRules(), all mapped rules can't be removed
from the BUILD file.

Close #1440
  • Loading branch information
PingWriter authored Sep 13, 2023
1 parent de70ddd commit 8522970
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
r.SetKind(repl.KindName)
}
}
for _, r := range empty {
if repl, ok := c.KindMap[r.Kind()]; ok {
mappedKindInfo[repl.KindName] = kinds[r.Kind()]
mappedKinds = append(mappedKinds, repl)
mrslv.MappedKind(rel, repl)
r.SetKind(repl.KindName)
}
}

// Insert or merge rules into the build file.
if f == nil {
Expand Down
56 changes: 56 additions & 0 deletions cmd/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,47 @@ go_library(
Path: "enabled/existing_rules/unmapped/unmapped_lib.go",
Content: `package unmapped`,
},
{
Path: "enabled/existing_rules/clean/BUILD.bazel",
Content: `load("//tools/go:def.bzl", "my_go_library")
# gazelle:go_naming_convention import
# gazelle:map_kind go_library my_go_library //tools/go:def.bzl
my_go_library(
name = "clean",
srcs = [
"foo.go",
"bar.go",
"buz.go",
],
importpath = "example.com/mapkind/enabled/existing_rules/clean",
visibility = ["//visibility:private"],
)
`,
},
{
Path: "enabled/existing_rules/clean_binary/BUILD.bazel",
Content: `load("//tools/go:def.bzl", "my_go_binary", "my_go_library")
# gazelle:go_naming_convention import
# gazelle:map_kind go_binary my_go_binary //tools/go:def.bzl
# gazelle:map_kind go_library my_go_library //tools/go:def.bzl
my_go_library(
name = "clean_binary_lib",
srcs = ["main.go"],
importpath = "example.com/mapkind/enabled/existing_rules/clean_binary",
visibility = ["//visibility:private"],
)
my_go_binary(
name = "clean_binary",
embed = [":clean_binary_lib"],
visibility = ["//visibility:public"],
)
`,
},
{
Path: "enabled/existing_rules/nobuild/nobuild_lib.go",
Content: `package nobuild`,
Expand Down Expand Up @@ -2124,6 +2165,21 @@ my_library(
importpath = "example.com/mapkind/enabled/existing_rules/unmapped",
visibility = ["//visibility:public"],
)
`,
},
{
Path: "enabled/existing_rules/clean/BUILD.bazel",
Content: `
# gazelle:go_naming_convention import
# gazelle:map_kind go_library my_go_library //tools/go:def.bzl
`,
},
{
Path: "enabled/existing_rules/clean_binary/BUILD.bazel",
Content: `
# gazelle:go_naming_convention import
# gazelle:map_kind go_binary my_go_binary //tools/go:def.bzl
# gazelle:map_kind go_library my_go_library //tools/go:def.bzl
`,
},
{
Expand Down
9 changes: 6 additions & 3 deletions language/go/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ func fileContainsGoBinary(c *config.Config, f *rule.File) bool {
}
for _, r := range f.Rules {
kind := r.Kind()
if mappedKind, ok := c.KindMap[kind]; ok {
kind = mappedKind.KindName
}
if kind == "go_binary" {
return true
}

if mappedKind, ok := c.KindMap["go_binary"]; ok {
if mappedKind.KindName == kind {
return true
}
}
}
return false
}
Expand Down

0 comments on commit 8522970

Please sign in to comment.