Skip to content

Commit 7f6c9d5

Browse files
trim paths from ldflags in BuildInfo instead of removing them
Fixes #63432
1 parent 3145a2f commit 7f6c9d5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2348,8 +2348,9 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
23482348
// redact only those paths from the recorded -ldflags setting and still
23492349
// record the system-independent parts of the flags.
23502350
if !cfg.BuildTrimpath {
2351-
appendSetting("-ldflags", ldflags)
2351+
ldflags = trimLdFlags(ldflags)
23522352
}
2353+
appendSetting("-ldflags", ldflags)
23532354
}
23542355
if cfg.BuildCover {
23552356
appendSetting("-cover", "true")
@@ -2505,6 +2506,12 @@ omitVCS:
25052506
p.Internal.BuildInfo = info
25062507
}
25072508

2509+
// trimLdFlags replaces know paths with variable and removes
2510+
// flags with absolute paths
2511+
func trimLdFlags(flags string) string {
2512+
return flags
2513+
}
2514+
25082515
// SafeArg reports whether arg is a "safe" command-line argument,
25092516
// meaning that when it appears in a command-line, it probably
25102517
// doesn't have some special meaning other than its own name.

src/cmd/go/internal/load/pkg_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,27 @@ build -ldflags="-flag1 -flag2"
247247
build CGO_ENABLED=0
248248
build GOARCH=foo
249249
build GOOS=bar
250+
`,
251+
},
252+
{
253+
name: "ldflags with trimpath",
254+
buildContext: map[string]string{
255+
"arch": "foo",
256+
"os": "bar",
257+
"compiler": "baz",
258+
"cgo": "false",
259+
"ldflags": "-flag1 -flag2",
260+
"trimpath": "true",
261+
},
262+
pkg: Package{},
263+
autoVCS: true,
264+
want: `build -buildmode=
265+
build -compiler=baz
266+
build -ldflags="-flag1 -flag2"
267+
build -trimpath=true
268+
build CGO_ENABLED=0
269+
build GOARCH=foo
270+
build GOOS=bar
250271
`,
251272
},
252273
{

0 commit comments

Comments
 (0)