Skip to content

Commit

Permalink
Restore the --shorten_label flag (bazelbuild#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos committed Sep 10, 2020
1 parent 812e9f2 commit 6b71249
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 11 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ function test_sorted_deps() {
)'
}

function test_noshorten_labels_flag() {
in='go_library(
name = "edit",
)'
run "$in" --shorten_labels=false 'add deps //pkg:local' '//pkg:edit'
assert_equals 'go_library(
name = "edit",
deps = ["//pkg:local"],
)'
}

function test_add_duplicate_label() {
# "build" and ":build" labels are equivalent
in='go_library(
Expand Down
4 changes: 2 additions & 2 deletions edit/buildozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ func getStringValue(value string) string {
// and shortens the label value if possible.
func getStringExpr(value, pkg string) build.Expr {
if unquoted, triple, err := build.Unquote(value); err == nil {
return &build.StringExpr{Value: labels.ShortenLabel(unquoted, pkg), TripleQuote: triple}
return &build.StringExpr{Value: ShortenLabel(unquoted, pkg), TripleQuote: triple}
}
return &build.StringExpr{Value: labels.ShortenLabel(value, pkg)}
return &build.StringExpr{Value: ShortenLabel(value, pkg)}
}

func cmdCopy(opts *Options, env CmdEnvironment) (*build.File, error) {
Expand Down
12 changes: 5 additions & 7 deletions edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ func AllStrings(e build.Expr) []*build.StringExpr {

// listsFind looks for a string in list expressions
func listsFind(lists []*build.ListExpr, item string, pkg string) *build.StringExpr {
item = labels.ShortenLabel(item, pkg)
for _, list := range lists {
for _, elem := range list.List {
str, ok := elem.(*build.StringExpr)
Expand All @@ -442,15 +441,13 @@ func listsFind(lists []*build.ListExpr, item string, pkg string) *build.StringEx
// concatenation of lists). It returns the element if it is found. nil
// otherwise.
func ListFind(e build.Expr, item string, pkg string) *build.StringExpr {
item = labels.ShortenLabel(item, pkg)
return listsFind(AllLists(e), item, pkg)
}

// listOrSelectFind looks for a string in the list expression (which may be a
// concatenation of lists and select statements). It returns the element
// if it is found. nil otherwise.
func listOrSelectFind(e build.Expr, item string, pkg string) *build.StringExpr {
item = labels.ShortenLabel(item, pkg)
return listsFind(allListsIncludingSelects(e), item, pkg)
}

Expand Down Expand Up @@ -656,7 +653,6 @@ func ListDelete(e build.Expr, item, pkg string) (deleted *build.StringExpr) {
item = unquoted
}
deleted = nil
item = labels.ShortenLabel(item, pkg)
for _, li := range AllLists(e) {
RemoveFromList(li, item, pkg, &deleted)
}
Expand All @@ -682,14 +678,13 @@ func ListAttributeDelete(rule *build.Rule, attr, item, pkg string) *build.String
// to indicate whether the replacement was successful.
func ListReplace(e build.Expr, old, value, pkg string) bool {
replaced := false
old = labels.ShortenLabel(old, pkg)
for _, li := range allListsIncludingSelects(e) {
for k, elem := range li.List {
str, ok := elem.(*build.StringExpr)
if !ok || !labels.Equal(str.Value, old, pkg) {
continue
}
li.List[k] = &build.StringExpr{Value: labels.ShortenLabel(value, pkg), Comments: *elem.Comment()}
li.List[k] = &build.StringExpr{Value: ShortenLabel(value, pkg), Comments: *elem.Comment()}
replaced = true
}
}
Expand Down Expand Up @@ -1133,8 +1128,11 @@ func ParseLabel(target string) (string, string, string) {

// ShortenLabel rewrites labels to use the canonical form (the form
// recommended by build-style).
// Deprecated; use labels.ShortenLabel instead
// Doesn't do anything if `--noshorten_label` flag is provided.
func ShortenLabel(label string, pkg string) string {
if !ShortenLabelsFlag {
return label
}
return labels.ShortenLabel(label, pkg)
}

Expand Down

0 comments on commit 6b71249

Please sign in to comment.