cmd/go: introduce WriteOpts argument for WriteGoMod

This CL is a no-op, just adding the new options and plumbing it through.
'go get' will use this option to let commitRequirements know whether
toolchain was mentioned explicitly on the command line.

For #57001.

Change-Id: Iee7145f3335e899704df3e98fb840f1aa4063b0c
Reviewed-on: https://go-review.googlesource.com/c/go/+/499555
Run-TryBot: Russ Cox <rsc@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Russ Cox 2023-05-31 09:57:03 -04:00 committed by Gopher Robot
parent 51114a3fa5
commit e73e5d80ea
6 changed files with 15 additions and 11 deletions

View File

@ -187,7 +187,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
// TODO(#45551): In the future, report an error if go.mod or go.sum need to
// be updated after loading the build list. This may require setting
// the mode to "mod" or "readonly" depending on haveExplicitArgs.
if err := modload.WriteGoMod(ctx); err != nil {
if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
base.Fatalf("go: %v", err)
}
}
@ -266,7 +266,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
// Don't save sums for 'go mod download' without arguments unless we're in
// workspace mode; see comment above.
if haveExplicitArgs || modload.WorkFilePath() != "" {
if err := modload.WriteGoMod(ctx); err != nil {
if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
base.Errorf("go: %v", err)
}
}

View File

@ -379,7 +379,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
// Everything succeeded. Update go.mod.
oldReqs := reqsFromGoMod(modload.ModFile())
if err := modload.WriteGoMod(ctx); err != nil {
if err := modload.WriteGoMod(ctx, modload.WriteOpts{}); err != nil {
if tooNew, ok := err.(*gover.TooNewError); ok {
// This can happen for 'go get go@newversion'
// when all the required modules are old enough

View File

@ -940,7 +940,7 @@ func CreateModFile(ctx context.Context, modPath string) {
base.Fatalf("go: %v", err)
}
requirements = rs
if err := commitRequirements(ctx); err != nil {
if err := commitRequirements(ctx, WriteOpts{}); err != nil {
base.Fatalf("go: %v", err)
}
@ -1515,10 +1515,14 @@ func findImportComment(file string) string {
return path
}
// WriteOpts control the behavior of WriteGoMod.
type WriteOpts struct {
}
// WriteGoMod writes the current build list back to go.mod.
func WriteGoMod(ctx context.Context) error {
func WriteGoMod(ctx context.Context, opts WriteOpts) error {
requirements = LoadModFile(ctx)
return commitRequirements(ctx)
return commitRequirements(ctx, opts)
}
// commitRequirements ensures go.mod and go.sum are up to date with the current
@ -1530,7 +1534,7 @@ func WriteGoMod(ctx context.Context) error {
// go.mod or go.sum are out of date in a semantically significant way.
//
// In workspace mode, commitRequirements only writes changes to go.work.sum.
func commitRequirements(ctx context.Context) (err error) {
func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
if inWorkspaceMode() {
// go.mod files aren't updated in workspace mode, but we still want to
// update the go.work.sum file.

View File

@ -111,7 +111,7 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
if err == nil {
requirements = rs
if !ExplicitWriteGoMod {
err = commitRequirements(ctx)
err = commitRequirements(ctx, WriteOpts{})
}
}
return mods, err

View File

@ -450,7 +450,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
sort.Strings(loadedPackages)
if !ExplicitWriteGoMod && opts.ResolveMissingImports {
if err := commitRequirements(ctx); err != nil {
if err := commitRequirements(ctx, WriteOpts{}); err != nil {
base.Fatalf("go: %v", err)
}
}
@ -733,7 +733,7 @@ func ImportFromFiles(ctx context.Context, gofiles []string) {
requirements = loaded.requirements
if !ExplicitWriteGoMod {
if err := commitRequirements(ctx); err != nil {
if err := commitRequirements(ctx, WriteOpts{}); err != nil {
base.Fatalf("go: %v", err)
}
}

View File

@ -122,7 +122,7 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) {
SilenceMissingStdImports: true,
SilencePackageErrors: true,
}, "all")
modload.WriteGoMod(ctx)
modload.WriteGoMod(ctx, modload.WriteOpts{})
}
wf, err := modload.ReadWorkFile(workFilePath)