Skip to content

Commit 68395a6

Browse files
author
Jay Conrod
committed
cmd/go: forbid module pattern 'all' when outside a module
Also, in cmd/doc, avoid calling 'go list -m all' when in module mode outside a module since it's now an error. Fixes #32027 Change-Id: I7224c7fdf7e950bce6c058ab2a5837c27ba3b899 Reviewed-on: https://go-review.googlesource.com/c/go/+/200297 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 46b7557 commit 68395a6

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

src/cmd/doc/dirs.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,15 @@ func findCodeRoots() []Dir {
162162
// Check for use of modules by 'go env GOMOD',
163163
// which reports a go.mod file path if modules are enabled.
164164
stdout, _ := exec.Command("go", "env", "GOMOD").Output()
165-
usingModules = len(bytes.TrimSpace(stdout)) > 0
165+
gomod := string(bytes.TrimSpace(stdout))
166+
usingModules = len(gomod) > 0
167+
if gomod == os.DevNull {
168+
// Modules are enabled, but the working directory is outside any module.
169+
// We can still access std, cmd, and packages specified as source files
170+
// on the command line, but there are no module roots.
171+
// Avoid 'go list -m all' below, since it will not work.
172+
return list
173+
}
166174
}
167175

168176
if !usingModules {

src/cmd/go/internal/modget/get.go

+4
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ func runGet(cmd *base.Command, args []string) {
358358
// upgrade golang.org/x/tools.
359359

360360
case path == "all":
361+
// If there is no main module, "all" is not meaningful.
362+
if !modload.HasModRoot() {
363+
base.Errorf(`go get %s: cannot match "all": working directory is not part of a module`, arg)
364+
}
361365
// Don't query modules until we load packages. We'll automatically
362366
// look up any missing modules.
363367

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

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func listModules(args []string, listVersions bool) []*modinfo.ModulePublic {
5555
if search.IsRelativePath(arg) {
5656
base.Fatalf("go: cannot use relative path %s to specify module", arg)
5757
}
58+
if !HasModRoot() && arg == "all" {
59+
base.Fatalf(`go: cannot match "all": working directory is not part of a module`)
60+
}
5861
if i := strings.Index(arg, "@"); i >= 0 {
5962
path := arg[:i]
6063
vers := arg[i+1:]

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ stderr 'cannot find main module'
2525
go list all
2626
! stdout .
2727
stderr 'warning: "all" matched no packages'
28-
go list -m all
29-
stderr 'warning: pattern "all" matched no module dependencies'
3028

3129
# 'go list' on standard-library packages should work, since they do not depend
3230
# on the contents of any module.
@@ -49,10 +47,13 @@ stdout 'example.com/version v1.1.0'
4947
go list -m -versions example.com/version
5048
stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'
5149

52-
# 'go list -m <mods> all' does not include the dependencies of <mods> in the computation of 'all'.
53-
go list -m example.com/[email protected] all
54-
stdout 'example.com/printversion v1.0.0'
55-
stderr 'warning: pattern "all" matched no module dependencies'
50+
# 'go list -m all' should fail. "all" is not meaningful outside of a module.
51+
! go list -m all
52+
stderr 'go: cannot match "all": working directory is not part of a module'
53+
54+
# 'go list -m <mods> all' should also fail.
55+
! go list -m example.com/[email protected] all
56+
stderr 'go: cannot match "all": working directory is not part of a module'
5657
! stdout 'example.com/version'
5758

5859

@@ -87,6 +88,10 @@ go mod download example.com/[email protected]
8788
exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
8889
! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip
8990

91+
# 'go mod download all' should fail. "all" is not meaningful outside of a module.
92+
! go mod download all
93+
stderr 'go: cannot match "all": working directory is not part of a module'
94+
9095
# 'go mod vendor' should fail: it starts by clearing the existing vendor
9196
# directory, and we don't know where that is.
9297
! go mod vendor
@@ -109,9 +114,8 @@ stderr 'cannot find main module'
109114

110115
# 'go get -u all' upgrades the transitive import graph of the main module,
111116
# which is empty.
112-
go get -u all
113-
! stdout .
114-
stderr 'warning: "all" matched no packages'
117+
! go get -u all
118+
stderr 'go get all: cannot match "all": working directory is not part of a module'
115119

116120
# 'go get' should check the proposed module graph for consistency,
117121
# even though we won't write it anywhere.

0 commit comments

Comments
 (0)