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

Breaking change: runtime/debug.ReadBuildInfo no longer populates BuildInfo.Main in 1.18 #51831

Closed
bufdev opened this issue Mar 20, 2022 · 3 comments

Comments

@bufdev
Copy link

bufdev commented Mar 20, 2022

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

$ go version
go version go1.18 darwin/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="on"
GOARCH="amd64"
GOBIN="/Users/alice/.cache/buf/Darwin/x86_64/gobin"
GOCACHE="/Users/alice/.cache/buf/Darwin/x86_64/gocache"
GOENV="/Users/alice/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/alice/.cache/buf/Darwin/x86_64/go/pkg/mod"
GONOPROXY="github.com/bufbuild,github.com/bufbuild/buf"
GONOSUMDB="github.com/bufbuild,github.com/bufbuild/buf"
GOOS="darwin"
GOPATH="/Users/alice/.cache/buf/Darwin/x86_64/go"
GOPRIVATE="github.com/bufbuild,github.com/bufbuild/buf"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/alice/.cache/gimme/versions/go1.18.darwin.amd64"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/alice/.cache/gimme/versions/go1.18.darwin.amd64/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/alice/git/buf/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gl/rk7vtj000000000000gn/T/go-build0041845511=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://pkg.go.dev/runtime/debug#ReadBuildInfo no longer sets the Main field on https://pkg.go.dev/runtime/debug#BuildInfo. We use debug.ReadBuildInfo to make it so that code from https://github.com/bufbuild/buf/tree/main/private can only be imported by projects in github.com/bufbuild - our version of "organization-private". We do this via https://github.com/bufbuild/buf/blob/main/private/usage/usage.go. See bufbuild/homebrew-buf#8

mkdir tmp
cd ./tmp
cat <<EOF > go.mod
module foo

go 1.17
EOF
cat <<EOF > main.go
package main

import (
	"fmt"
	"runtime/debug"
)

func main() {
	buildInfo, _ := debug.ReadBuildInfo()
	fmt.Printf("%+v\n", buildInfo)
}
EOF
go build -o foo main.go
./foo

What did you expect to see?

A value for the Main.Path field of foo.

In 1.17.8 this is:

&{Path:command-line-arguments Main:{Path:foo Version:(devel) Sum: Replace:<nil>} Deps:[]}

What did you see instead?

In 1.18 this is:

go	go1.18
path	command-line-arguments
build	-compiler=gc
build	CGO_ENABLED=1
build	CGO_CFLAGS=
build	CGO_CPPFLAGS=
build	CGO_CXXFLAGS=
build	CGO_LDFLAGS=
build	GOARCH=amd64
build	GOOS=darwin
build	GOAMD64=v1

Formatting differences are expected based on the work in https://github.com/golang/go/commits/master/src/runtime/debug/mod.go, but no longer producing the Main field is a breaking change. I've verified that no fields are set as well, and that this is not just formatting, by specifically printing buildInfo.Main.Path from the code above (which is empty).

@seankhliao
Copy link
Member

This is CL 339170 and working as intended.

@lrewega
Copy link

lrewega commented Mar 21, 2022

@michaelmatloob @seankhliao I'm rather surprised by this otherwise undocumented change in behaviour, I would expect at least some mention of this in the release notes. @ianlancetaylor can we amend the notes to help other folks from tripping over this change? Could you also please confirm that my understanding below is correct? I am not sure I fully understand the consequences of this change.


For future readers who stumble upon this: It looks like as part of the workspaces feature, some runtime/debug.BuildInfo details for the pseudo-package command-line-arguments, which represents file arguments passed to go commands, were removed. Using a package path (relative or otherwise) instead of file arguments (such as path/to/main.go) avoids the command-line-arguments package, and produces consistent results across versions. For example, changing the original example above from

go build -o foo main.go

to

go build -o foo .

produces the expected output:

go	go1.18rc1
path	foo
mod	foo	(devel)
build	-compiler=gc
build	CGO_ENABLED=1
build	CGO_CFLAGS=
build	CGO_CPPFLAGS=
build	CGO_CXXFLAGS=
build	CGO_LDFLAGS=
build	GOARCH=arm64
build	GOOS=darwin

Note the presence of mod foo (devel), representing buildInfo.Main.Path, which is absent when using file arguments.

@lrewega
Copy link

lrewega commented Mar 21, 2022

Related to #51279, due to changing command-line-arguments to not-a-module.

cc @bcmills

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

No branches or pull requests

4 participants