Skip to content

Conversation

@jamesog
Copy link
Contributor

@jamesog jamesog commented Jul 9, 2025

Go has different semantics when you run with go run . vs go run main.go.

In the former case, Go provides information about the module, including some version info. If the tree is not on a semver tag, it outputs "(devel)".

In the latter case, Go provides no information about the module and running go run main.go version would output a blank line.

The getVersion function from build/build.go has been factored out to the version package, and simplified to use git describe instead of the multi-step git rev-parse and git diff-index. Where this previously output the version as

<sha>[dirty]

It now outputs it as

<last tag>-<num commits since tag>-<short sha>

Optionally with a trailing -dirty.

The -sha flag has been removed from build.go as we get the information directly from git.

Go has different semantics when you run with `go run .` vs `go run
main.go`.

In the former case, Go provides information about the module, including
some version info. If the tree is not on a semver tag, it outputs
"(devel)".

In the latter case, Go provides no information about the module and
running `go run main.go version` would output a blank line.

The getVersion function from build/build.go has been factored out to the
version package, and simplified to use `git describe` instead of the
multi-step `git rev-parse` and `git diff-index`. Where this previously
output the version as

    <sha>[dirty]

It now outputs it as

    <last tag>-<num commits since tag>-<short sha>

Optionally with a trailing -dirty.

The -sha flag has been removed from build.go as we get the information
directly from git.
@tlimoncelli
Copy link
Collaborator

Awesome! This is a big improvement and will help clarify things when bugs are reported. Thanks!

@tlimoncelli tlimoncelli changed the title BUG: Fix version running main.go directly BUG: Output better "version" string when running main.go directly Jul 9, 2025
@tlimoncelli tlimoncelli merged commit 74e1bb5 into StackExchange:main Jul 9, 2025
@jamesog jamesog deleted the fix-version branch July 9, 2025 20:57
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.

2 participants