]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: report a helpful error when there are no modules in workspace
authorMichael Matloob <matloob@golang.org>
Fri, 19 Nov 2021 21:09:52 +0000 (16:09 -0500)
committerMichael Matloob <matloob@golang.org>
Mon, 22 Nov 2021 16:53:57 +0000 (16:53 +0000)
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 <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/alldocs.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/workcmd/work.go
src/cmd/go/testdata/script/work_build_no_modules.txt [new file with mode: 0644]

index 6805d56e2c7ce301850918f14ac1262a0bdd9107..296f8f8c6aaee505e159ecf6488b4a88e63313a4 100644 (file)
 //
 // Workspace maintenance
 //
-// Go workspace provides access to operations on worskpaces.
+// Go workspace provides access to operations on workspaces.
 //
 // Note that support for workspaces is built into many other commands,
 // not just 'go work'.
 // See 'go help modules' for information about Go's module system of
 // which workspaces are a part.
 //
+// A workspace is specified by a go.work file that specifies a set of
+// module directories with the "use" directive. These modules are used
+// as root modules by the go command for builds and related operations.
+// A workspace that does not specify modules to be used cannot be used
+// to do builds from local code.
+//
+// To determine whether the go command is operating in workspace mode,
+// use the "go env GOWORK" command. This will specify the workspace
+// file being used.
+//
 // Usage:
 //
 //     go work <command> [arguments]
index 8bb3875e37f1aa775c8e1d384e2c9844ac60eb89..30fe446e43e76314184036200768b1368f52e026 100644 (file)
@@ -525,6 +525,9 @@ func die() {
        if cfg.Getenv("GO111MODULE") == "off" {
                base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
        }
+       if inWorkspaceMode() {
+               base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
+       }
        if dir, name := findAltConfig(base.Cwd()); dir != "" {
                rel, err := filepath.Rel(base.Cwd(), dir)
                if err != nil {
index 98d5a01de635ca8ac9785e259203ac231a4d4e04..a79eebe649e9cf881ff266f5d8e12d7a5b770cca 100644 (file)
@@ -12,13 +12,23 @@ import (
 var CmdWork = &base.Command{
        UsageLine: "go work",
        Short:     "workspace maintenance",
-       Long: `Go workspace provides access to operations on worskpaces.
+       Long: `Go workspace provides access to operations on workspaces.
 
 Note that support for workspaces is built into many other commands,
 not just 'go work'.
 
 See 'go help modules' for information about Go's module system of
 which workspaces are a part.
+
+A workspace is specified by a go.work file that specifies a set of
+module directories with the "use" directive. These modules are used
+as root modules by the go command for builds and related operations.
+A workspace that does not specify modules to be used cannot be used
+to do builds from local code.
+
+To determine whether the go command is operating in workspace mode,
+use the "go env GOWORK" command. This will specify the workspace
+file being used.
 `,
 
        Commands: []*base.Command{
diff --git a/src/cmd/go/testdata/script/work_build_no_modules.txt b/src/cmd/go/testdata/script/work_build_no_modules.txt
new file mode 100644 (file)
index 0000000..c9859b4
--- /dev/null
@@ -0,0 +1,13 @@
+! go build .
+stderr 'go: no modules were found in the current workspace; see ''go help work'''
+
+-- go.work --
+go 1.18
+-- go.mod --
+go 1.18
+
+module foo
+-- foo.go --
+package main
+
+func main() {}
\ No newline at end of file