diff options
author | Michael Matloob <matloob@golang.org> | 2021-06-28 15:48:03 -0400 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2021-07-30 22:29:52 +0000 |
commit | 47694b59eb30bfe6a1c12a2eaaf631a4e956b9c7 (patch) | |
tree | 37f2aa8c6b76c71f7241ca604dbc02261e2ab960 /src/cmd/go/internal/modload/import.go | |
parent | 90830699aee61a154e989b2d9f8ce3ff4eabbce1 (diff) | |
download | go-47694b59eb30bfe6a1c12a2eaaf631a4e956b9c7.tar.gz go-47694b59eb30bfe6a1c12a2eaaf631a4e956b9c7.zip |
[dev.cmdgo] cmd/go: provide a more helpful missing required module error in workspaces
If the user is in a workspace, they might not be in the main module
they need to run go get from to add a module that provides a missing
dependency. Figure out what that module is from the import stack (there
might be multiple but we pick according to the stack computed by
the loader for errors) and tell the user to cd to that directory
first in the message.
Change-Id: I7c919eb61ea3dd122334ff1acd2d7e817cad4b25
Reviewed-on: https://go-review.googlesource.com/c/go/+/334940
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/import.go')
-rw-r--r-- | src/cmd/go/internal/modload/import.go | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go index 773d8b600b..088d0c14ec 100644 --- a/src/cmd/go/internal/modload/import.go +++ b/src/cmd/go/internal/modload/import.go @@ -32,7 +32,7 @@ type ImportMissingError struct { Module module.Version QueryErr error - ImportingModule module.Version + ImportingMainModule module.Version // isStd indicates whether we would expect to find the package in the standard // library. This is normally true for all dotless import paths, but replace @@ -73,6 +73,9 @@ func (e *ImportMissingError) Error() string { if e.QueryErr != nil { return fmt.Sprintf("%s: %v", message, e.QueryErr) } + if e.ImportingMainModule.Path != "" && e.ImportingMainModule != MainModules.ModContainingCWD() { + return fmt.Sprintf("%s; to add it:\n\tcd %s\n\tgo get %s", message, MainModules.ModRoot(e.ImportingMainModule), e.Path) + } return fmt.Sprintf("%s; to add it:\n\tgo get %s", message, e.Path) } |