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

Missing version output in revive -version in goreleaser released files #787

Open
echoix opened this issue Feb 12, 2023 · 5 comments · Fixed by oxsecurity/megalinter#2318

Comments

@echoix
Copy link
Contributor

echoix commented Feb 12, 2023

Describe the bug
A clear and concise description of what the bug is.
The released binaries do not contain a version when calling revive -version

To Reproduce
Steps to reproduce the behavior:

  1. Get revive by the following three methods, and run revive -version
  2. See that the versions are different

From the downloaded latest release (linux amd64)

$ chmod +x revive
$ ./revive -version
version (devel)

If using the released docker image, (also built by goreleaser):
(Note: I used this dockerfile to have an image where I could call with an entry point)

FROM ghcr.io/mgechev/revive:1.2.5 as revive
FROM alpine:latest
COPY --from=revive /usr/bin/revive /usr/bin/revive
$ docker run --rm   revive:test-echoix sh -c 'revive -version' 
version (devel)

If building from go install (inside a container)

$ docker run --rm -it golang:1.19-alpine sh -c 'go install github.com/mgechev/revive@master && revive -version'
Unable to find image 'golang:1.19-alpine' locally

1.19-alpine: Pulling from library/golang
63b65145d645: Already exists 
a2d21d5440eb: Pull complete 
46eea44c656f: Pull complete 
f44e73cf898e: Pull complete 
Digest: sha256:a00a03c72ecc1a01e4ca7b7cfb62459063912ff45ccb13011f9fb061e25916eb
Status: Downloaded newer image for golang:1.19-alpine

go: downloading github.com/mgechev/revive v1.2.5
go: downloading github.com/mitchellh/go-homedir v1.1.0
go: downloading github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517
go: downloading github.com/fatih/color v1.14.1
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/BurntSushi/toml v1.2.1
go: downloading github.com/chavacava/garif v0.0.0-20221024190013-b3ef35877348
go: downloading github.com/olekukonko/tablewriter v0.0.5
go: downloading github.com/fatih/structtag v1.2.0
go: downloading golang.org/x/tools v0.5.0
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/mattn/go-isatty v0.0.17
go: downloading github.com/mattn/go-runewidth v0.0.9
go: downloading golang.org/x/sys v0.4.0
version 1.2.5

Expected behavior
A clear and concise description of what you expected to happen.
The released binaries are the same as the ones that we can build locally with the latest available release source code. This means that version returned by -version would be the same.

Logs
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: VERSION="20.04.5 LTS (Focal Fossa)" on Gitpod
  • Version of Go : 1.19?

Additional context
Add any other context about the problem here.
The goreleaser/goreleaser-action@v2 seems outdated, there is a goreleaser/goreleaser-action@v4

By default, (at the latest releases, I don't know about older versions of goreleaser), Goreleaser adds the following ldflags:
-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
See also the note on their docs to get reproducible builds https://goreleaser.com/customization/builds/#reproducible-builds

In my try to get editorconfig-checker using goreleaser, the build section looked like this to have reproducible builds:

builds:
  - # ID of the build.
    # Defaults to the binary name.
    id: "editorconfig-checker"

    # Path to main.go file or main package.
    # Notice: when used with `gomod.proxy`, this must be a package.
    #
    # Default is `.`.
    main: ./cmd/editorconfig-checker
    env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin

    goarch:
      - amd64
      - arm64
      - arm
      - 386
    # Set the modified timestamp on the output binary, typically
    # you would do this to ensure a build was reproducible. Pass
    # empty string to skip modifying the output.
    # Default is empty string, which will be the compile time.
    mod_timestamp: "{{ .CommitTimestamp }}"
    flags:
      - -trimpath
    # Custom ldflags templates.
    # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.
    # The goal of the changes to the default flags is to make reproducible builds.
    ldflags:
      - -s -w -X main.version={{.Version}}
      - -X main.commit={{.Commit}} -X main.date={{.CommitDate}}
      - -X main.builtBy=goreleaser

However, in the Makefile at the root of the project, the ldflags seems to not apply the version to main, but to cli.

revive/Makefile

Lines 3 to 9 in a4f4632

export GO111MODULE=on
GIT_COMMIT ?= $(shell git rev-parse --verify HEAD)
GIT_VERSION ?= $(shell git describe --tags --always --dirty="-dev")
DATE ?= $(shell date -u '+%Y-%m-%d %H:%M UTC')
BUILDER ?= Makefile
VERSION_FLAGS := -X "github.com/mgechev/revive/cli.version=$(GIT_VERSION)" -X "github.com/mgechev/revive/cli.date=$(DATE)" -X "github.com/mgechev/revive/cli.commit=$(GIT_COMMIT)" -X "github.com/mgechev/revive/cli.builtBy=$(BUILDER)"

Thus, the solution is either to have the project use a main something to be like other default go projects, or to configure the .goreleaser.yml to have the same flags if it is what it takes to have a working build:

revive/.goreleaser.yml

Lines 16 to 24 in a4f4632

builds:
-
env:
- CGO_ENABLED=0
- BUILDER=GoReleaser
goos:
- linux
- darwin
- windows

@chavacava
Copy link
Collaborator

Hi @echoix, thanks for filling the issue.

@echoix
Copy link
Contributor Author

echoix commented Feb 18, 2023

I can make a PR with what I think should minimally work, that is, by porting the flags of the Makefile to the GoReleaser config. If you would like to do a more in-depth fix, it could be done afterwards.

Would you also like to modernize the .goreleaser.yml file from what is the new default template (there are some deprecation warnings now)?

@chavacava
Copy link
Collaborator

Hi @echoix,

I can make a PR with what I think should minimally work, that is, by porting the flags of the Makefile to the GoReleaser config.

It sound good to me. Thanks!

@chavacava
Copy link
Collaborator

Hi @echoix, any progress on the PR ?

@echoix
Copy link
Contributor Author

echoix commented Nov 1, 2023

Sorry, not more that what was in my branches at the time. It was fonctionnal, but not the complete upgrade of GoReleaser config I hoped for.

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

Successfully merging a pull request may close this issue.

2 participants