From 2d16b83fe98cd1bed9e2ce9fdc86bd438d14aab7 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sun, 5 May 2019 13:44:54 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- go/vcs/discovery.go | 9 ++++++++- go/vcs/vcs_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/go/vcs/discovery.go b/go/vcs/discovery.go index f431dc1c..2428d888 100644 --- a/go/vcs/discovery.go +++ b/go/vcs/discovery.go @@ -28,13 +28,16 @@ func charsetReader(charset string, input io.Reader) (io.Reader, error) { // parseMetaGoImports returns meta imports from the HTML in r. // Parsing ends at the end of the section or the beginning of the . +// +// This copy of cmd/go/internal/vcs.parseMetaGoImports always operates +// in IgnoreMod ModuleMode. func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) { d := xml.NewDecoder(r) d.CharsetReader = charsetReader d.Strict = false var t xml.Token for { - t, err = d.Token() + t, err = d.RawToken() if err != nil { if err == io.EOF || len(imports) > 0 { err = nil @@ -55,6 +58,10 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) { continue } if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 { + // Ignore VCS type "mod", which is applicable only in module mode. + if f[1] == "mod" { + continue + } imports = append(imports, metaImport{ Prefix: f[0], VCS: f[1], diff --git a/go/vcs/vcs_test.go b/go/vcs/vcs_test.go index d18ff18e..b725f018 100644 --- a/go/vcs/vcs_test.go +++ b/go/vcs/vcs_test.go @@ -116,6 +116,20 @@ var parseMetaGoImportsTests = []struct { {"baz/quux", "git", "http://github.com/rsc/baz/quux"}, }, }, + { + ` + `, + []metaImport{ + {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, + }, + }, + { + ` + `, + []metaImport{ + {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, + }, + }, { ` @@ -127,6 +141,21 @@ var parseMetaGoImportsTests = []struct { `, []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, }, + { + ``, + []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, + }, + { + // XML doesn't like
. + `Page Not Found
DRAFT
`, + []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}}, + }, + { + ` + + `, + []metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}}, + }, } func TestParseMetaGoImports(t *testing.T) {