@@ -20,6 +20,7 @@ import (
20
20
"os"
21
21
pathpkg "path"
22
22
"path/filepath"
23
+ "regexp"
23
24
"runtime"
24
25
"runtime/debug"
25
26
"slices"
@@ -2343,12 +2344,8 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
2343
2344
// since it can include system paths through various linker flags (notably
2344
2345
// -extar, -extld, and -extldflags).
2345
2346
//
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 )
2352
2349
}
2353
2350
appendSetting ("-ldflags" , ldflags )
2354
2351
}
@@ -2506,10 +2503,14 @@ omitVCS:
2506
2503
p .Internal .BuildInfo = info
2507
2504
}
2508
2505
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` )
2513
2514
}
2514
2515
2515
2516
// SafeArg reports whether arg is a "safe" command-line argument,
0 commit comments