This change makes go env -w and -u check invalid GOOS and GOARCH values and abort if that's the case.
Fixes #34194
Change-Id: Idca8e93bb0b190fd273bf786c925be7993c24a2b
GitHub-Last-Rev:
ee67f09d75f4552001cb8b6506bc4af0894c9b05
GitHub-Pull-Request: golang/go#34221
Reviewed-on: https://go-review.googlesource.com/c/go/+/194617
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
import (
"encoding/json"
"fmt"
+ "go/build"
"io/ioutil"
"os"
"path/filepath"
fmt.Fprintf(os.Stderr, "warning: go env -w %s=... does not override conflicting OS environment variable\n", key)
}
}
+
+ goos, okGOOS := add["GOOS"]
+ goarch, okGOARCH := add["GOARCH"]
+ if okGOOS || okGOARCH {
+ if !okGOOS {
+ goos = cfg.Goos
+ }
+ if !okGOARCH {
+ goarch = cfg.Goarch
+ }
+ if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
+ base.Fatalf("go env -w: %v", err)
+ }
+ }
+
updateEnvFile(add, nil)
return
}
}
del[arg] = true
}
+ if del["GOOS"] || del["GOARCH"] {
+ goos, goarch := cfg.Goos, cfg.Goarch
+ if del["GOOS"] {
+ goos = getOrigEnv("GOOS")
+ if goos == "" {
+ goos = build.Default.GOOS
+ }
+ }
+ if del["GOARCH"] {
+ goarch = getOrigEnv("GOARCH")
+ if goarch == "" {
+ goarch = build.Default.GOARCH
+ }
+ }
+ if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
+ base.Fatalf("go env -u: %v", err)
+ }
+ }
updateEnvFile(nil, del)
return
}
}
}
+func getOrigEnv(key string) string {
+ for _, v := range cfg.OrigEnv {
+ if strings.HasPrefix(v, key+"=") {
+ return strings.TrimPrefix(v, key+"=")
+ }
+ }
+ return ""
+}
+
func checkEnvWrite(key, val string) error {
switch key {
case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR":
}
}
- if _, ok := cfg.OSArchSupportsCgo[cfg.Goos+"/"+cfg.Goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
- fmt.Fprintf(os.Stderr, "cmd/go: unsupported GOOS/GOARCH pair %s/%s\n", cfg.Goos, cfg.Goarch)
+ if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil {
+ fmt.Fprintf(os.Stderr, "cmd/go: %v", err)
base.SetExitStatus(2)
base.Exit()
}
+
for _, tag := range cfg.BuildContext.BuildTags {
if strings.Contains(tag, ",") {
fmt.Fprintf(os.Stderr, "cmd/go: -tags space-separated list contains comma\n")
}
}
+func CheckGOOSARCHPair(goos, goarch string) error {
+ if _, ok := cfg.OSArchSupportsCgo[goos+"/"+goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
+ return fmt.Errorf("unsupported GOOS/GOARCH pair %s/%s", goos, goarch)
+ }
+ return nil
+}
+
// NewObjdir returns the name of a fresh object directory under b.WorkDir.
// It is up to the caller to call b.Mkdir on the result at an appropriate time.
// The result ends in a slash, so that file names in that directory
! go env -w GOPATH=./go
stderr 'GOPATH entry is relative; must be absolute path'
+
+# go env -w/-u checks validity of GOOS/ARCH combinations
+env GOOS=
+env GOARCH=
+# check -w doesn't allow invalid GOOS
+! go env -w GOOS=linuxx
+stderr 'unsupported GOOS/GOARCH pair linuxx'
+# check -w doesn't allow invalid GOARCH
+! go env -w GOARCH=amd644
+stderr 'unsupported GOOS/GOARCH.*/amd644$'
+# check -w doesn't allow invalid GOOS with valid GOARCH
+! go env -w GOOS=linuxx GOARCH=amd64
+stderr 'unsupported GOOS/GOARCH pair linuxx'
+# check a valid GOOS and GOARCH values but an incompatible combinations
+! go env -w GOOS=android GOARCH=s390x
+stderr 'unsupported GOOS/GOARCH pair android/s390x'
+# check that -u considers explicit envs
+go env -w GOOS=linux GOARCH=mips
+env GOOS=windows
+! go env -u GOOS
+stderr 'unsupported GOOS/GOARCH.*windows/mips$'