buildinfo

package
v1.0.0-beta.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package buildinfo wraps runtime/debug.ReadBuildInfo with optional VCS settings injection via -ldflags.

Go's standard `go build` populates VCS settings (vcs.revision, vcs.modified, vcs.time) by inspecting the local Git working tree at build time. In some build environments — sandboxed builders, CI pipelines that ship source as tarballs, vendored snapshots — the working tree is absent and the settings come out empty.

This package lets a build wrapper inject those settings via -ldflags, mirroring what `go build` would have stamped natively:

go build -ldflags "
    -X github.com/dagger/dagger/internal/version/buildinfo.InjectedVCS=git
    -X github.com/dagger/dagger/internal/version/buildinfo.InjectedVCSRevision=$(git rev-parse HEAD)
    -X github.com/dagger/dagger/internal/version/buildinfo.InjectedVCSModified=false
    -X github.com/dagger/dagger/internal/version/buildinfo.InjectedVCSTime=$(git log -1 --format=%cI)
" ./...

At runtime, callers use ReadBuildInfo just like they would runtime/debug's. Non-empty Injected* variables override the corresponding settings; empty ones leave whatever the toolchain stamped natively untouched.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	InjectedVCS         string // VCS system, typically "git"
	InjectedVCSRevision string // commit hash
	InjectedVCSModified string // "true" or "false"
	InjectedVCSTime     string // RFC3339 commit time
)

Set at link time via `go build -ldflags "-X <pkg>.InjectedXxx=..."`. Empty means "no override; defer to whatever runtime/debug.ReadBuildInfo returns".

Functions

func ReadBuildInfo

func ReadBuildInfo() (*debug.BuildInfo, bool)

ReadBuildInfo returns the build information of the running binary, like runtime/debug.ReadBuildInfo, with VCS settings overridden by any non-empty Injected* package variables.

If runtime/debug.ReadBuildInfo returns ok=false (binary built outside module mode) but any Injected* variable is set, ReadBuildInfo still returns a usable *debug.BuildInfo populated with the injected settings, and ok=true.

Example

ExampleReadBuildInfo shows the typical pattern for reading VCS info from the running binary. Outside any special build environment this returns the same data as runtime/debug.ReadBuildInfo. Under build wrappers that injected values via -ldflags (e.g. -X github.com/dagger/dagger/internal/version/buildinfo.InjectedVCSRevision=…), the injected values appear here too.

package main

import (
	"fmt"

	"github.com/dagger/dagger/internal/version/buildinfo"
)

func main() {
	info, ok := buildinfo.ReadBuildInfo()
	if !ok {
		return
	}
	for _, s := range info.Settings {
		switch s.Key {
		case "vcs.revision":
			fmt.Println("commit:", s.Value)
		case "vcs.modified":
			fmt.Println("dirty: ", s.Value)
		case "vcs.time":
			fmt.Println("time:  ", s.Value)
		}
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL