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

Add go1.12 versioning support #172

Closed
wants to merge 1 commit into from
Closed

Add go1.12 versioning support #172

wants to merge 1 commit into from

Conversation

vearutop
Copy link
Contributor

With upcoming go1.12 we can now embed version information without LDFLAGS. This PR uses debug.ReadBuildInfo() to simplify versioning with go get.

Here is a working example

GO111MODULE=on go1.12rc1 get github.com/vearutop/go-versioning-example@v0.0.1
go-versioning-example
Build info: &{Path:github.com/vearutop/go-versioning-example Main:{Path:github.com/vearutop/go-versioning-example Version:v0.0.1 Sum:h1:RtKk8SiukO2jrjtkz+0mYQ6bQjUcom3UUqTdBpGAk5o= Replace:<nil>} Deps:[0xc0000a02c0 0xc0000a0300]}
2019-02-12T11:11:20.447+0100	INFO	go-versioning-example@v0.0.1/main.go:9	App Version: v0.0.1

@coveralls
Copy link

coveralls commented Feb 12, 2019

Pull Request Test Coverage Report for Build 303

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 59.188%

Totals Coverage Status
Change from base Build 298: 0.0%
Covered Lines: 773
Relevant Lines: 1306

💛 - Coveralls

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
Are there more docs around how the main module version is populated?
I'm currently looking at: https://tip.golang.org/pkg/runtime/debug/#ReadBuildInfo

What happens when the master branch is built? Will "dev" be overwritten with the previous version number?

I like -ldflags="-X main.Version=$(git describe --tags)" since git describe --tags will only print the tag name if the tag commit is checked out. Otherwise, it prints the nearest tag with the number of commits between the tag and the commit hash. It'd be nice to continue to have this behavior.

@vearutop
Copy link
Contributor Author

This change does not intend to replace LDFLAGS versioning, but rather complement it when LDFLAGS are missing. Version would be replaced only if it has a default value of dev, meaning that LDFLAGS did not change it.

Unfortunately documentation on debug.ReadBuildInfo is very limited, when you build against master you will have "(devel)" value in info.Main.Version.

Here is a relevant issue regarding limited documentation: golang/go#29228.

// +build go1.12

package main

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import "runtime/debug"

@dhui
Copy link
Member

dhui commented Feb 19, 2019

Thanks for helping migrate stay updated!

However, I'm going to hold off on merging this until there's more documentation around how debug.ReadBuildInfo() works and there's other projects/examples using this method for populating the version.

I peeked at the readBuildInfo() implementation in runtime/debug/mod.go, which lead me to the equally undocumented PackageBuildInfo.

It looks like there's also an issue for tracking how to use VCS to get the module's version, which we could then replace our usage of: -ldflags="-X main.Version=$(git describe --tags)"

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

Successfully merging this pull request may close these issues.

3 participants