Skip to content

Commit 25bee96

Browse files
Bryan C. MillsFiloSottile
Bryan C. Mills
authored andcommitted
[release-branch.go1.10] cmd/go/internal/get: move wildcard-trimming to before CheckImportPath
Previously, RepoRootForImportPath trimmed certain "..." wildcards from package patterns (even though its name suggests that the argument must be an actual import path). It trimmed at the first path element that was literally "..." (although wildcards in general may appear within a larger path element), and relied on a subsequent check in RepoRootForImportPath to catch confusing resolutions. However, that causes 'go get' with wildcard patterns in fresh paths to fail as of CL 154101: a wildcard pattern is not a valid import path, and fails the path check. (The existing Test{Vendor,Go}Get* packages in go_test.go and vendor_test.go catch the failure, but they are all skipped when the "-short" flag is set — including in all.bash — and we had forgotten to run them separately.) We now trim the path before any element that contains a wildcard, and perform the path check (and repo resolution) on only that prefix. It is possible that the expanded path after fetching the repo will be invalid, but a repository can contain directories that are not valid import paths in general anyway. Fixes #29247 Change-Id: I70fb2f7fc6603b7d339fd6c02e8cdeacfc93fc4b Reviewed-on: https://go-review.googlesource.com/c/154108 Reviewed-by: Russ Cox <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/154111 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent f40c049 commit 25bee96

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/cmd/go/internal/get/get.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var downloadCache = map[string]bool{}
207207
var downloadRootCache = map[string]bool{}
208208

209209
// download runs the download half of the get command
210-
// for the package named by the argument.
210+
// for the package or pattern named by the argument.
211211
func download(arg string, parent *load.Package, stk *load.ImportStack, mode int) {
212212
if mode&load.ResolveImport != 0 {
213213
// Caller is responsible for expanding vendor paths.
@@ -376,7 +376,20 @@ func downloadPackage(p *load.Package) error {
376376
security = web.Insecure
377377
}
378378

379-
if err := CheckImportPath(p.ImportPath); err != nil {
379+
// p can be either a real package, or a pseudo-package whose “import path” is
380+
// actually a wildcard pattern.
381+
// Trim the path at the element containing the first wildcard,
382+
// and hope that it applies to the wildcarded parts too.
383+
// This makes 'go get rsc.io/pdf/...' work in a fresh GOPATH.
384+
importPrefix := p.ImportPath
385+
if i := strings.Index(importPrefix, "..."); i >= 0 {
386+
slash := strings.LastIndexByte(importPrefix[:i], '/')
387+
if slash < 0 {
388+
return fmt.Errorf("cannot expand ... in %q", p.ImportPath)
389+
}
390+
importPrefix = importPrefix[:slash]
391+
}
392+
if err := CheckImportPath(importPrefix); err != nil {
380393
return fmt.Errorf("%s: invalid import path: %v", p.ImportPath, err)
381394
}
382395

@@ -397,7 +410,7 @@ func downloadPackage(p *load.Package) error {
397410
}
398411
repo = remote
399412
if !*getF {
400-
if rr, err := repoRootForImportPath(p.ImportPath, security); err == nil {
413+
if rr, err := repoRootForImportPath(importPrefix, security); err == nil {
401414
repo := rr.repo
402415
if rr.vcs.resolveRepo != nil {
403416
resolved, err := rr.vcs.resolveRepo(rr.vcs, dir, repo)
@@ -414,7 +427,7 @@ func downloadPackage(p *load.Package) error {
414427
} else {
415428
// Analyze the import path to determine the version control system,
416429
// repository, and the import path for the root of the repository.
417-
rr, err := repoRootForImportPath(p.ImportPath, security)
430+
rr, err := repoRootForImportPath(importPrefix, security)
418431
if err != nil {
419432
return err
420433
}

src/cmd/go/internal/get/vcs.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,7 @@ var httpPrefixRE = regexp.MustCompile(`^https?:`)
647647
func repoRootForImportPath(importPath string, security web.SecurityMode) (*repoRoot, error) {
648648
rr, err := repoRootFromVCSPaths(importPath, "", security, vcsPaths)
649649
if err == errUnknownSite {
650-
// If there are wildcards, look up the thing before the wildcard,
651-
// hoping it applies to the wildcarded parts too.
652-
// This makes 'go get rsc.io/pdf/...' work in a fresh GOPATH.
653-
lookup := strings.TrimSuffix(importPath, "/...")
654-
if i := strings.Index(lookup, "/.../"); i >= 0 {
655-
lookup = lookup[:i]
656-
}
657-
rr, err = repoRootForImportDynamic(lookup, security)
650+
rr, err = repoRootForImportDynamic(importPath, security)
658651
if err != nil {
659652
err = fmt.Errorf("unrecognized import path %q (%v)", importPath, err)
660653
}
@@ -667,6 +660,7 @@ func repoRootForImportPath(importPath string, security web.SecurityMode) (*repoR
667660
}
668661
}
669662

663+
// Should have been taken care of above, but make sure.
670664
if err == nil && strings.Contains(importPath, "...") && strings.Contains(rr.root, "...") {
671665
// Do not allow wildcards in the repo root.
672666
rr = nil

0 commit comments

Comments
 (0)