Skip to content

Commit cd0bf38

Browse files
committed
cmd/go: report a helpful error when there are no modules in workspace
The current error message that no go.mod files were found is not helpful, especially when a go.mod file exists in the current directory. Fixes #49594 Change-Id: I750475ce8654eeb3e0a2857d5a2de1a9c6ede415 Reviewed-on: https://go-review.googlesource.com/c/go/+/365319 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 6275b54 commit cd0bf38

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/cmd/go/alldocs.go

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/modload/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ func die() {
525525
if cfg.Getenv("GO111MODULE") == "off" {
526526
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
527527
}
528+
if inWorkspaceMode() {
529+
base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
530+
}
528531
if dir, name := findAltConfig(base.Cwd()); dir != "" {
529532
rel, err := filepath.Rel(base.Cwd(), dir)
530533
if err != nil {

src/cmd/go/internal/workcmd/work.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ import (
1212
var CmdWork = &base.Command{
1313
UsageLine: "go work",
1414
Short: "workspace maintenance",
15-
Long: `Go workspace provides access to operations on worskpaces.
15+
Long: `Go workspace provides access to operations on workspaces.
1616
1717
Note that support for workspaces is built into many other commands,
1818
not just 'go work'.
1919
2020
See 'go help modules' for information about Go's module system of
2121
which workspaces are a part.
22+
23+
A workspace is specified by a go.work file that specifies a set of
24+
module directories with the "use" directive. These modules are used
25+
as root modules by the go command for builds and related operations.
26+
A workspace that does not specify modules to be used cannot be used
27+
to do builds from local code.
28+
29+
To determine whether the go command is operating in workspace mode,
30+
use the "go env GOWORK" command. This will specify the workspace
31+
file being used.
2232
`,
2333

2434
Commands: []*base.Command{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! go build .
2+
stderr 'go: no modules were found in the current workspace; see ''go help work'''
3+
4+
-- go.work --
5+
go 1.18
6+
-- go.mod --
7+
go 1.18
8+
9+
module foo
10+
-- foo.go --
11+
package main
12+
13+
func main() {}

0 commit comments

Comments
 (0)