Commit ceca6022 authored by Russ Cox's avatar Russ Cox

cmd/go/internal/get: fix "mod over non-mod" preference for meta tags

If there was a mod and non-mod meta tag for a given prefix,
the meta tag extractor was already dropping the non-mod meta tag.
But we might have mod and non-mod meta tags with different
prefixes, in which case the mod tag should prevail when both match.

Fixes #26200.

Change-Id: I17ab361338e270b9fa03999ad1954f2bbe0f5017
Reviewed-on: https://go-review.googlesource.com/124714Reviewed-by: 's avatarBryan C. Mills <bcmills@google.com>
parent ff81a644
...@@ -98,6 +98,23 @@ var parseMetaGoImportsTests = []struct { ...@@ -98,6 +98,23 @@ var parseMetaGoImportsTests = []struct {
IgnoreMod, IgnoreMod,
[]metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}}, []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
}, },
{
`<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
<meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
`,
IgnoreMod,
[]metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}},
},
{
`<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
<meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
`,
PreferMod,
[]metaImport{
{"myitcv.io/blah2", "mod", "https://raw.githubusercontent.com/myitcv/pubx/master"},
{"myitcv.io", "git", "https://github.com/myitcv/x"},
},
},
} }
func TestParseMetaGoImports(t *testing.T) { func TestParseMetaGoImports(t *testing.T) {
......
...@@ -948,7 +948,13 @@ func matchGoImport(imports []metaImport, importPath string) (metaImport, error) ...@@ -948,7 +948,13 @@ func matchGoImport(imports []metaImport, importPath string) (metaImport, error)
continue continue
} }
if match != -1 { if match >= 0 {
if imports[match].VCS == "mod" && im.VCS != "mod" {
// All the mod entries precede all the non-mod entries.
// We have a mod entry and don't care about the rest,
// matching or not.
break
}
return metaImport{}, fmt.Errorf("multiple meta tags match import path %q", importPath) return metaImport{}, fmt.Errorf("multiple meta tags match import path %q", importPath)
} }
match = i match = i
......
...@@ -401,6 +401,22 @@ func TestMatchGoImport(t *testing.T) { ...@@ -401,6 +401,22 @@ func TestMatchGoImport(t *testing.T) {
path: "different.example.com/user/foo", path: "different.example.com/user/foo",
err: errors.New("meta tags do not match import path"), err: errors.New("meta tags do not match import path"),
}, },
{
imports: []metaImport{
{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
},
path: "myitcv.io/blah2/foo",
mi: metaImport{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
},
{
imports: []metaImport{
{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
},
path: "myitcv.io/other",
mi: metaImport{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
},
} }
for _, test := range tests { for _, test := range tests {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment