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

Use runtime.debug.ReadBuildInfo() for git revision hash or version? #1930

Open
FiskFan1999 opened this issue Apr 9, 2022 · 1 comment
Open

Comments

@FiskFan1999
Copy link
Contributor

New in go 1.18, the compiler embeds information about how a module was built. This can include the vcs commit string, or the version number.

If implemented, this could replace the functionality provided by

ergo/Makefile

Lines 3 to 4 in 1f08c97

GIT_COMMIT := $(shell git rev-parse HEAD 2> /dev/null)
GIT_TAG := $(shell git tag --points-at HEAD 2> /dev/null | head -n 1)

package main

import (
	"log"
	"runtime/debug"
)

func main() {
	deb, _ := debug.ReadBuildInfo()
	log.Println(deb)
}

Output when calling go build from source. Note vcs.revision which is the revision hash of the current commit.

$ ./go_runtime_debug 
2022/04/08 23:35:08 go	go1.18
path	github.com/FiskFan1999/go_runtime_debug
mod	github.com/FiskFan1999/go_runtime_debug	(devel)	
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
build	vcs=git
build	vcs.revision=7223bb500758d817bd12456fef595602e5c74022
build	vcs.time=2022-04-09T03:23:46Z
build	vcs.modified=false

Output when running via go run

$ go run github.com/FiskFan1999/go_runtime_debug@main
go: downloading github.com/FiskFan1999/go_runtime_debug v1.0.0
2022/04/09 03:31:38 go	go1.18
path	github.com/FiskFan1999/go_runtime_debug
mod	github.com/FiskFan1999/go_runtime_debug	v1.0.0	h1:oFg1YfNX13U5L6jYv5sPCsGbT2ccQmz4H9xWPZx1UJc=
build	-compiler=gc
build	CGO_ENABLED=1
build	CGO_CFLAGS=
build	CGO_CPPFLAGS=
build	CGO_CXXFLAGS=
build	CGO_LDFLAGS=
build	GOARCH=amd64
build	GOOS=linux
build	GOAMD64=v1

Unfortunately we get inconsistent output depending on wether the binary was built from source or via go run (this results in the same output as go install). And for compiling from source, I don't know any way to get the tag number for the current revision without a exec call to git which is what we do in the makefile anyway. Perhaps we should stick with what we have now and say people shouldn't install ergo via go install, because that would be missing important files anyway. Just wanted to put this out there and see what are your thoughts on the matter.

@slingamn
Copy link
Member

Thanks, this is interesting. I think for now I'm pretty comfortable sticking with make install as the recommended "local install" mechanism, but I'll leave this open in case we want to revisit in future.

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

No branches or pull requests

2 participants