Skip to content

Commit

Permalink
gopackagesdriver: Make kind query regexes anchored (#3978)
Browse files Browse the repository at this point in the history
A query like kind("go_library", ...) also returns rules that are not go 
libraries but have go_library as a substring of the rule name, e.g. 
not_a_go_library. To fix that, add regex anchors.
  • Loading branch information
jscissr committed Jul 2, 2024
1 parent 77e04cc commit 634fc28
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (b *BazelJSONBuilder) fileQuery(filename string) string {
}

kinds := append(_defaultKinds, additionalKinds...)
return fmt.Sprintf(`kind("%s", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
return fmt.Sprintf(`kind("^(%s) rule$", same_pkg_direct_rdeps("%s"))`, strings.Join(kinds, "|"), label)
}

func (b *BazelJSONBuilder) getKind() string {
Expand All @@ -104,7 +104,7 @@ func (b *BazelJSONBuilder) localQuery(request string) string {
request = fmt.Sprintf("%s:*", request)
}

return fmt.Sprintf(`kind("%s", %s)`, b.getKind(), request)
return fmt.Sprintf(`kind("^(%s) rule$", %s)`, b.getKind(), request)
}

func (b *BazelJSONBuilder) packageQuery(importPath string) string {
Expand All @@ -113,7 +113,7 @@ func (b *BazelJSONBuilder) packageQuery(importPath string) string {
}

return fmt.Sprintf(
`kind("%s", attr(importpath, "%s", deps(%s)))`,
`kind("^(%s) rule$", attr(importpath, "%s", deps(%s)))`,
b.getKind(),
importPath,
bazelQueryScope)
Expand Down

0 comments on commit 634fc28

Please sign in to comment.