diff options
author | Jay Conrod <jayconrod@google.com> | 2020-12-22 16:57:46 -0500 |
---|---|---|
committer | Jay Conrod <jayconrod@google.com> | 2020-12-22 23:39:46 +0000 |
commit | 223331fc0cf5b23fbb9999eb1164b23695ef612a (patch) | |
tree | 430af57c24c25f15891e6e222bf9fe4f35006764 /src/cmd/go/internal/modload/load.go | |
parent | c9fb4eb0a22131cc9922fa96afba01d4e21d4fd4 (diff) | |
download | go-223331fc0cf5b23fbb9999eb1164b23695ef612a.tar.gz go-223331fc0cf5b23fbb9999eb1164b23695ef612a.zip |
cmd/go/internal/modload: add hint for missing implicit dependency
By default (and with -mod=readonly), the go command imports an error
if a package provided by an implicitly required module is
imported by a package in the main module. This import requires an
update to go.mod: the module must be required explicitly.
The package loader now provides a hint that 'go get' should be run on
the importing package. This is preferred to 'go get' on the imported
package, since that would add an "// indirect" requirement.
For #43131
Change-Id: I0b353ce8ac8c4ddf1a9863544dfaf6c1964daf42
Reviewed-on: https://go-review.googlesource.com/c/go/+/279528
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
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 | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index a0f93d028a..27f47fad4d 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -863,12 +863,21 @@ func loadFromRoots(params loaderParams) *loader { for _, pkg := range ld.pkgs { if pkg.mod == Target { for _, dep := range pkg.imports { - if dep.mod.Path != "" { + if dep.mod.Path != "" && dep.mod.Path != Target.Path && index != nil { + _, explicit := index.require[dep.mod] + if allowWriteGoMod && cfg.BuildMod == "readonly" && !explicit { + // TODO(#40775): attach error to package instead of using + // base.Errorf. Ideally, 'go list' should not fail because of this, + // but today, LoadPackages calls WriteGoMod unconditionally, which + // would fail with a less clear message. + base.Errorf("go: %[1]s: package %[2]s imported from implicitly required module; try 'go get -d %[1]s' to add missing requirements", pkg.path, dep.path) + } ld.direct[dep.mod.Path] = true } } } } + base.ExitIfErrors() // If we didn't scan all of the imports from the main module, or didn't use // imports.AnyTags, then we didn't necessarily load every package that |