1
0
mirror of https://github.com/golang/go synced 2024-11-17 09:14:46 -07:00

cmd/go: report 'go get' errors for absolute paths outside module root

'go get' will now check absolute paths without wildcards the same way
it checks relative paths. modload.DirImportPath may be used for both
without converting path separators.

Fixes #38038

Change-Id: I453299898ece58f3b5002a5e80021d6bfe815fdd
Reviewed-on: https://go-review.googlesource.com/c/go/+/226857
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Jay Conrod 2020-04-01 13:08:41 -04:00
parent 98534812bd
commit 44ae94751a
2 changed files with 24 additions and 12 deletions

View File

@ -326,15 +326,18 @@ func runGet(cmd *base.Command, args []string) {
// patterns like golang.org/x/tools/..., which can't be expanded // patterns like golang.org/x/tools/..., which can't be expanded
// during package loading until they're in the build list. // during package loading until they're in the build list.
switch { switch {
case search.IsRelativePath(path): case filepath.IsAbs(path) || search.IsRelativePath(path):
// Relative paths like ../../foo or ../../foo... are restricted to // Absolute paths like C:\foo and relative paths like ../foo...
// matching packages in the main module. If the path is explicit and // are restricted to matching packages in the main module. If the path
// contains no wildcards (...), check that it is a package in // is explicit and contains no wildcards (...), check that it is a
// the main module. If the path contains wildcards but matches no // package in the main module. If the path contains wildcards but
// packages, we'll warn after package loading. // matches no packages, we'll warn after package loading.
if !strings.Contains(path, "...") { if !strings.Contains(path, "...") {
pkgPath := modload.DirImportPath(filepath.FromSlash(path)) var pkgs []string
if pkgs := modload.TargetPackages(pkgPath); len(pkgs) == 0 { if pkgPath := modload.DirImportPath(path); pkgPath != "." {
pkgs = modload.TargetPackages(pkgPath)
}
if len(pkgs) == 0 {
abs, err := filepath.Abs(path) abs, err := filepath.Abs(path)
if err != nil { if err != nil {
abs = path abs = path
@ -520,7 +523,7 @@ func runGet(cmd *base.Command, args []string) {
// If the pattern did not match any packages, look up a new module. // If the pattern did not match any packages, look up a new module.
// If the pattern doesn't match anything on the last iteration, // If the pattern doesn't match anything on the last iteration,
// we'll print a warning after the outer loop. // we'll print a warning after the outer loop.
if !search.IsRelativePath(arg.path) && !match.IsLiteral() && arg.path != "all" { if !match.IsLocal() && !match.IsLiteral() && arg.path != "all" {
addQuery(&query{querySpec: querySpec{path: arg.path, vers: arg.vers}, arg: arg.raw}) addQuery(&query{querySpec: querySpec{path: arg.path, vers: arg.vers}, arg: arg.raw})
} else { } else {
for _, err := range match.Errs { for _, err := range match.Errs {

View File

@ -1,9 +1,18 @@
env GO111MODULE=on env GO111MODULE=on
[short] skip cp go.mod.orig go.mod
# relative and absolute paths must be within the main module.
! go get -d ..
stderr '^go get \.\.: path '$WORK'[/\\]gopath is not a package in module rooted at '$WORK'[/\\]gopath[/\\]src$'
! go get -d $WORK
stderr '^go get '$WORK': path '$WORK' is not a package in module rooted at '$WORK'[/\\]gopath[/\\]src$'
! go get -d ../...
stderr '^go get: pattern \.\./\.\.\.: directory prefix \.\. outside available modules$'
! go get -d $WORK/...
stderr '^go get: pattern '$WORK'[/\\]\.\.\.: directory prefix \.\.[/\\]\.\. outside available modules$'
# @patch and @latest within the main module refer to the current version. # @patch and @latest within the main module refer to the current version.
# The main module won't be upgraded, but missing dependencies will be added. # The main module won't be upgraded, but missing dependencies will be added.
cp go.mod.orig go.mod
go get -d rsc.io/x go get -d rsc.io/x
grep 'rsc.io/quote v1.5.2' go.mod grep 'rsc.io/quote v1.5.2' go.mod
go get -d rsc.io/x@upgrade go get -d rsc.io/x@upgrade
@ -18,7 +27,7 @@ cp go.mod.orig go.mod
stderr '^go get rsc.io/x@latest: can.t request explicit version of path in main module$' stderr '^go get rsc.io/x@latest: can.t request explicit version of path in main module$'
# The main module cannot be updated to a specific version. # The main module cannot be updated to a specific version.
! go get rsc.io/x@v0.1.0 ! go get -d rsc.io/x@v0.1.0
stderr '^go get rsc.io/x@v0.1.0: can.t request explicit version of path in main module$' stderr '^go get rsc.io/x@v0.1.0: can.t request explicit version of path in main module$'
! go get -d rsc.io/x@v0.1.0 ! go get -d rsc.io/x@v0.1.0
stderr '^go get rsc.io/x@v0.1.0: can.t request explicit version of path in main module$' stderr '^go get rsc.io/x@v0.1.0: can.t request explicit version of path in main module$'