From faa549e4c7aeb997a7fa802f5f9398db5a57c32c Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 20 Jun 2023 13:14:11 -0400 Subject: [PATCH] cmd/go: keep BuildInfo list sorted even with -pgo The -pgo build setting is added late, so sort it into place. Noticed while working on CL 504536. Change-Id: I080d2389dc0b3176fb72c9e2434e5f3ae70e294e Reviewed-on: https://go-review.googlesource.com/c/go/+/504537 Run-TryBot: Russ Cox Reviewed-by: Cherry Mui Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot --- src/cmd/go/internal/load/pkg.go | 6 +++++- src/cmd/go/testdata/script/build_pgo_auto.txt | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 4ea1ebaa25..0abc09186d 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -2387,10 +2387,10 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { appendSetting("-ldflags", ldflags) } } - // N.B. -pgo added later by setPGOProfilePath. if cfg.BuildMSan { appendSetting("-msan", "true") } + // N.B. -pgo added later by setPGOProfilePath. if cfg.BuildRace { appendSetting("-race", "true") } @@ -2928,6 +2928,10 @@ func setPGOProfilePath(pkgs []*Package) { } else { appendBuildSetting(p.Internal.BuildInfo, "-pgo", file) } + // Adding -pgo breaks the sort order in BuildInfo.Settings. Restore it. + slices.SortFunc(p.Internal.BuildInfo.Settings, func(x, y debug.BuildSetting) int { + return strings.Compare(x.Key, y.Key) + }) } switch cfg.BuildPGO { diff --git a/src/cmd/go/testdata/script/build_pgo_auto.txt b/src/cmd/go/testdata/script/build_pgo_auto.txt index 5dd799a77f..509be0d5c6 100644 --- a/src/cmd/go/testdata/script/build_pgo_auto.txt +++ b/src/cmd/go/testdata/script/build_pgo_auto.txt @@ -14,6 +14,9 @@ stderr 'compile.*-p test/dep.*-pgoprofile=.*default\.pgo' # if the first arg starts with - it is a grep flag. stderr 'build\\t-pgo=.*default\.pgo' +# check also that -pgo appears with the other flags, before non-flag settings +! stderr 'build\\t[A-Za-z].*build\\t-pgo' + # use default.pgo for ... with a single main package go build -n -pgo=auto ./a/... stderr 'compile.*-pgoprofile=.*default\.pgo.*a1.go' -- 2.50.0