Skip to content

Commit 446d76e

Browse files
committed
cmd/go: fix 'go get' compatibility for direct download of vgo-aware module
CL 109340 added “minimal module-awareness for legacy operation.” One part of that is reinterpreting imports inside code trees with go.mod files as using semantic import versioning, and converting them back to legacy import paths by stripping the major version element (for example, interpreting import "x.com/foo/v2/bar" as import "x.com/foo/bar"). This rewrite was not being applied during "go get", with the effect that once you had the target code downloaded already, everything was fine, but it didn't download and build successfully the first time. Fixes #25687. Change-Id: I3e122efdc8fd9a0a4e66f5aa3e6a99f90c7df779 Reviewed-on: https://go-review.googlesource.com/115797 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 41dd2eb commit 446d76e

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
217217
if parent == nil {
218218
return load.LoadPackage(path, stk)
219219
}
220-
return load.LoadImport(path, parent.Dir, parent, stk, nil, mode)
220+
return load.LoadImport(path, parent.Dir, parent, stk, nil, mode|load.ResolveModule)
221221
}
222222

223223
p := load1(arg, mode)
@@ -346,12 +346,12 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
346346
base.Errorf("%s", err)
347347
continue
348348
}
349-
// If this is a test import, apply vendor lookup now.
350-
// We cannot pass useVendor to download, because
349+
// If this is a test import, apply module and vendor lookup now.
350+
// We cannot pass ResolveImport to download, because
351351
// download does caching based on the value of path,
352352
// so it must be the fully qualified path already.
353353
if i >= len(p.Imports) {
354-
path = load.VendoredImportPath(p, path)
354+
path = load.ResolveImportPath(p, path)
355355
}
356356
download(path, p, stk, 0)
357357
}

src/cmd/go/internal/load/pkg.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ const (
404404
// disallowVendor will reject direct use of paths containing /vendor/.
405405
ResolveImport = 1 << iota
406406

407+
// ResolveModule is for download (part of "go get") and indicates
408+
// that the module adjustment should be done, but not vendor adjustment.
409+
ResolveModule
410+
407411
// GetTestDeps is for download (part of "go get") and indicates
408412
// that test dependencies should be fetched too.
409413
GetTestDeps
@@ -434,6 +438,9 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo
434438
// The code is also needed in a few other places anyway.
435439
path = ResolveImportPath(parent, path)
436440
importPath = path
441+
} else if mode&ResolveModule != 0 {
442+
path = ModuleImportPath(parent, path)
443+
importPath = path
437444
}
438445

439446
p := packageCache[importPath]

src/cmd/go/vendor_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"fmt"
1212
"internal/testenv"
13+
"os"
1314
"path/filepath"
1415
"regexp"
1516
"strings"
@@ -352,10 +353,51 @@ func TestModLegacyGet(t *testing.T) {
352353
tg := testgo(t)
353354
defer tg.cleanup()
354355
tg.makeTempdir()
355-
tg.setenv("GOPATH", tg.path("."))
356+
tg.setenv("GOPATH", tg.path("d1"))
356357
tg.run("get", "vcs-test.golang.org/git/modlegacy1-old.git/p1")
357358
tg.run("list", "-f", "{{.Deps}}", "vcs-test.golang.org/git/modlegacy1-old.git/p1")
358359
tg.grepStdout("new.git/p2", "old/p1 should depend on new/p2")
359360
tg.grepStdoutNot("new.git/v2/p2", "old/p1 should NOT depend on new/v2/p2")
360361
tg.run("build", "vcs-test.golang.org/git/modlegacy1-old.git/p1", "vcs-test.golang.org/git/modlegacy1-new.git/p1")
362+
363+
tg.setenv("GOPATH", tg.path("d2"))
364+
365+
tg.must(os.RemoveAll(tg.path("d2")))
366+
tg.run("get", "github.com/rsc/vgotest5")
367+
tg.run("get", "github.com/rsc/vgotest4")
368+
tg.run("get", "github.com/myitcv/vgo_example_compat")
369+
370+
if testing.Short() {
371+
return
372+
}
373+
374+
tg.must(os.RemoveAll(tg.path("d2")))
375+
tg.run("get", "github.com/rsc/vgotest4")
376+
tg.run("get", "github.com/rsc/vgotest5")
377+
tg.run("get", "github.com/myitcv/vgo_example_compat")
378+
379+
tg.must(os.RemoveAll(tg.path("d2")))
380+
tg.run("get", "github.com/rsc/vgotest4", "github.com/rsc/vgotest5")
381+
tg.run("get", "github.com/myitcv/vgo_example_compat")
382+
383+
tg.must(os.RemoveAll(tg.path("d2")))
384+
tg.run("get", "github.com/rsc/vgotest5", "github.com/rsc/vgotest4")
385+
tg.run("get", "github.com/myitcv/vgo_example_compat")
386+
387+
tg.must(os.RemoveAll(tg.path("d2")))
388+
tg.run("get", "github.com/myitcv/vgo_example_compat")
389+
tg.run("get", "github.com/rsc/vgotest4", "github.com/rsc/vgotest5")
390+
391+
pkgs := []string{"github.com/myitcv/vgo_example_compat", "github.com/rsc/vgotest4", "github.com/rsc/vgotest5"}
392+
for i := 0; i < 3; i++ {
393+
for j := 0; j < 3; j++ {
394+
for k := 0; k < 3; k++ {
395+
if i == j || i == k || k == j {
396+
continue
397+
}
398+
tg.must(os.RemoveAll(tg.path("d2")))
399+
tg.run("get", pkgs[i], pkgs[j], pkgs[k])
400+
}
401+
}
402+
}
361403
}

0 commit comments

Comments
 (0)