Skip to content

Commit d4e2128

Browse files
committed
cmd/go: add minimal module-awareness for legacy operation
We want authors to be able to publish code that works with both the current standard go command and the planned new go command support for modules. If authors have tagged their code v2 or later, semantic import versioning means the import paths must include a v2 path element after the path prefix naming the module. One option for making this convention compatible with original go get is to move code into a v2 subdirectory of the root. That makes sense for some authors, but many authors would prefer not to move all the code into a v2 subdirectory for a transition and then move it back up once we everyone has a module-aware go command. Instead, this CL teaches the old (non-module-aware) go command a tiny amount about modules and their import paths, to expand the options for authors who want to publish compatible packages. If an author has a v2 of a package, say my/thing/v2/sub/pkg, in the my/thing repo's sub/pkg subdirectory (no v2 in the file system path), then old go get continues to import that package as my/thing/sub/pkg. But when go get is processing code in any module (code in a tree with a go.mod file) and encounters a path like my/thing/v2/sub/pkg, it will check to see if my/thing/go.mod says "module my/thing/v2". If so, the go command will read the import my/thing/v2/sub/pkg as if it said my/thing/sub/pkg, which is the correct "old" import path for the package in question. This CL will be back-ported to Go 1.10 and Go 1.9 as well. Once users have updated to the latest Go point releases containing this new logic, authors will be able to update to using modules within their own repos, including using semantic import paths with vN path elements, and old go get will still be able to consume those repositories. This CL also makes "go get" ignore meta go-import lines using the new "mod" VCS type. This allows a package to specify both a "mod" type and a "git" type, to present more efficient module access to module-aware go but still present a Git repo to the old "go get". Fixes #24751. Fixes #25069. Change-Id: I378955613a0d63834d4f50f121f4db7e4d87dc0a Reviewed-on: https://go-review.googlesource.com/109340 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent b9ecac0 commit d4e2128

File tree

18 files changed

+296
-41
lines changed

18 files changed

+296
-41
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
5555
continue
5656
}
5757
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
58+
// Ignore VCS type "mod", which is new Go modules.
59+
// This code is for old go get and must ignore the new mod lines.
60+
// Otherwise matchGoImport will complain about two
61+
// different metaImport lines for the same Prefix.
62+
if f[1] == "mod" {
63+
continue
64+
}
5865
imports = append(imports, metaImport{
5966
Prefix: f[0],
6067
VCS: f[1],

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ var downloadRootCache = map[string]bool{}
209209
// download runs the download half of the get command
210210
// for the package named by the argument.
211211
func download(arg string, parent *load.Package, stk *load.ImportStack, mode int) {
212-
if mode&load.UseVendor != 0 {
212+
if mode&load.ResolveImport != 0 {
213213
// Caller is responsible for expanding vendor paths.
214214
panic("internal error: download mode has useVendor set")
215215
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ var parseMetaGoImportsTests = []struct {
4747
{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
4848
},
4949
},
50+
{
51+
`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
52+
<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`,
53+
[]metaImport{
54+
{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
55+
},
56+
},
57+
{
58+
`<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
59+
<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
60+
[]metaImport{
61+
{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
62+
},
63+
},
5064
{
5165
`<head>
5266
<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">

src/cmd/go/internal/list/list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ func runList(cmd *base.Command, args []string) {
296296

297297
for _, p := range pkgs {
298298
// Show vendor-expanded paths in listing
299-
p.TestImports = p.Vendored(p.TestImports)
300-
p.XTestImports = p.Vendored(p.XTestImports)
299+
p.TestImports = p.Resolve(p.TestImports)
300+
p.XTestImports = p.Resolve(p.XTestImports)
301301
}
302302

303303
if *listTest {

0 commit comments

Comments
 (0)