From 280c735b07af9ea313d73049b0031f466e8d1000 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 4 Mar 2021 23:12:22 -0500 Subject: [PATCH] cmd/go: require a module root in 'go list -m' with an unversioned path Fixes #44803 Change-Id: Ie6ee2e3bca1809c91ecedec75d2c6620da914b29 Reviewed-on: https://go-review.googlesource.com/c/go/+/298752 Trust: Bryan C. Mills Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot Reviewed-by: Jay Conrod --- src/cmd/go/internal/modload/list.go | 9 +++++++-- src/cmd/go/testdata/script/mod_outside.txt | 5 +++++ src/cmd/go/testdata/script/mod_retract_versions.txt | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/modload/list.go b/src/cmd/go/internal/modload/list.go index c7ef8c9fb7a..44803e960ba 100644 --- a/src/cmd/go/internal/modload/list.go +++ b/src/cmd/go/internal/modload/list.go @@ -72,8 +72,13 @@ func listModules(ctx context.Context, args []string, listVersions, listRetracted if search.IsRelativePath(arg) { base.Fatalf("go: cannot use relative path %s to specify module", arg) } - if !HasModRoot() && (arg == "all" || strings.Contains(arg, "...")) { - base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot) + if !HasModRoot() { + if arg == "all" || strings.Contains(arg, "...") { + base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot) + } + if !listVersions && !strings.Contains(arg, "@") { + base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot) + } } if i := strings.Index(arg, "@"); i >= 0 { path := arg[:i] diff --git a/src/cmd/go/testdata/script/mod_outside.txt b/src/cmd/go/testdata/script/mod_outside.txt index 9d4c22c77b8..7c57db9f7cb 100644 --- a/src/cmd/go/testdata/script/mod_outside.txt +++ b/src/cmd/go/testdata/script/mod_outside.txt @@ -61,6 +61,11 @@ stderr 'go: cannot match "all": go.mod file not found in current directory or an stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$' ! stdout 'example.com/version' +# 'go list -m ' should fail if any of the mods lacks an explicit version. +! go list -m example.com/printversion +stderr 'go: cannot match "example.com/printversion" without -versions or an explicit version: go.mod file not found in current directory or any parent directory; see ''go help modules''$' +! stdout 'example.com/version' + # 'go list -m' with wildcards should fail. Wildcards match modules in the # build list, so they aren't meaningful outside a module. ! go list -m ... diff --git a/src/cmd/go/testdata/script/mod_retract_versions.txt b/src/cmd/go/testdata/script/mod_retract_versions.txt index 961a0a1fa3b..012fa15f420 100644 --- a/src/cmd/go/testdata/script/mod_retract_versions.txt +++ b/src/cmd/go/testdata/script/mod_retract_versions.txt @@ -15,3 +15,8 @@ stdout '^example.com/retract/self/pseudo ""$' go list -m -e -f $FMT --versions example.com/retract/self/pseudo@latest stdout '^example.com/retract/self/pseudo: "module example.com/retract/self/pseudo: no matching versions for query \\"latest\\"" "latest"$' + +-- go.mod -- +module test + +go 1.17