Skip to content

Commit 2d16b83

Browse files
committed
go/vcs: ignore "mod" VCS type
golang.org/x/tools/go/vcs is significantly behind the upstream cmd/go/internal/get code, and has no support for modules. It continues to implement mechanics for GOPATH mode only. This change is a minimal fix to get it to continue to work in the presence of the module mode-only "mod" VCS type (documented at https://golang.org/cmd/go/#hdr-Remote_import_paths) by effectively implementing IgnoreMod ModuleMode behavior. It is similar to issue golang/go#24751 and a small subset of CL 109340 that fixed it. This helps with module adoption by reducing the harm of adding the "mod" VCS type for vanity import paths, something that was meant to be backwards compatible. While here, also backport CL 14482 (the Token to RawToken change). Fixes golang/go#31845 Updates golang/go#24751 Change-Id: I0852f52cb9bda56879f923337c7f361df8412845 Reviewed-on: https://go-review.googlesource.com/c/tools/+/175219 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent 3b6f9c0 commit 2d16b83

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

go/vcs/discovery.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ func charsetReader(charset string, input io.Reader) (io.Reader, error) {
2828

2929
// parseMetaGoImports returns meta imports from the HTML in r.
3030
// Parsing ends at the end of the <head> section or the beginning of the <body>.
31+
//
32+
// This copy of cmd/go/internal/vcs.parseMetaGoImports always operates
33+
// in IgnoreMod ModuleMode.
3134
func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
3235
d := xml.NewDecoder(r)
3336
d.CharsetReader = charsetReader
3437
d.Strict = false
3538
var t xml.Token
3639
for {
37-
t, err = d.Token()
40+
t, err = d.RawToken()
3841
if err != nil {
3942
if err == io.EOF || len(imports) > 0 {
4043
err = nil
@@ -55,6 +58,10 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
5558
continue
5659
}
5760
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
61+
// Ignore VCS type "mod", which is applicable only in module mode.
62+
if f[1] == "mod" {
63+
continue
64+
}
5865
imports = append(imports, metaImport{
5966
Prefix: f[0],
6067
VCS: f[1],

go/vcs/vcs_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ var parseMetaGoImportsTests = []struct {
116116
{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
117117
},
118118
},
119+
{
120+
`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
121+
<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`,
122+
[]metaImport{
123+
{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
124+
},
125+
},
126+
{
127+
`<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
128+
<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
129+
[]metaImport{
130+
{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
131+
},
132+
},
119133
{
120134
`<head>
121135
<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
@@ -127,6 +141,21 @@ var parseMetaGoImportsTests = []struct {
127141
<body>`,
128142
[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
129143
},
144+
{
145+
`<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
146+
[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
147+
},
148+
{
149+
// XML doesn't like <div style=position:relative>.
150+
`<!doctype html><title>Page Not Found</title><meta name=go-import content="chitin.io/chitin git https://github.com/chitin-io/chitin"><div style=position:relative>DRAFT</div>`,
151+
[]metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
152+
},
153+
{
154+
`<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
155+
<meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
156+
`,
157+
[]metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}},
158+
},
130159
}
131160

132161
func TestParseMetaGoImports(t *testing.T) {

0 commit comments

Comments
 (0)