Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: get ignores GOARCH environment variable #36022

Closed
dh1tw opened this issue Dec 6, 2019 · 4 comments
Closed

cmd/go: get ignores GOARCH environment variable #36022

dh1tw opened this issue Dec 6, 2019 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dh1tw
Copy link

dh1tw commented Dec 6, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

$ go env

GO111MODULE=""
GOARCH="386"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=".exe"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="windows"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
GO386="sse2"
AR="ar"
CC="i686-w64-mingw32-gcc"
CXX="i686-w64-mingw32-g++"
CGO_ENABLED="1"
GOMOD="/usr/src/myapp/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-m32 -mthreads -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build660781913=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I want to cross compile a program to GOOS=windows and GOARCH=386. These variables have already been set in the environment (see above). However, I want to install some tools (code generators) before actually (cross) compiling.

Since the HostOS and HostARCH differ, I inline GOOSand GOARCH before the actual go get:

$ GOOS=linux GOARCH=amd64 go get github.com/gogo/protobuf/protoc-gen-gofast

What did you expect to see?

I expect that by specifying GOOS and GOARCH inline with the go get command, that the program (in this case protoc-gen-gofast) is compiled for linux amd64.

What did you see instead?

The inline GOARCH environment variable is ingnored. Instead a binary is produced for linux_386.

@dh1tw dh1tw changed the title inline GOARCH ignored for go get inline GOARCH ignored by go get Dec 6, 2019
@cagedmantis cagedmantis changed the title inline GOARCH ignored by go get cmd/go: inline GOARCH ignored by go get Dec 6, 2019
@toothrot toothrot changed the title cmd/go: inline GOARCH ignored by go get cmd/go: go get ignores inline GOARCH ignored Dec 9, 2019
@toothrot toothrot changed the title cmd/go: go get ignores inline GOARCH ignored cmd/go: get ignores inline GOARCH Dec 9, 2019
@toothrot toothrot changed the title cmd/go: get ignores inline GOARCH cmd/go: get ignores GOARCH environment variable Dec 9, 2019
@toothrot
Copy link
Contributor

toothrot commented Dec 9, 2019

@dh1tw I believe that when you are specifying GOARCH, go get will install the compiled binary to a subdirectory of $GOPATH/bin. In this case, I believe it would be named $GOPATH/bin/linux_amd64/protoc-gen-gofast.

For example, on my amd64 linux install, if I specify a GOARCH of 386:

$ GOARCH=386 go get github.com/gogo/protobuf/protoc-gen-gofast
$ file $GOPATH/bin/linux_386/protoc-gen-gofast 
go/1.13.3/bin/linux_386/protoc-gen-gofast: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, Go BuildID=RPEyDh1sv4tnwvNOxSHp/EehTLUa5pI1qu2yMin51/sLznK566rlSWYqmCDwTM/IgTIQ-IUDCYV5GkUCGRU, not stripped

Can you check $GOPATH/bin/ for me and see if that is the case?

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 9, 2019
@toothrot toothrot added this to the Backlog milestone Dec 9, 2019
@dh1tw
Copy link
Author

dh1tw commented Dec 12, 2019

Hi @toothrot,

The problem is actually easy to reproduce. For example, let's use a clean golang container:

$ docker run -it golang:1.13-buster /bin/bash

Within the container, we execute:

$ export GOARCH=386
$ GOOS=linux GOOARCH=amd64 go get github.com/gogo/protobuf/protoc-gen-gofast
$ ls -al /go/bin
linux_386
$ ls -al /go/bin/linux_386
protoc-gen-gofast
$ file /go/bin/linux_386/protoc-gen-gofast
/go/bin/linux_386/protoc-gen-gofast: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, Go BuildID=WMKXHIKIYum37lDtIAnS/TsmmTc2G8ppJZvlNqQea/RbWhcsXqhRnKMWQcf3DW/UXNXxoQYDBTuNAoKMvWp, not stripped

Despite explicitly specifying GOARCH=amd64 inline with the go get command, GOARCH gets ignored and a binary for 386 gets generated (which was set as an environment variable earlier).
Same problem happens with go build.

@toothrot
Copy link
Contributor

@dh1tw In your example here, GOARCH is mistakenly spelled as GOOARCH. Can you verify that is not the issue?

@dh1tw
Copy link
Author

dh1tw commented Dec 12, 2019

https://media.giphy.com/media/8WdsK61D9YOOc/giphy.gif

Indeed, misspelling GOARCH was the problem. Embarrassing... I thought those kinds of things wouldn't happen to me anymore 😅

@dh1tw dh1tw closed this as completed Dec 12, 2019
@golang golang locked and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants