mirror of
https://github.com/golang/go
synced 2024-09-29 04:14:27 -06:00
cmd/go: fail 'go work' subcommands with a more helpful error if no go.work file exists
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>
This commit is contained in:
parent
54b2a75406
commit
d0a0606841
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
20
src/cmd/go/testdata/script/work_nowork.txt
vendored
Normal file
20
src/cmd/go/testdata/script/work_nowork.txt
vendored
Normal file
@ -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.
|
Loading…
Reference in New Issue
Block a user