From 91db5103250819f95f43324101e108b9d3b815ae Mon Sep 17 00:00:00 2001 From: Vui Lam Date: Mon, 1 Jul 2024 16:15:53 -0700 Subject: [PATCH] Enable escape of space in goflag value Signed-off-by: Vui Lam --- cmd/plugin/builder/command/cli_compile.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/plugin/builder/command/cli_compile.go b/cmd/plugin/builder/command/cli_compile.go index 7d868c7c1..e3bdbb4ac 100644 --- a/cmd/plugin/builder/command/cli_compile.go +++ b/cmd/plugin/builder/command/cli_compile.go @@ -308,6 +308,18 @@ type target struct { args []string } +func getGoFlagsList(goflags string) []string { + var result []string + tmpSpaceReplacement := "__##SPC##__" + // escape '\ ' with a replacement string before splitting + goflags = strings.ReplaceAll(goflags, "\\ ", tmpSpaceReplacement) + goflagsList := strings.Split(goflags, " ") + for _, v := range goflagsList { + result = append(result, strings.ReplaceAll(v, tmpSpaceReplacement, " ")) + } + return result +} + func (t target) build(targetPath, prefix, modPath, ldflags, tags, goflags string) error { cmd := goCommand("build") @@ -317,8 +329,15 @@ func (t target) build(targetPath, prefix, modPath, ldflags, tags, goflags string } if goflags != "" { - cmd.Args = append(cmd.Args, strings.Split(goflags, " ")...) + // Because the entire goflags string is treated as a space-delimited + // string, any goflag entry that itself has space(s) in them will not + // be processed properly. + // So, we introduce a helper to support any goflag value with space + // chararacters as long as each is escaped with a preceding '\' + // e.g. tanzu builder plugin build ... --goflag "-gcflags=all=-N\ -l" ... + cmd.Args = append(cmd.Args, getGoFlagsList(goflags)...) } + cmd.Args = append(cmd.Args, t.args...) cmd.Args = append(cmd.Args, commonArgs...)