Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Unset $GOOS and $GOARCH before setting #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Equim-chan
Copy link

@Equim-chan Equim-chan commented Aug 7, 2017

This PR fixes an issue I found when I was writing a build tool driven by go generate on my own project sha3sum.

tl;dr go generate injects $GOOS and $GOARCH variables, leading to this issue.


The reproduction of the issue is as follows:

$GOPATH/src tree:

src
|
\---gox-issue
    |   main.go
    |
    \---builder
            main.go

$GOPATH/src/gox-issue/main.go:

package main

//go:generate go run $GOPATH/src/gox-issue/builder/main.go

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Println("runtime.GOOS:", runtime.GOOS)
	fmt.Println("runtime.GOARCH:", runtime.GOARCH)
}

$GOPATH/src/gox-issue/build/main.go:

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	fmt.Println("Env of the builder:")
	fmt.Println("GOOS=" + os.Getenv("GOOS"))
	fmt.Println("GOARCH=" + os.Getenv("GOARCH"))

	cmd := exec.Command("gox", "gox-issue")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}

Now we do:

$ cd $GOPATH/src/gox-issue

$ test X"$GOOS" == X""; echo $?
0

$ test X"$GOARCH" == X""; echo $?
0

$ go generate
Env of the builder:
GOOS=linux
GOARCH=amd64
Number of parallel builds: 2

-->      netbsd/arm: gox-issue
-->   freebsd/amd64: gox-issue
-->   windows/amd64: gox-issue
-->     openbsd/386: gox-issue
-->   openbsd/amd64: gox-issue
-->     windows/386: gox-issue
-->       linux/386: gox-issue
-->      darwin/386: gox-issue
-->    darwin/amd64: gox-issue
-->      netbsd/386: gox-issue
-->     freebsd/arm: gox-issue
-->    netbsd/amd64: gox-issue
-->       linux/arm: gox-issue
-->     linux/amd64: gox-issue
-->     freebsd/386: gox-issue

And the issue shows up:

$ md5sum gox-issue*
d005e0418c6b30a7ed8c07010b13801b  gox-issue_darwin_386
d005e0418c6b30a7ed8c07010b13801b  gox-issue_darwin_amd64
d005e0418c6b30a7ed8c07010b13801b  gox-issue_freebsd_386
d005e0418c6b30a7ed8c07010b13801b  gox-issue_freebsd_amd64
d005e0418c6b30a7ed8c07010b13801b  gox-issue_freebsd_arm
d005e0418c6b30a7ed8c07010b13801b  gox-issue_linux_386
d005e0418c6b30a7ed8c07010b13801b  gox-issue_linux_amd64
d005e0418c6b30a7ed8c07010b13801b  gox-issue_linux_arm
d005e0418c6b30a7ed8c07010b13801b  gox-issue_netbsd_386
d005e0418c6b30a7ed8c07010b13801b  gox-issue_netbsd_amd64
d005e0418c6b30a7ed8c07010b13801b  gox-issue_netbsd_arm
d005e0418c6b30a7ed8c07010b13801b  gox-issue_openbsd_386
d005e0418c6b30a7ed8c07010b13801b  gox-issue_openbsd_amd64
d005e0418c6b30a7ed8c07010b13801b  gox-issue_windows_386.exe
d005e0418c6b30a7ed8c07010b13801b  gox-issue_windows_amd64.exe

$ file gox-issue_windows_386.exe
gox-issue_windows_386.exe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

$ ./gox-issue_windows_386.exe
runtime.GOOS: linux
runtime.GOARCH: amd64

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant