Skip to content

Commit 62bca28

Browse files
committed
Handle GOARCH env var similarly to cmd/go.
Externally, GOARCH=js is the only supported mode for gopherjs compiler. Make it so that setting GOARCH=js explicitly does what one would expect, which is normal gopherjs behavior instead of a strange error. If GOARCH is set to any value other than js, which are not supported, exit with an error (similar to cmd/go behavior). Set "GOARCH" env var to correct value for gopherjs in tests/run.go. Fixes #594. Closes #595.
1 parent 69499ef commit 62bca28

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

tests/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,9 @@ func (t *test) run() {
620620
os.Setenv("GOOS", runtime.GOOS)
621621
}
622622
if os.Getenv("GOARCH") == "" {
623-
os.Setenv("GOARCH", runtime.GOARCH)
623+
//os.Setenv("GOARCH", runtime.GOARCH)
624+
// GOPHERJS.
625+
os.Setenv("GOARCH", "js") // We're running this script natively, but the tests are executed with js architecture.
624626
}
625627

626628
useTmp := true

tool.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func main() {
9191
cmdBuild.Flags().AddFlag(flagTags)
9292
cmdBuild.Flags().AddFlag(flagLocalMap)
9393
cmdBuild.Run = func(cmd *cobra.Command, args []string) {
94+
if err := verifyGOARCH(); err != nil {
95+
printError(err, options, nil)
96+
os.Exit(2)
97+
}
9498
options.BuildTags = strings.Fields(*tags)
9599
for {
96100
s := gbuild.NewSession(options)
@@ -172,6 +176,10 @@ func main() {
172176
cmdInstall.Flags().AddFlag(flagTags)
173177
cmdInstall.Flags().AddFlag(flagLocalMap)
174178
cmdInstall.Run = func(cmd *cobra.Command, args []string) {
179+
if err := verifyGOARCH(); err != nil {
180+
printError(err, options, nil)
181+
os.Exit(2)
182+
}
175183
options.BuildTags = strings.Fields(*tags)
176184
for {
177185
s := gbuild.NewSession(options)
@@ -238,6 +246,10 @@ func main() {
238246
Short: "display documentation for the requested, package, method or symbol",
239247
}
240248
cmdDoc.Run = func(cmd *cobra.Command, args []string) {
249+
if err := verifyGOARCH(); err != nil {
250+
printError(err, options, nil)
251+
os.Exit(2)
252+
}
241253
exitCode := handleError(func() error {
242254
goDoc := exec.Command("go", append([]string{"doc"}, args...)...)
243255
goDoc.Stdout = os.Stdout
@@ -270,6 +282,10 @@ func main() {
270282
Short: "compile and run Go program",
271283
}
272284
cmdRun.Run = func(cmd *cobra.Command, args []string) {
285+
if err := verifyGOARCH(); err != nil {
286+
printError(err, options, nil)
287+
os.Exit(2)
288+
}
273289
os.Exit(handleError(func() error {
274290
lastSourceArg := 0
275291
for {
@@ -321,6 +337,10 @@ func main() {
321337
cmdTest.Flags().AddFlag(flagTags)
322338
cmdTest.Flags().AddFlag(flagLocalMap)
323339
cmdTest.Run = func(cmd *cobra.Command, args []string) {
340+
if err := verifyGOARCH(); err != nil {
341+
printError(err, options, nil)
342+
os.Exit(2)
343+
}
324344
options.BuildTags = strings.Fields(*tags)
325345
os.Exit(handleError(func() error {
326346
pkgs := make([]*gbuild.PackageData, len(args))
@@ -513,6 +533,10 @@ func main() {
513533
var addr string
514534
cmdServe.Flags().StringVarP(&addr, "http", "", ":8080", "HTTP bind address to serve")
515535
cmdServe.Run = func(cmd *cobra.Command, args []string) {
536+
if err := verifyGOARCH(); err != nil {
537+
printError(err, options, nil)
538+
os.Exit(2)
539+
}
516540
options.BuildTags = strings.Fields(*tags)
517541
dirs := append(filepath.SplitList(build.Default.GOPATH), build.Default.GOROOT)
518542
var root string
@@ -772,6 +796,19 @@ func printError(err error, options *gbuild.Options, browserErrors *bytes.Buffer)
772796
}
773797
}
774798

799+
// verifyGOARCH verifies that GOARCH environment value is not set to
800+
// an unsupported value.
801+
func verifyGOARCH() error {
802+
goarch, ok := os.LookupEnv("GOARCH")
803+
if !ok {
804+
return nil
805+
}
806+
if goarch != "js" {
807+
return fmt.Errorf("gopherjs: unsupported GOOS/GOARCH pair %s/%s", build.Default.GOOS, goarch)
808+
}
809+
return nil
810+
}
811+
775812
func runNode(script string, args []string, dir string, quiet bool) error {
776813
var allArgs []string
777814
if b, _ := strconv.ParseBool(os.Getenv("SOURCE_MAP_SUPPORT")); os.Getenv("SOURCE_MAP_SUPPORT") == "" || b {

0 commit comments

Comments
 (0)