Skip to content

Commit

Permalink
Revert "Preserve canonical labels as such (#1863)"
Browse files Browse the repository at this point in the history
This reverts commit 2b326f8.
  • Loading branch information
fmeum authored Aug 22, 2024
1 parent 2b326f8 commit 79f7d9b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 54 deletions.
25 changes: 7 additions & 18 deletions label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ 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 @@ -89,11 +83,10 @@ 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
}
if strings.HasPrefix(s, "@") {
relative = false
Expand Down Expand Up @@ -147,11 +140,10 @@ func Parse(s string) (Label, error) {
}

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

Expand All @@ -168,9 +160,6 @@ 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 @@ -224,8 +213,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: 2 additions & 36 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", Canonical: true}},
{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++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", Canonical: true}},
{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"}},
} {
got, err := Parse(tc.str)
if err != nil && !tc.wantErr {
Expand All @@ -117,37 +117,3 @@ 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)
}
}
}

0 comments on commit 79f7d9b

Please sign in to comment.