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

Do not override defaults with blanks #11

Merged
merged 1 commit into from
Aug 2, 2023

Conversation

kke
Copy link
Contributor

@kke kke commented Aug 2, 2023

Check the values from debug.ReadBuildInfo() before overriding the versioninfo.* variables in init functions.

This makes it easier/possible to override them using the -X LD_FLAGS when building from a Makefile or such:

LD_FLAGS = -s -w -X github.com/carlmjohnson/versioninfo.Revision=$(GIT_COMMIT) -X github.com/carlmjohnson/versioninfo.Version=$(BUILD_VERSION)
BUILD_FLAGS = -ldflags "$(LD_FLAGS)"

This should still maintain the existing functionality when using go install repo/tool@version and possibly partly fix it if some of the keys are for some reason blank and would overwrite the defaults set in variables.go.

@earthboundkid
Copy link
Owner

Thanks! I’m on mobile, but I’ll tag a version when I get back to a real computer.

@arnested
Copy link

This is really useful!

Although it appears to be only working when building, main.go and not .

With a minimal example:

package main

import (
	"fmt"

	"github.com/carlmjohnson/versioninfo"
)

func main() {
	a := versioninfo.Short()
	fmt.Println(a)
}
$ go run -ldflags='-X github.com/carlmjohnson/versioninfo.Version=v1.2.3' main.go 
v1.2.3
$ go run -ldflags='-X github.com/carlmjohnson/versioninfo.Version=v1.2.3' .
devel

I don't think this is an error in this change or this package. I guess this is Go's compiler or linker behaving in a way I don't understand 😄

@arnested
Copy link

arnested commented Aug 13, 2023

Or... This appears to resolve the problem. I'm not sure if this is the right fix (it's getting late at my localtime).

diff --git a/legacy.go b/legacy.go
index b8c5fac..1433f46 100644
--- a/legacy.go
+++ b/legacy.go
@@ -9,7 +9,7 @@ func init() {
        if !ok {
                return
        }
-       if info.Main.Version != "" {
+       if info.Main.Version != "" && info.Main.Version != "(devel)" {
                Version = info.Main.Version
        }
 }

@earthboundkid
Copy link
Owner

I haven't had time to look at this, so I added a tracker for it: #12

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