Skip to content

Commit 3c068ff

Browse files
committed
buildozer: naïve handling of multiple glob arguments
1 parent 4bd717d commit 3c068ff

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

buildozer/buildozer_test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,18 @@ function test_set_glob_srcs() {
19611961
)'
19621962
}
19631963

1964+
function test_set_multiple_glob_srcs() {
1965+
# Note that the inner tokens are not sorted.
1966+
run "$no_deps" 'set srcs glob(["*.go", "*.cgo"])' '//pkg:edit'
1967+
assert_equals 'go_library(
1968+
name = "edit",
1969+
srcs = glob([
1970+
"*.go",
1971+
"*.cgo",
1972+
]),
1973+
)'
1974+
}
1975+
19641976
function test_fix_unused_load() {
19651977
run 'load(":a.bzl", "a")
19661978
# TODO: refactor

edit/buildozer.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,21 @@ func cmdSetIfAbsent(opts *Options, env CmdEnvironment) (*build.File, error) {
575575
return env.File, nil
576576
}
577577

578+
// isFunctionCall parses the args to see if they are arguments to a single
579+
// function call. This is naïve and does not attempt to parse the inner tokens.
580+
// example:
581+
// the user input: "glob(["*.c", "*.h"])"
582+
// is tokenized to: "glob([\"*.c\",", "\"*.h\"])",
583+
func isFunctionCall(function string, args[] string) bool {
584+
return strings.HasPrefix(args[0], function + "(") &&
585+
strings.HasSuffix(args[len(args) -1], ")")
586+
}
587+
588+
func functionCallExpr(args []string) build.Expr {
589+
joined := strings.Join(args, "")
590+
return &build.Ident{Name: joined}
591+
}
592+
578593
func getAttrValueExpr(attr string, args []string, env CmdEnvironment) build.Expr {
579594
switch {
580595
case attr == "kind":
@@ -585,9 +600,8 @@ func getAttrValueExpr(attr string, args []string, env CmdEnvironment) build.Expr
585600
list = append(list, &build.LiteralExpr{Token: i})
586601
}
587602
return &build.ListExpr{List: list}
588-
case IsList(attr) && len(args) == 1 && strings.HasPrefix(args[0], "glob("):
589-
// Single-value glob
590-
return &build.Ident{Name: args[0]}
603+
case IsList(attr) && isFunctionCall("glob", args):
604+
return functionCallExpr(args)
591605
case IsList(attr):
592606
var list []build.Expr
593607
for _, arg := range args {

0 commit comments

Comments
 (0)