Skip to content

Commit

Permalink
Look in call args for loadable symbols
Browse files Browse the repository at this point in the history
Since bazelbuild#1310 we can add
arguments to rules - this PR allows symbols to be loaded for these
arguments.
  • Loading branch information
illicitonion committed Aug 16, 2022
1 parent 2b30bc4 commit ea0d5c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
24 changes: 17 additions & 7 deletions merger/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,30 @@ func FixLoads(f *rule.File, knownLoads []rule.LoadInfo) {
return
}

id, ok := ce.X.(*bzl.Ident)
functionIdent, ok := ce.X.(*bzl.Ident)
if !ok {
return
}

file, ok := knownSymbols[id.Name]
if !ok || otherLoadedKinds[id.Name] {
return
idents := []*bzl.Ident{functionIdent}

for _, arg := range ce.List {
if argIdent, ok := arg.(*bzl.Ident); ok {
idents = append(idents, argIdent)
}
}

if usedSymbols[file] == nil {
usedSymbols[file] = make(map[string]bool)
for _, id := range idents {
file, ok := knownSymbols[id.Name]
if !ok || otherLoadedKinds[id.Name] {
continue
}

if usedSymbols[file] == nil {
usedSymbols[file] = make(map[string]bool)
}
usedSymbols[file][id.Name] = true
}
usedSymbols[file][id.Name] = true
})

// Fix the load statements. The order is important, so we iterate over
Expand Down
21 changes: 21 additions & 0 deletions merger/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func TestFixLoads(t *testing.T) {
"foo_test",
},
},
{
Name: "@bazel_tools//tools/build_defs/repo:utils.bzl",
Symbols: []string{
"maybe",
},
},
}

type testCase struct {
Expand Down Expand Up @@ -153,6 +159,21 @@ foo_library(name = "a_lib")
foo_binary(name = "a")
foo_library(name = "a_lib")
`,
},
"missing wrapper and wrapped kind load symbol": {
input: `maybe(
foo_binary,
name = "a",
)
`,
want: `load("@foo", "foo_binary")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
maybe(
foo_binary,
name = "a",
)
`,
},
"unused kind load symbol": {
Expand Down

0 comments on commit ea0d5c3

Please sign in to comment.