]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fail 'go work' subcommands with a more helpful error if no go.work file exists
authorBryan C. Mills <bcmills@google.com>
Tue, 1 Feb 2022 21:59:41 +0000 (16:59 -0500)
committerBryan Mills <bcmills@google.com>
Thu, 3 Feb 2022 05:54:02 +0000 (05:54 +0000)
Otherwise, the failure mode for these subcommands refers to an empty file path:
go: open : no such file or directory

Fixes #50964

Change-Id: I8776431a294d2b2246d7d147b6059054f31bc255
Reviewed-on: https://go-review.googlesource.com/c/go/+/382246
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/workcmd/edit.go
src/cmd/go/internal/workcmd/sync.go
src/cmd/go/internal/workcmd/use.go
src/cmd/go/testdata/script/work_nowork.txt [new file with mode: 0644]

index 879ddc3b1deb2f84fbdb5f423772696e93a1c1c6..e7b1b1327163a315e2385a991e317b78ab60d900 100644 (file)
@@ -115,17 +115,6 @@ func init() {
 }
 
 func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
-       anyFlags :=
-               *editGo != "" ||
-                       *editJSON ||
-                       *editPrint ||
-                       *editFmt ||
-                       len(workedits) > 0
-
-       if !anyFlags {
-               base.Fatalf("go: no flags specified (see 'go help work edit').")
-       }
-
        if *editJSON && *editPrint {
                base.Fatalf("go: cannot use both -json and -print")
        }
@@ -147,6 +136,21 @@ func runEditwork(ctx context.Context, cmd *base.Command, args []string) {
                }
        }
 
+       if gowork == "" {
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+       }
+
+       anyFlags :=
+               *editGo != "" ||
+                       *editJSON ||
+                       *editPrint ||
+                       *editFmt ||
+                       len(workedits) > 0
+
+       if !anyFlags {
+               base.Fatalf("go: no flags specified (see 'go help work edit').")
+       }
+
        workFile, err := modload.ReadWorkFile(gowork)
        if err != nil {
                base.Fatalf("go: errors parsing %s:\n%s", base.ShortPath(gowork), err)
index 1cca817517c2852ee7f37651a3abcefc94795035..948fc5d370d6daf6eabd764c24a86cb6332947d9 100644 (file)
@@ -43,9 +43,11 @@ func init() {
 }
 
 func runSync(ctx context.Context, cmd *base.Command, args []string) {
-       modload.InitWorkfile()
-
        modload.ForceUseModules = true
+       modload.InitWorkfile()
+       if modload.WorkFilePath() == "" {
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+       }
 
        workGraph := modload.LoadModGraph(ctx, "")
        _ = workGraph
index a5ba6c7133cefcd88558d08f90f3460301e5b2eb..d3bc1b7d55ea00e42c99b3228a8e4f2eb3b118c2 100644 (file)
@@ -49,6 +49,9 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
        modload.InitWorkfile()
        gowork = modload.WorkFilePath()
 
+       if gowork == "" {
+               base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using -workfile flag)")
+       }
        workFile, err := modload.ReadWorkFile(gowork)
        if err != nil {
                base.Fatalf("go: %v", err)
diff --git a/src/cmd/go/testdata/script/work_nowork.txt b/src/cmd/go/testdata/script/work_nowork.txt
new file mode 100644 (file)
index 0000000..b0320cb
--- /dev/null
@@ -0,0 +1,20 @@
+! go work use
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+
+! go work use .
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+
+! go work edit
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+
+! go work edit -go=1.18
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+
+! go work sync
+stderr '^go: no go\.work file found\n\t\(run ''go work init'' first or specify path using -workfile flag\)$'
+
+-- go.mod --
+module example
+go 1.18
+-- README.txt --
+There is no go.work file here.