Skip to content

Commit 280c735

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent a2f7067 commit 280c735

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ func listModules(ctx context.Context, args []string, listVersions, listRetracted
7272
if search.IsRelativePath(arg) {
7373
base.Fatalf("go: cannot use relative path %s to specify module", arg)
7474
}
75-
if !HasModRoot() && (arg == "all" || strings.Contains(arg, "...")) {
76-
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
75+
if !HasModRoot() {
76+
if arg == "all" || strings.Contains(arg, "...") {
77+
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
78+
}
79+
if !listVersions && !strings.Contains(arg, "@") {
80+
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
81+
}
7782
}
7883
if i := strings.Index(arg, "@"); i >= 0 {
7984
path := arg[:i]

src/cmd/go/testdata/script/mod_outside.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ stderr 'go: cannot match "all": go.mod file not found in current directory or an
6161
stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
6262
! stdout 'example.com/version'
6363

64+
# 'go list -m <mods>' should fail if any of the mods lacks an explicit version.
65+
! go list -m example.com/printversion
66+
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''$'
67+
! stdout 'example.com/version'
68+
6469
# 'go list -m' with wildcards should fail. Wildcards match modules in the
6570
# build list, so they aren't meaningful outside a module.
6671
! go list -m ...

src/cmd/go/testdata/script/mod_retract_versions.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ stdout '^example.com/retract/self/pseudo ""$'
1515

1616
go list -m -e -f $FMT --versions example.com/retract/self/pseudo@latest
1717
stdout '^example.com/retract/self/pseudo: "module example.com/retract/self/pseudo: no matching versions for query \\"latest\\"" "latest"$'
18+
19+
-- go.mod --
20+
module test
21+
22+
go 1.17

0 commit comments

Comments
 (0)