]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add a better error message when in a module outside workspace
authorMichael Matloob <matloob@golang.org>
Tue, 12 Apr 2022 19:17:18 +0000 (15:17 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 21 Apr 2022 18:22:57 +0000 (18:22 +0000)
When the user is trying to list or build a package in a module that's
outside of the workspace provide a more clear message hinting to the
user that they can add the module to the workspace using go work use.

Fixes #51604

Change-Id: I1202ecb2f22fd6351bfdec88ed613b8167687fb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/400014
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Auto-Submit: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/work_module_not_in_go_work.txt

index e85a33dd50666dc880c0bfefda61eb82b559be95..7f1a88ffe7bc2889c82baf7538f5a70245ecd0e5 100644 (file)
@@ -605,11 +605,13 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
 
        pkg := pathInModuleCache(ctx, absDir, rs)
        if pkg == "" {
-               scope := "main module or its selected dependencies"
                if inWorkspaceMode() {
-                       scope = "modules listed in go.work or their selected dependencies"
+                       if mr := findModuleRoot(absDir); mr != "" {
+                               return "", fmt.Errorf("directory %s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use %s", base.ShortPath(absDir), base.ShortPath(mr))
+                       }
+                       return "", fmt.Errorf("directory %s outside modules listed in go.work or their selected dependencies", base.ShortPath(absDir))
                }
-               return "", fmt.Errorf("directory %s outside %s", base.ShortPath(absDir), scope)
+               return "", fmt.Errorf("directory %s outside main module or its selected dependencies", base.ShortPath(absDir))
        }
        return pkg, nil
 }
index 23d908c3022441934dc287b33d7441c73c66239f..9109b2de7f21c1ec29fade0e11fc3c249d2400de 100644 (file)
@@ -6,8 +6,8 @@
 ! go list ./...
 stderr 'pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies'
 
-! go list ./a
-stderr 'directory a outside modules listed in go.work'
+! go list ./a/c
+stderr 'directory a[\\/]c is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use a'
 
 -- go.work --
 go 1.18
@@ -19,6 +19,8 @@ module example.com/a
 go 1.18
 -- a/a.go --
 package a
+-- a/c/c.go --
+package c
 -- b/go.mod --
 module example.com/b