Skip to content

Replace embedded version getter with Go build info for version#5790

Open
maruel wants to merge 1 commit into
bluenviron:mainfrom
maruel:caic-0
Open

Replace embedded version getter with Go build info for version#5790
maruel wants to merge 1 commit into
bluenviron:mainfrom
maruel:caic-0

Conversation

@maruel
Copy link
Copy Markdown

@maruel maruel commented May 20, 2026

internal/core/versiongetter/main.go: This utility used go-git to retrieve the version from the repository tags at go generate. Replace it with Go's debug.ReadBuildInfo() (added in go1.12), which provides version and VCS metadata from the binary itself.

scripts/binaries.mk: use variables to shell out less often. Switch from using ./VERSION to using the git tag. This is likely the most tricky part of this change.

scripts/dockerhub.mk already uses git describe --tags | tr -d v as the version. So this does help make both docker version and tarballs versioning more coherent. Note that it's different from what scripts/binaries.mk used. I didn't change the command in script/binaries.mk.

Behavior change:

The local go install automatically bumps the version for build with modified local
changes
:

go generate ./... && go install . && mediamtx --version
v1.18.3-0.20260520142345-762ae87b5f75

Previously:

go generate ./... && go install . && mediamtx --version
v1.18.2-6-111302f9

Ran:

make format lint binaries
make test
make test-32

Both make test and make test-32 "passed" but both printed "/bin/sh: git: not found" at the beginning. Binaries are in the form mediamtx_v1.18.2-7-gdea66680_linux_armv6.tar.gz since I have local changes. This naming comes from git describe --tags.

Potential follow ups:

  • make binaries ask for root access. One idea would be to mount the container with the right user ID so the chown is not necessary.
  • Update internal/upgrade/upgrade.go to use debug.ReadBuildInfo() so go-git is not a dependency anymore.

internal/core/versiongetter/main.go: The versiongetter utility used
go-git to retrieve the version from the repository tags at compilation
time. Replace versiongetter with Go's debug.ReadBuildInfo() (added in
go1.12), which provides version and VCS metadata from the binary itself.

scripts/binaries.mk: use variables to shell out less often. Switch from
using ./VERSION to using the git tag. This is likely the most tricky
part of this change.

scripts/dockerhub.mk already uses `git describe --tags | tr -d v` as the
version. So this does help make both docker version and tarballs
versioning more coherent.

Here's the outcomes:

| Scenario | Returns | Source |
|---|---|---|
| `go install @latest` | `v1.18.2` | `info.Main.Version` |
| `go build` in tagged repo (at tag) | `v1.18.2` | `info.Main.Version` |
| `go build` in tagged repo (past tag) | `v1.18.3-0.TIMESTAMP-HASH` | `info.Main.Version` |
| `go build` in untagged repo (fork) | `v0.0.0-TIMESTAMP-HASH` | `info.Main.Version` (Go pseudo-version) |
| `go build -buildvcs=false` | `devel` | fallback when no VCS settings |
| stripped/broken binary | `unknown` | `debug.ReadBuildInfo()` returned false |

Behavior change:
    The local go install automatically bump the version for modified local
    changes:

    ```
    go install . && mediamtx --version
    v1.18.3-0.20260520142345-762ae87b5f75
    ```

Ran:
    make format lint binaries
    make test
    make test-32

Both make test and make test-32 "passed" but both printed "/bin/sh: git: not found" at the beginning.
Binaries are in the form mediamtx_v1.18.2-7-gdea66680_linux_armv6.tar.gz
since I have local changes.

Potential follow ups:
- make binaries ask for root access. One idea would be to mount the
  container with the right user ID so the chown is not necessary.
- I didn't touch internal/upgrade/upgrade.go, so go-git is still a
  dependency.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 41.66667% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.86%. Comparing base (111302f) to head (f01ff0a).

Files with missing lines Patch % Lines
internal/core/core.go 41.66% 12 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5790      +/-   ##
==========================================
+ Coverage   62.83%   62.86%   +0.03%     
==========================================
  Files         219      219              
  Lines       18659    18677      +18     
==========================================
+ Hits        11725    11742      +17     
- Misses       5973     5974       +1     
  Partials      961      961              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aler9
Copy link
Copy Markdown
Member

aler9 commented May 21, 2026

Hello, the first implementation of the version retrieval system was based on debug.ReadBuildInfo, but it was not compatible with CI/CD at the time. Seems that this issue has disappeared though, so we can give this patch a try. Thanks for providing it.

@aler9
Copy link
Copy Markdown
Member

aler9 commented May 22, 2026

By reviewing this patch i noticed that go install is necessary for the tag to be printed correctly. Am i correct? we cannot make go install mandatory. Is there a way to implement this without go install ?

@maruel
Copy link
Copy Markdown
Author

maruel commented May 22, 2026

By reviewing this patch i noticed that go install is necessary for the tag to be printed correctly. Am i correct? we cannot make go install mandatory. Is there a way to implement this without go install ?

Can you clarify what you mean by go install becoming necessary?

scripts/binaries.mk uses go build as before, I haven't touched this. It is true that the git tag must be created: the binaries job in .github/workflows/release.yml is triggered exactly on git tag push so this still work by definition as before.

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