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: breakage with go version command and GOFLAGS environment variable #41264

Closed
williamh opened this issue Sep 7, 2020 · 16 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@williamh
Copy link

williamh commented Sep 7, 2020

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

$ go version
go version go1.15.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/william/.cache/go-build"
GOENV="/home/william/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/william/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/william/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="x86_64-pc-linux-gnu-gcc"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build239748757=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ export GOFLAGS="-v -x"
$ go version

What did you expect to see?

go version go1.15.1 linux/amd64

What did you see instead?

william@linux1 ~ $ export GOFLAGS="-v -x"
william@linux1 ~ $ go version
go version: flags can only be used with arguments
william@linux1 ~ $ echo $?
2
william@linux1 ~ $

I found this because it breaks build systems for go packages that use "go version" to check the version of go they are using, for example: https://github.com/go-gitea/gitea

@tpaschalis
Copy link
Contributor

tpaschalis commented Sep 7, 2020

Thanks for opening this issue! This behavior stems from a check implemented here, when either the -m or -v flags are passed, as they are used to enable printing the embedded module version information for executables and reporting unrecognized files respectively.

Basically, when go version parses -v, it switches from reporting its own go version to searching for version information on Go executable files, on the path provided as argument(s).

Edit: I guess the certain way to get Go to print its own version would be GOFLAGS="" go version; I'm not aware of any cases where there are exceptions on GOFLAGS.

@williamh
Copy link
Author

williamh commented Sep 7, 2020

I can write a p/r for gitea to make that work, but if you go this route, this behavior should probably be documented in the go 1.15 release notes some how. I'm sure I'm not the only person who will hit it. :-)

@tpaschalis
Copy link
Contributor

I'm not the most qualified person to say which way this should be handled eg. a better error message, or documenting the two go version 'modes' in a clear way.

Maybe @mvdan will have some more context since he worked more recently on implementing these checks.

@mvdan
Copy link
Member

mvdan commented Sep 7, 2020

This seems like an unintended regression; I'll take a look in the morning.

@williamh
Copy link
Author

williamh commented Sep 7, 2020

Thanks much. :-)

@andybons andybons changed the title breakage with go version command and GOFLAGS environment variable cmd/go: breakage with go version command and GOFLAGS environment variable Sep 8, 2020
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 8, 2020
@andybons andybons added this to the Unplanned milestone Sep 8, 2020
@andybons
Copy link
Member

andybons commented Sep 8, 2020

@bcmills @jayconrod @matloob

@bcmills bcmills assigned bcmills and mvdan and unassigned bcmills Sep 8, 2020
@bcmills bcmills modified the milestones: Unplanned, Go1.16 Sep 8, 2020
@mvdan
Copy link
Member

mvdan commented Sep 8, 2020

@bcmills is there a way for runVersion to tell apart go version -v from GOFLAGS=-v go version? I think it's OK for the first to error, because the explicit flag is pointless, but the second shouldn't. It's perfectly reasonable to write something like:

export GOFLAGS=-v # always be verbose
go version
go build ./...

If it's not possible to separate the two cases, then I'll just send a revert CL which we can backport to 1.15.x.

@bcmills
Copy link
Contributor

bcmills commented Sep 9, 2020

@mvdan, you could iterate over cmd/go/internal/base.GOFLAGS:

func inGOFLAGS(flag string) bool {
for _, goflag := range base.GOFLAGS() {
name := goflag
if strings.HasPrefix(name, "--") {
name = name[1:]
}
if i := strings.Index(name, "="); i >= 0 {
name = name[:i]
}
if name == flag {
return true
}
}
return false
}

That would reduce the false-positives, at the cost of some (rare) false-negatives: if the user ran GOFLAGS=-v go version -v then the explicit argument wouldn't be diagnosed, but I'm not sure that matters much.

@mvdan
Copy link
Member

mvdan commented Sep 9, 2020

Do you reckon that's worth it versus a revert, given the slight inconsistency and unlikely false negatives? It would be a pretty tiny patch if we're OK with exporting inGOFLAGS, so at least there's that.

@bcmills
Copy link
Contributor

bcmills commented Sep 9, 2020

I think either approach is fairly low-risk, given that go version is not that widely used and GOFLAGS does not often contain -v or -m.

@gopherbot
Copy link

Change https://golang.org/cl/254157 mentions this issue: cmd/go: relax version's error on unexpected flags

@mvdan
Copy link
Member

mvdan commented Sep 15, 2020

Do we think this is enough of a regression to warrant backporting to 1.15.x? I personally think not, because it seems pretty rare and also fairly easy to work around. But if anyone else feels strongly about it, it wouldn't be a lot of effort to backport.

@williamh
Copy link
Author

I would like to see it back ported if it isn't a lot of work, otherwise I will have to backport it myself in Gentoo's go packages since we set GOFLAGS=-v by default in our builds.

@mvdan
Copy link
Member

mvdan commented Sep 17, 2020

@gopherbot please consider this for backport to 1.15, it's a regression.

@gopherbot
Copy link

Backport issue(s) opened: #41464 (for 1.15).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@mvdan
Copy link
Member

mvdan commented Sep 17, 2020

Thanks @williamh, that seems like enough reason to me. I've opened a backport issue and CL.

@golang golang locked and limited conversation to collaborators Sep 17, 2021
@rsc rsc unassigned mvdan Jun 23, 2022
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

6 participants