From e1fbf13896feb0b55050c22f5be0746f8a2ba01b Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 9 Mar 2022 12:01:16 -0500 Subject: [PATCH] cmd/go: include the "-trimpath" flag in the stamped build settings The -trimpath flag has a strong effect on the resulting binary: in particular, it determines whether runtime.GOROOT can report a meaningful path in the absence of an explicit GOROOT environment variable. For #51461 Change-Id: Id0d55572c0a0a4e2e4724363ed80dfa05b202186 Reviewed-on: https://go-review.googlesource.com/c/go/+/391810 Trust: Bryan Mills Reviewed-by: Russ Cox Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot --- src/cmd/go/internal/load/pkg.go | 3 +++ src/cmd/go/testdata/script/version_build_settings.txt | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index ab70845959..2592cf5447 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2333,6 +2333,9 @@ func (p *Package) setBuildInfo(includeVCS bool) { if tags := cfg.BuildContext.BuildTags; len(tags) > 0 { appendSetting("-tags", strings.Join(tags, ",")) } + if cfg.BuildTrimpath { + appendSetting("-trimpath", "true") + } cgo := "0" if cfg.BuildContext.CgoEnabled { cgo = "1" diff --git a/src/cmd/go/testdata/script/version_build_settings.txt b/src/cmd/go/testdata/script/version_build_settings.txt index dc9e67681e..90c7253764 100644 --- a/src/cmd/go/testdata/script/version_build_settings.txt +++ b/src/cmd/go/testdata/script/version_build_settings.txt @@ -23,6 +23,10 @@ go build -ldflags=example.com/m=-w go version -m m$GOEXE stdout '^\tbuild\t-ldflags=example\.com/m=-w$' +go build -trimpath +go version -m m$GOEXE +stdout '\tbuild\t-trimpath=true$' + # gccgoflags are not added when gc is used, and vice versa. # TODO: test gccgo. go build -gccgoflags=all=UNUSED -- 2.48.1