Skip to content

Commit

Permalink
[Gazelle] Fix Duplicate Load Bug (#1841)
Browse files Browse the repository at this point in the history
If two extensions both import Kinds from the same file, and no load
statement for those Kinds is already in the BUILD.bazel file, then
Gazelle will add the load statement twice.

In this commit, we fix the issue by updating the tracked loads when a
new load is inserted into the index. This will cause future iterations
to update the existing load instead of creating a duplicate.
  • Loading branch information
ckilian867 authored Jul 18, 2024
1 parent fe3043f commit 906a322
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions merger/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func FixLoads(f *rule.File, knownLoads []rule.LoadInfo) {
if load != nil {
index := newLoadIndex(f, known.After)
load.Insert(f, index)
loads = append(loads, load)
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions merger/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestFixLoads(t *testing.T) {
Symbols: []string{
"foo_binary",
"foo_library",
},
},
{
Name: "@foo",
Symbols: []string{
"foo_test",
},
},
Expand Down Expand Up @@ -173,8 +178,8 @@ foo_library(name = "a_lib")
name = "a",
)
`,
want: `load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@foo", "foo_binary")
want: `load("@foo", "foo_binary")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
maybe(
foo_binary,
Expand Down Expand Up @@ -227,7 +232,7 @@ selects.config_setting_group(
f.Sync()

want := strings.TrimSpace(tc.want)
got := strings.TrimSpace(string(bzl.Format(f.File)))
got := strings.TrimSpace(string(bzl.FormatWithoutRewriting(f.File)))
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("FixLoads() mismatch (-want +got):\n%s", diff)
}
Expand Down

0 comments on commit 906a322

Please sign in to comment.