diff options
author | Michael Matloob <matloob@golang.org> | 2021-10-19 14:05:29 -0400 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2021-11-08 21:30:25 +0000 |
commit | 0e39946e8df426b459103ab94256b697a6a4dd9c (patch) | |
tree | 38bb3d762dbf37b90d61ac5daf067a36651c3b15 /src/cmd/go/internal/modload/load.go | |
parent | ccea0b2fbe8eaf0ac69fab4aef28f300bf676d21 (diff) | |
download | go-0e39946e8df426b459103ab94256b697a6a4dd9c.tar.gz go-0e39946e8df426b459103ab94256b697a6a4dd9c.zip |
cmd/go: add workspace pruning mode
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: I5d4d9877e492e196681f6ee9f8f18a08b4e95c61
Reviewed-on: https://go-review.googlesource.com/c/go/+/357169
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
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.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 845bf2f8a23..83fcafead3c 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. |