diff --git a/go/tools/builders/env.go b/go/tools/builders/env.go index 4307e69818..177617f8aa 100644 --- a/go/tools/builders/env.go +++ b/go/tools/builders/env.go @@ -436,6 +436,23 @@ func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) { return cleanup } +// quotePathIfNeeded quotes path if it contains whitespace and isn't already quoted. +// Use this for paths that will be passed through +// https://github.com/golang/go/blob/06264b740e3bfe619f5e90359d8f0d521bd47806/src/cmd/internal/quoted/quoted.go#L25 +func quotePathIfNeeded(path string) string { + if strings.HasPrefix(path, "\"") || strings.HasPrefix(path, "'") { + // Assume already quoted + return path + } + // https://github.com/golang/go/blob/06264b740e3bfe619f5e90359d8f0d521bd47806/src/cmd/internal/quoted/quoted.go#L16 + if strings.IndexAny(path, " \t\n\r") < 0 { + // Does not require quoting + return path + } + // Escaping quotes is not supported, so we can assume path doesn't contain any quotes. + return "'" + path + "'" +} + func useResponseFile(path string, argLen int) bool { // Unless the program uses objabi.Flagparse, which understands // response files, don't use response files. diff --git a/go/tools/builders/stdlib.go b/go/tools/builders/stdlib.go index 1ac0a25425..244388d0db 100644 --- a/go/tools/builders/stdlib.go +++ b/go/tools/builders/stdlib.go @@ -79,7 +79,7 @@ You may need to use the flags --cpu=x64_windows --compiler=mingw-gcc.`) // Make sure we have an absolute path to the C compiler. // TODO(#1357): also take absolute paths of includes and other paths in flags. - os.Setenv("CC", abs(os.Getenv("CC"))) + os.Setenv("CC", quotePathIfNeeded(abs(os.Getenv("CC")))) // Ensure paths are absolute. absPaths := []string{} diff --git a/go/tools/builders/stdliblist.go b/go/tools/builders/stdliblist.go index eb203e21c7..9caf37c9e9 100644 --- a/go/tools/builders/stdliblist.go +++ b/go/tools/builders/stdliblist.go @@ -202,7 +202,7 @@ func stdliblist(args []string) error { os.Setenv("GOROOT", newGoRoot) // Make sure we have an absolute path to the C compiler. // TODO(#1357): also take absolute paths of includes and other paths in flags. - os.Setenv("CC", abs(os.Getenv("CC"))) + os.Setenv("CC", quotePathIfNeeded(abs(os.Getenv("CC")))) cachePath := abs(*out + ".gocache") defer os.RemoveAll(cachePath)