cmd/go: mention go.work when local path outside modules in go.work

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>
This commit is contained in:
Michael Matloob 2022-02-08 16:45:17 -05:00
parent e4a173adf6
commit 8ba3ad92eb
6 changed files with 46 additions and 12 deletions

View 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
}

View 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

View 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

View 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

View 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'

View File

@ -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