Skip to content

Commit 0092318

Browse files
wip
1 parent 7f6c9d5 commit 0092318

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
pathpkg "path"
2222
"path/filepath"
23+
"regexp"
2324
"runtime"
2425
"runtime/debug"
2526
"slices"
@@ -2343,12 +2344,8 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
23432344
// since it can include system paths through various linker flags (notably
23442345
// -extar, -extld, and -extldflags).
23452346
//
2346-
// TODO: since we control cmd/link, in theory we can parse ldflags to
2347-
// determine whether they may refer to system paths. If we do that, we can
2348-
// redact only those paths from the recorded -ldflags setting and still
2349-
// record the system-independent parts of the flags.
2350-
if !cfg.BuildTrimpath {
2351-
ldflags = trimLdFlags(ldflags)
2347+
if cfg.BuildTrimpath {
2348+
ldflags = trimPathsFromLdFlags(ldflags)
23522349
}
23532350
appendSetting("-ldflags", ldflags)
23542351
}
@@ -2506,10 +2503,14 @@ omitVCS:
25062503
p.Internal.BuildInfo = info
25072504
}
25082505

2509-
// trimLdFlags replaces know paths with variable and removes
2510-
// flags with absolute paths
2511-
func trimLdFlags(flags string) string {
2512-
return flags
2506+
// trimLdFlags removes system paths from the ldflag.
2507+
// since we control cmd/link, in theory we parse ldflags to
2508+
// determine whether they refer to system paths. We then can
2509+
// redact only those paths from the recorded -ldflags setting and still
2510+
// record the system-independent parts of the flags.
2511+
func trimPathsFromLdFlags(flags string) string {
2512+
re := regexp.MustCompile(`(?:(['"])|\s)(?:-[A-Z])?\/[aA-zZ0-9][^\s'"]+`)
2513+
return re.ReplaceAllString(flags, `$1`)
25132514
}
25142515

25152516
// SafeArg reports whether arg is a "safe" command-line argument,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ build GOOS=bar
256256
"os": "bar",
257257
"compiler": "baz",
258258
"cgo": "false",
259-
"ldflags": "-flag1 -flag2",
259+
"ldflags": "all=-buildid '1649548598' -linkmode external -extldflags '-L/media/path1 -L/media/path2 -Wl,-z,now -Wl,-z,relro' -X 'github.com/foo/bar/app.Version=v1.2.3'",
260260
"trimpath": "true",
261261
},
262262
pkg: Package{},
263263
autoVCS: true,
264264
want: `build -buildmode=
265265
build -compiler=baz
266-
build -ldflags="-flag1 -flag2"
266+
build -ldflags="all=-buildid '1649548598' -linkmode external -extldflags ' -Wl,-z,now -Wl,-z,relro' -X 'github.com/foo/bar/app.Version=v1.2.3'"
267267
build -trimpath=true
268268
build CGO_ENABLED=0
269269
build GOARCH=foo

0 commit comments

Comments
 (0)