aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/load.go
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2021-09-13 16:48:53 -0700
committerJay Conrod <jayconrod@google.com>2021-09-24 17:53:04 +0000
commitb00222fcdd9f2aeb426887b005865eca1aec3631 (patch)
tree304e14c9cf1259263f349688d364e65168efafe9 /src/cmd/go/internal/modload/load.go
parent584afc29289c2f1a204daa39f44a4a4afbacb741 (diff)
downloadgo-b00222fcdd9f2aeb426887b005865eca1aec3631.tar.gz
go-b00222fcdd9f2aeb426887b005865eca1aec3631.zip
cmd/go: refactor {Allow,Disallow}WriteGoMod to ExplicitWriteGoMod
Subcommands may now set the global flag modload.ExplicitWriteGoMod instead of calling {Allow,Disallow}WriteGoMod. When ExplicitWriteGoMod is false (default), modload.LoadPackages and ListModules will either update go.mod and go.sum or report an error if they need to be updated, depending on cfg.BuildMod. When ExplicitWriteGoMod is true, commands must explicitly call modload.WriteGoMod to update go.mod and go.sum or report an error. Commands that perform some operation after loading the build list (like downloading zips or building packages) and commands that load packages multiple times should set this. For now, only 'go get' and 'go mod download' set this. This CL is a pure refactor: no change in behavior is expected. There are some other minor changes in here, too: commitRequirements no longer sets the global requirements: that should be done separately first. Change-Id: I69942a808bb177faf7904a53aaf2d4ac68500e82 Reviewed-on: https://go-review.googlesource.com/c/go/+/349600 Trust: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r--src/cmd/go/internal/modload/load.go18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index 48f268ce5f..3498c662f3 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -327,7 +327,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
}
}
- initialRS, _ := loadModFile(ctx) // Ignore needCommit — we're going to commit at the end regardless.
+ initialRS := LoadModFile(ctx)
ld := loadFromRoots(ctx, loaderParams{
PackageOpts: opts,
@@ -396,7 +396,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
}
}
- if allowWriteGoMod {
+ if !ExplicitWriteGoMod {
modfetch.TrimGoSum(keep)
// commitRequirements below will also call WriteGoSum, but the "keep" map
@@ -417,8 +417,11 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
}
// Success! Update go.mod and go.sum (if needed) and return the results.
+ // We'll skip updating if ExplicitWriteGoMod is true (the caller has opted
+ // to call WriteGoMod itself) or if ResolveMissingImports is false (the
+ // command wants to examine the package graph as-is).
loaded = ld
- commitRequirements(ctx, loaded.requirements)
+ requirements = loaded.requirements
for _, pkg := range ld.pkgs {
if !pkg.isTest() {
@@ -426,6 +429,13 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
}
}
sort.Strings(loadedPackages)
+
+ if !ExplicitWriteGoMod && opts.ResolveMissingImports {
+ if err := commitRequirements(ctx); err != nil {
+ base.Fatalf("go: %v", err)
+ }
+ }
+
return matches, loadedPackages
}
@@ -685,7 +695,7 @@ func ImportFromFiles(ctx context.Context, gofiles []string) {
return roots
},
})
- commitRequirements(ctx, loaded.requirements)
+ requirements = loaded.requirements
}
// DirImportPath returns the effective import path for dir,