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

Preserve canonical labels as such #1863

Merged
merged 2 commits into from
Aug 22, 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
25 changes: 18 additions & 7 deletions label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type Label struct {
// Relative indicates whether the label refers to a target in the current
// package. Relative is true if and only if Repo and Pkg are both omitted.
Relative bool

// Canonical indicates whether the repository name is canonical. If true,
// then the label will be stringified with an extra "@" prefix if it is
// absolute.
// Note: Label does not apply any kind of repo mapping.
Canonical bool
}

// New constructs a new label from components.
Expand Down Expand Up @@ -83,10 +89,11 @@ func Parse(s string) (Label, error) {
origStr := s

relative := true
canonical := false
var repo string
// if target name begins @@ drop the first @
if strings.HasPrefix(s, "@@") {
s = s[len("@"):]
canonical = true
Copy link
Contributor

Choose a reason for hiding this comment

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

optional:

canonical := strings.HasPrefix(s, "@@")
if canonical {
		s = s[len("@"):]
}

}
if strings.HasPrefix(s, "@") {
relative = false
Expand Down Expand Up @@ -140,10 +147,11 @@ func Parse(s string) (Label, error) {
}

return Label{
Repo: repo,
Pkg: pkg,
Name: name,
Relative: relative,
Repo: repo,
Pkg: pkg,
Name: name,
Relative: relative,
Canonical: canonical,
}, nil
}

Expand All @@ -160,6 +168,9 @@ func (l Label) String() string {
// if l.Repo == "@", the label string will begin with "@//"
repo = l.Repo
}
if l.Canonical && strings.HasPrefix(repo, "@") {
repo = "@" + repo
}

if path.Base(l.Pkg) == l.Name {
return fmt.Sprintf("%s//%s", repo, l.Pkg)
Expand Down Expand Up @@ -213,8 +224,8 @@ func (l Label) Contains(other Label) bool {
}

func (l Label) BzlExpr() bzl.Expr {
return &bzl.StringExpr {
Value: l.String(),
return &bzl.StringExpr{
Value: l.String(),
}
}

Expand Down
38 changes: 36 additions & 2 deletions label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func TestParse(t *testing.T) {
{str: "@a//some/pkg/[someId]:[someId]", want: Label{Repo: "a", Pkg: "some/pkg/[someId]", Name: "[someId]"}},
{str: "@rules_python~0.0.0~pip~name_dep//:_pkg", want: Label{Repo: "rules_python~0.0.0~pip~name_dep", Name: "_pkg"}},
{str: "@rules_python~0.0.0~pip~name//:dep_pkg", want: Label{Repo: "rules_python~0.0.0~pip~name", Name: "dep_pkg"}},
{str: "@@rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu//:python_runtimes", want: Label{Repo: "rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu", Name: "python_runtimes"}},
{str: "@@rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu//:python_runtimes", want: Label{Repo: "rules_python~0.26.0~python~python_3_10_x86_64-unknown-linux-gnu", Name: "python_runtimes", Canonical: true}},
{str: "@rules_python++pip+name_dep//:_pkg", want: Label{Repo: "rules_python++pip+name_dep", Name: "_pkg"}},
{str: "@rules_python++pip+name//:dep_pkg", want: Label{Repo: "rules_python++pip+name", Name: "dep_pkg"}},
{str: "@@rules_python++python+python_3_10_x86_64-unknown-linux-gnu//:python_runtimes", want: Label{Repo: "rules_python++python+python_3_10_x86_64-unknown-linux-gnu", Name: "python_runtimes"}},
{str: "@@rules_python++python+python_3_10_x86_64-unknown-linux-gnu//:python_runtimes", want: Label{Repo: "rules_python++python+python_3_10_x86_64-unknown-linux-gnu", Name: "python_runtimes", Canonical: true}},
} {
got, err := Parse(tc.str)
if err != nil && !tc.wantErr {
Expand All @@ -117,3 +117,37 @@ func TestImportPathToBazelRepoName(t *testing.T) {
}
}
}

func TestParseStringRoundtrip(t *testing.T) {
for _, tc := range []struct {
in string
out string
}{
{in: "target", out: ":target"},
{in: ":target"},
{in: "//:target"},
{in: "//pkg:target"},
{in: "@repo//:target"},
{in: "@repo//pkg:target"},
{in: "@repo", out: "@repo//:repo"},
{in: "@//pkg:target"},
{in: "@@canonical~name//:target"},
{in: "@@//:target"},
} {
lbl, err := Parse(tc.in)
if err != nil {
t.Errorf("Parse(%q) failed: %v", tc, err)
continue
}
got := lbl.String()
var want string
if len(tc.out) == 0 {
want = tc.in
} else {
want = tc.out
}
if got != want {
t.Errorf("Parse(%q).String() = %q; want %q", tc.in, got, want)
}
}
}