diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index cca0b9be8b7..f7ba1b53ff7 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2348,8 +2348,9 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { // redact only those paths from the recorded -ldflags setting and still // record the system-independent parts of the flags. if !cfg.BuildTrimpath { - appendSetting("-ldflags", ldflags) + ldflags = trimLdFlags(ldflags) } + appendSetting("-ldflags", ldflags) } if cfg.BuildCover { appendSetting("-cover", "true") @@ -2505,6 +2506,12 @@ omitVCS: p.Internal.BuildInfo = info } +// trimLdFlags replaces know paths with variable and removes +// flags with absolute paths +func trimLdFlags(flags string) string { + return flags +} + // SafeArg reports whether arg is a "safe" command-line argument, // meaning that when it appears in a command-line, it probably // doesn't have some special meaning other than its own name. diff --git a/src/cmd/go/internal/load/pkg_test.go b/src/cmd/go/internal/load/pkg_test.go index d9494db2271..e14e9ed908b 100644 --- a/src/cmd/go/internal/load/pkg_test.go +++ b/src/cmd/go/internal/load/pkg_test.go @@ -247,6 +247,27 @@ build -ldflags="-flag1 -flag2" build CGO_ENABLED=0 build GOARCH=foo build GOOS=bar +`, + }, + { + name: "ldflags with trimpath", + buildContext: map[string]string{ + "arch": "foo", + "os": "bar", + "compiler": "baz", + "cgo": "false", + "ldflags": "-flag1 -flag2", + "trimpath": "true", + }, + pkg: Package{}, + autoVCS: true, + want: `build -buildmode= +build -compiler=baz +build -ldflags="-flag1 -flag2" +build -trimpath=true +build CGO_ENABLED=0 +build GOARCH=foo +build GOOS=bar `, }, {