Skip to content

Commit eb5e08c

Browse files
leodidoona-agent
andcommitted
fix: add -trimpath to default Go build command for reproducible builds
Without -trimpath, Go embeds absolute file paths in the binary, which vary between build machines and break reproducibility. Co-authored-by: Ona <[email protected]>
1 parent 740a63f commit eb5e08c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pkg/leeway/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,8 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
21242124
if len(cfg.BuildCommand) > 0 {
21252125
buildCmd = cfg.BuildCommand
21262126
} else if cfg.Packaging == GoApp {
2127-
buildCmd = []string{goCommand, "build"}
2127+
// Use -trimpath for reproducible builds: removes absolute file paths from the binary
2128+
buildCmd = []string{goCommand, "build", "-trimpath"}
21282129
buildCmd = append(buildCmd, cfg.BuildFlags...)
21292130
buildCmd = append(buildCmd, ".")
21302131
}

pkg/leeway/build_internal_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,42 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"slices"
1011
"strings"
1112
"testing"
1213

1314
"github.com/google/go-cmp/cmp"
1415
)
1516

17+
func TestDefaultGoBuildCommand_IncludesTrimpath(t *testing.T) {
18+
// The default Go build command should include -trimpath for reproducible builds.
19+
// Without -trimpath, Go embeds absolute file paths in the binary, which vary
20+
// between build machines and break reproducibility.
21+
22+
// Simulate what buildGo does when no custom buildCommand is specified
23+
goCommand := "go"
24+
buildCmd := []string{goCommand, "build", "-trimpath"}
25+
buildCmd = append(buildCmd, ".")
26+
27+
if !slices.Contains(buildCmd, "-trimpath") {
28+
t.Error("default Go build command should include -trimpath for reproducible builds")
29+
}
30+
31+
// Verify -trimpath comes after "build" and before "."
32+
buildIdx := slices.Index(buildCmd, "build")
33+
trimpathIdx := slices.Index(buildCmd, "-trimpath")
34+
dotIdx := slices.Index(buildCmd, ".")
35+
36+
if buildIdx == -1 || trimpathIdx == -1 || dotIdx == -1 {
37+
t.Fatalf("expected build, -trimpath, and . in command, got: %v", buildCmd)
38+
}
39+
40+
if !(buildIdx < trimpathIdx && trimpathIdx < dotIdx) {
41+
t.Errorf("expected order: build < -trimpath < ., got indices: build=%d, -trimpath=%d, .=%d",
42+
buildIdx, trimpathIdx, dotIdx)
43+
}
44+
}
45+
1646
func TestParseGoCoverOutput(t *testing.T) {
1747
type Expectation struct {
1848
Error string

0 commit comments

Comments
 (0)