diff options
author | Michael Matloob <matloob@golang.org> | 2021-10-19 14:05:29 -0400 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2021-11-09 22:11:33 +0000 |
commit | 4aa0746f6abae7dc112883e79f93993a430bd340 (patch) | |
tree | 92fe47ea2427009cc4d2e0d7c82d4c253b81e167 /src/cmd/go/internal/modload/load.go | |
parent | a65a095ca423c21bdd53a6a8300b501b88d60137 (diff) | |
download | go-4aa0746f6abae7dc112883e79f93993a430bd340.tar.gz go-4aa0746f6abae7dc112883e79f93993a430bd340.zip |
cmd/go: add workspace pruning mode
[ this is a roll-forward of golang.org/cl/357169 with minor changes
to fix the cmd/go/internal/modload tests: because they don't run the
go command, some initialization isn't run on the test and modroots is
empty in cases it can't be when the full command setup is done. So
directly check for workFilePath != "" instead of calling inWorkspaceMode
which checks that Init is called first, and check that modRoots is non
empty when calling mustGetSingleMainModule.]
This change corrects a bug in the handling of module loading of
workspaces. Namely, there is an assumption by the module pruning code
that if a root module is selected then the packages of that module can
be resolved without loading the whole module graph. This is not true
in workspace mode because two workspace modules can require different
versions of a dependency. Worse, one workspace module can directly
require a depencency that is transitively required by another
workspace module, changing the version of that module loaded in the
fully expanded graph.
To correct this, a new 'workspace' pruning mode is added where the
roots are the workspace modules themselves, satisfying the assumption
made by the module pruning logic.
The rest of this change accounts for the new pruning mode where it's
used and correctly sets the requirements in this pruning mode.
Change-Id: I8bdf4b30f669c1ded0ed8a5dd202ac8d1939bbbd
Reviewed-on: https://go-review.googlesource.com/c/go/+/362754
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r-- | src/cmd/go/internal/modload/load.go | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index 845bf2f8a2..83fcafead3 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -1004,7 +1004,11 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader { } var err error - ld.requirements, err = convertPruning(ctx, ld.requirements, pruningForGoVersion(ld.GoVersion)) + desiredPruning := pruningForGoVersion(ld.GoVersion) + if ld.requirements.pruning == workspace { + desiredPruning = workspace + } + ld.requirements, err = convertPruning(ctx, ld.requirements, desiredPruning) if err != nil { ld.errorf("go: %v\n", err) } @@ -1246,6 +1250,24 @@ func (ld *loader) updateRequirements(ctx context.Context) (changed bool, err err continue } + if inWorkspaceMode() { + // In workspace mode / workspace pruning mode, the roots are the main modules + // rather than the main module's direct dependencies. The check below on the selected + // roots does not apply. + if mg, err := rs.Graph(ctx); err != nil { + return false, err + } else if _, ok := mg.RequiredBy(dep.mod); !ok { + // dep.mod is not an explicit dependency, but needs to be. + // See comment on error returned below. + pkg.err = &DirectImportFromImplicitDependencyError{ + ImporterPath: pkg.path, + ImportedPath: dep.path, + Module: dep.mod, + } + } + continue + } + if pkg.err == nil && cfg.BuildMod != "mod" { if v, ok := rs.rootSelected(dep.mod.Path); !ok || v != dep.mod.Version { // dep.mod is not an explicit dependency, but needs to be. |