Skip to content

Commit

Permalink
fix(cmd): injects version at build time
Browse files Browse the repository at this point in the history
Couple of past released missed aligning harcoded `main.version` variable
with the current tag. This results in confusing output of `gci --version`,
where e.g. last release (tagged with `v0.13.5`) prints `0.13.4`.

```shell
❯ go install github.com/daixiang0/[email protected]
❯ gci --version
gci version 0.13.4
```

With this change, `main.Version` is calculated at build time and
injected. Therefore it  will always contain the tag that it was
built from (or `devel` if it's built from untagged commit).

```shell
❯ make build
❯ ./dist/gci -v
gci version devel
❯ git tag v0.13.13
❯ make build
BIN_OUTPUT: dist/gci
❯ ./dist/gci -v
gci version 0.13.13
```

With the upcoming `go1.24` release this information can be obtained from
`runtime/debug.BuildInfo.Main.Version` [1].

This will simplify the process even further.

[1] golang/go#50603 (comment)

Signed-off-by: bartoszmajsak <[email protected]>
  • Loading branch information
bartoszmajsak committed Jan 23, 2025
1 parent dca258a commit a8472d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ clean:
@echo BIN_OUTPUT: ${BIN_OUTPUT}
@rm -rf dist cover.out

VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
GIT_TAG := $(shell git describe --tags --abbrev=0 --exact-match > /dev/null 2>&1; echo $$?)

ifneq ($(GIT_TAG),0)
ifeq ($(origin VERSION),file)
VERSION := devel
endif
endif

LDFLAGS ?= -w -X main.Version=${VERSION}
build: clean
@go build -v -trimpath -o ${BIN_OUTPUT} .
@go build -v -trimpath -ldflags "${LDFLAGS}" -o ${BIN_OUTPUT} .

test: clean
@go test -v -count=1 -cover ./...
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"os"

"github.com/daixiang0/gci/cmd/gci"
"os"
"strings"
)

var version = "0.13.4"
var Version = "0.0.0"

func main() {
e := gci.NewExecutor(version)
e := gci.NewExecutor(strings.TrimPrefix(Version, "v"))

err := e.Execute()
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/section/standard_list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8472d2

Please sign in to comment.