-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Make it easier to embed compiled git SHA into binary #22147
Comments
I have thought the same in the past, and I agree that the two current workarounds you listed are suboptimal. Let me play the devil's advocate, however:
|
Here are places where this field would come in handy. I will update this list as I come across more examples.
|
@kevinburke As you say above, you can of course use Do you have any suggestions for how this could work in practice? Also, one of your reasons is so that you can know which git revision a user is running. But their git SHA doesn't necessarily mean anything to you, depending on how the sources have moved around. So what you want to know is actually the git SHA that they started from. You could do that by storing the git SHA in your own repo, somehow, though I agree that that too has problems. Does the new go.mod file used by vgo help in any way here? |
A small note explaining what we do in practice right now for restic: we're using a small program called build.go. It allows compiling restic from a checkout of the repo and from the extracted release tar.gz without having to setup a In addition (that's why I'm mentioning it here), it embeds the Git version if available and the contents of the
The first string It turned out to be very helpful when users report issues and bugs, we request this information in our issue template. To everybody having a similar requirements, I encourage you to give |
As of Go 1.12, you can get the versions of all modules compiled into the binary (in module mode) using If the version in use is a pseudo-version, it also includes the first dozen or so characters of the VCS commit ID. |
@bcmills how does this work? I've built a small test program, running it with Go 1.12beta2 shows the versions for all dependencies, but not for the main module (when compiled from a checkout of the repo):
I can only see Is there anything I'm missing? |
I think this is affected by #29228. I'll reopen this issue until that's resolved. |
Thanks! |
Sounds good enough to me, now that these two issues reference each other. |
If I compile Go from source and run
go version
, I get output like this:I would love to get the same output in my programs. Specifically, I'm trying to debug a reported error in a program I wrote and it would be a lot easier if I knew which SHA they were running.
It's easy in the Go build tool because it can specify what code runs during the build process. From
findgoversion()
:I have options for replicating this behavior, none very good:
Use linker flags: This blog post describes compiling Go with
-ldflags -X
in such a way that you can embed the SHA at compile time. https://medium.com/@joshroppo/setting-go-1-5-variables-at-compile-time-for-versioning-5b30a965d33eI can do this of course, but I'm not sure it's feasible or possible to ask all of my users to remember to do this when they compile my program. I can add a Make target or something to automate the instructions but that's something they have to remember to do too.
I could release compiled libraries but that's infeasible in a company environment where tip is changing rapidly and users are used to compiling from source.
Tag a new release every time I add a commit to the program. I wrote this tool to do that: github.com/Shyp/bump_version. Generally though it's not practical to tag every single commit, the same reason there aren't tags for every commit between Go 1.8.3 and Go 1.9.0.
It would be nice if there was some API for detecting or automatically embedding the git SHA in the binary so I can access it at runtime, and print it out when users of my program run
<mybinary> version
or<mybinary> env
, the same way the Go team found this information useful and found a way to embed it while the program was being compiled.The text was updated successfully, but these errors were encountered: