Skip to content

Commit

Permalink
cmd/go: mention go.work when local path outside modules in go.work
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
matloob committed Feb 10, 2022
1 parent e4a173a commit 8ba3ad9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/cmd/go/internal/modload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/go/testdata/script/mod_download_partial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/go/testdata/script/mod_fs_patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_list_dir.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_list_replace_dir.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
25 changes: 25 additions & 0 deletions src/cmd/go/testdata/script/work_module_not_in_go_work.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8ba3ad9

Please sign in to comment.