Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover missing cases during module extension label normalization #1219

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion edit/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ go_library(
srcs = ["bzlmod.go"],
importpath = "github.com/bazelbuild/buildtools/edit/bzlmod",
visibility = ["//visibility:public"],
deps = ["//build"],
deps = [
"//build",
"//labels",
],
)

go_test(
Expand Down
28 changes: 13 additions & 15 deletions edit/bzlmod/bzlmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ limitations under the License.
package bzlmod

import (
"strings"

"github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/buildtools/labels"
)

// Proxies returns the names of extension proxies (i.e. the names of variables to which the result
Expand Down Expand Up @@ -225,19 +224,18 @@ func getApparentModuleName(f *build.File) string {

// normalizeLabelString converts a label string into the form @apparent_name//path/to:target.
func normalizeLabelString(rawLabel, apparentModuleName string) string {
// This implements
// https:/bazelbuild/bazel/blob/dd822392db96bb7bccdb673414a20c4b91e3dbc1/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java#L416
// with the assumption that the current module is the root module.
if strings.HasPrefix(rawLabel, "//") {
// Relative labels always refer to the current module.
return "@" + apparentModuleName + rawLabel
} else if strings.HasPrefix(rawLabel, "@//") {
// In the root module only, this syntax refer to the module. Since we are inspecting its
// module file as a tool, we can assume that the current module is the root module.
return "@" + apparentModuleName + rawLabel[1:]
} else {
return rawLabel
}
label := labels.ParseRelative(rawLabel, "")
if label.Repository == "" {
// This branch is taken in two different cases:
// 1. The label is relative. In this case, labels.ParseRelative populates the Package field
// but not the Repository field.
// 2. The label is of the form "@//pkg:extension.bzl". Normalize to spelling out the
// apparent name of the root module. Note that this syntax is only allowed in the root
// module, but since we are inspecting its module file as a tool, we can assume that the
// current module is the root module.
label.Repository = apparentModuleName
}
return label.Format()
}

func parseUseExtension(stmt build.Expr) (proxy string, bzlFile string, name string, dev bool, isolate bool) {
Expand Down
19 changes: 19 additions & 0 deletions edit/bzlmod/bzlmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ prox9 = use_extension(
prox9.use(label = "@name//:bar")
prox10 = use_extension("@dep//:extensions.bzl", "other_ext", dev_dependency = bool(1))
prox10.use(dict = {"foo": "bar"})
prox11 = use_extension("extension.bzl", "ext")
prox12 = use_extension(":extension.bzl", "ext")
prox13 = use_extension("//:extension.bzl", "ext")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about use_extension("@repo_name//:extension.bzl", "ext")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more tests covering this case, is that what you had in mind?

prox14 = use_extension("@name//:extension.bzl", "ext")
prox15 = use_extension("@repo_name//:extension.bzl", "ext")
`

func TestProxies(t *testing.T) {
Expand Down Expand Up @@ -139,6 +144,20 @@ func TestProxies(t *testing.T) {
true,
[]string{"prox10"},
},
{
proxiesModuleNameHeader + proxiesBody,
[]string{"//:extension.bzl", "@//:extension.bzl"},
"ext",
false,
[]string{"prox11", "prox12", "prox13", "prox14"},
},
{
proxiesModuleRepoNameHeader + proxiesBody,
[]string{"//:extension.bzl", "@//:extension.bzl"},
"ext",
false,
[]string{"prox11", "prox12", "prox13", "prox15"},
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
for _, extBzlFile := range tc.extBzlFiles {
Expand Down