]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go: mention go.work when local path outside modules in go.work
authorMichael Matloob <matloob@golang.org>
Tue, 8 Feb 2022 21:45:17 +0000 (16:45 -0500)
committerMichael Matloob <matloob@golang.org>
Thu, 10 Feb 2022 19:08:19 +0000 (19:08 +0000)
In workspace mode, if a user lists a package or patternthat's inside a
module that's not listed in go.work, mention that the package or pattern
is outside the modules listed in go.work so the user has a better idea
of how to fix the issue.

(Question: it's valid in those flows to add a pattern that points into
the module cache. Should we expand the error to say "package outside
modules listed in go.work file or contained in module cache"? That seems
clunky (and is the uncommon case) which is why I didn't do so in this
case, but it's possible)

Fixes #49632

Change-Id: I3f0ea1b2f566d52a8079b58593fcc5cc095e7a41
Reviewed-on: https://go-review.googlesource.com/c/go/+/384236
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: 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/mod_download_partial.txt
src/cmd/go/testdata/script/mod_fs_patterns.txt
src/cmd/go/testdata/script/mod_list_dir.txt
src/cmd/go/testdata/script/mod_list_replace_dir.txt
src/cmd/go/testdata/script/work_module_not_in_go_work.txt [new file with mode: 0644]

index 617b634d263e335946ef51ee84a26eb0cb0dc4d8..a4a7cb263eb70834f2941e4afa06a11534877a57 100644 (file)
@@ -479,7 +479,11 @@ func matchLocalDirs(ctx context.Context, modRoots []string, m *search.Match, rs
                }
                if !found && search.InDir(absDir, cfg.GOROOTsrc) == "" && pathInModuleCache(ctx, absDir, rs) == "" {
                        m.Dirs = []string{}
-                       m.AddError(fmt.Errorf("directory prefix %s outside available modules", base.ShortPath(absDir)))
+                       scope := "main module or its selected dependencies"
+                       if inWorkspaceMode() {
+                               scope = "modules listed in go.work or their selected dependencies"
+                       }
+                       m.AddError(fmt.Errorf("directory prefix %s does not contain %s", base.ShortPath(absDir), scope))
                        return
                }
        }
@@ -601,7 +605,11 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
 
        pkg := pathInModuleCache(ctx, absDir, rs)
        if pkg == "" {
-               return "", fmt.Errorf("directory %s outside available modules", base.ShortPath(absDir))
+               scope := "main module or its selected dependencies"
+               if inWorkspaceMode() {
+                       scope = "modules listed in go.work or their selected dependencies"
+               }
+               return "", fmt.Errorf("directory %s outside %s", base.ShortPath(absDir), scope)
        }
        return pkg, nil
 }
index 3a02fcd7474b5b795f3159d1ac718e3b541f3764..617b1fd8e363e3bb98a605a021024677d5e507d5 100644 (file)
@@ -15,12 +15,13 @@ cp empty $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.partial
 go mod verify
 
 # 'go list' should not load packages from the directory.
-# NOTE: the message "directory $dir outside available modules" is reported
-# for directories not in the main module, active modules in the module cache,
-# or local replacements. In this case, the directory is in the right place,
-# but it's incomplete, so 'go list' acts as if it's not an active module.
+# NOTE: the message "directory $dir outside main module or its selected dependencies"
+# is reported for directories not in the main module, active modules in the
+# module cache, or local replacements. In this case, the directory is in the
+# right place, but it's incomplete, so 'go list' acts as if it's not an
+# active module.
 ! go list $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
-stderr 'outside available modules'
+stderr 'outside main module or its selected dependencies'
 
 # 'go list -m' should not print the directory.
 go list -m -f '{{.Dir}}' rsc.io/quote
index a20fefd6d396d9d20ba56c02306ae4ba71563ef3..276d04e538e0b10945b74c317f55076040d36e81 100644 (file)
@@ -51,11 +51,11 @@ stdout '^at$'
 # a package path.
 cd ../badat/bad@
 ! go list .
-stderr 'directory . outside available modules'
+stderr 'directory . outside main module or its selected dependencies'
 ! go list $PWD
-stderr 'directory . outside available modules'
+stderr 'directory . outside main module or its selected dependencies'
 ! go list $PWD/...
-stderr 'directory . outside available modules'
+stderr 'directory . outside main module or its selected dependencies'
 
 -- x/go.mod --
 module m
index 7ad65ffbc734dea082e662c407a40e732913a33f..157d3b6a8a66721d1db65b4329b3ae64a9c6d935 100644 (file)
@@ -24,7 +24,7 @@ go get rsc.io/sampler@v1.3.1
 go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/sampler@v1.3.1
 stdout '^rsc.io/sampler$'
 ! go list -f '{{.ImportPath}}' $GOPATH/pkg/mod/rsc.io/sampler@v1.3.0
-stderr 'outside available modules'
+stderr 'outside main module or its selected dependencies'
 
 -- go.mod --
 module x
index eac5ca7dd322040b1dd0dc6a7dc6128b823b8772..b446543916f53bd04dc8bd4704059a62fb7a3bdc 100644 (file)
@@ -9,7 +9,7 @@ go get
 go mod download rsc.io/quote@v1.5.2
 
 ! go list $GOPATH/pkg/mod/rsc.io/quote@v1.5.2
-stderr '^directory ..[/\\]pkg[/\\]mod[/\\]rsc.io[/\\]quote@v1.5.2 outside available modules$'
+stderr '^directory ..[/\\]pkg[/\\]mod[/\\]rsc.io[/\\]quote@v1.5.2 outside main module or its selected dependencies$'
 
 go list $GOPATH/pkg/mod/rsc.io/quote@v1.5.1
 stdout 'rsc.io/quote'
diff --git a/src/cmd/go/testdata/script/work_module_not_in_go_work.txt b/src/cmd/go/testdata/script/work_module_not_in_go_work.txt
new file mode 100644 (file)
index 0000000..23d908c
--- /dev/null
@@ -0,0 +1,25 @@
+# This is a regression test for issue #49632.
+# The Go command should mention go.work if the user
+# tries to load a local package that's in a module
+# that's not in go.work and can't be resolved.
+
+! 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.work --
+go 1.18
+
+use ./b
+-- a/go.mod --
+module example.com/a
+
+go 1.18
+-- a/a.go --
+package a
+-- b/go.mod --
+module example.com/b
+
+go 1.18