[release-branch.go1.18] cmd/go: error out of 'go work use' if no directories are given

Otherwise, the behavior of 'go work use -r' (without arguments)
may be surprising.

For #51448.

Change-Id: I50cf1339591720ec5bd333146b89c9944ce420d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/389855
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
(cherry picked from commit 4f8094386c)
Reviewed-on: https://go-review.googlesource.com/c/go/+/389299
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Bryan C. Mills 2022-03-03 15:13:11 -05:00 committed by Dmitri Shuralyov
parent 839c0f3b3e
commit 40f6480db6
3 changed files with 16 additions and 2 deletions

View File

@ -1535,7 +1535,7 @@
//
// Usage:
//
// go work use [-r] [moddirs]
// go work use [-r] moddirs
//
// Use provides a command-line interface for adding
// directories, optionally recursively, to a go.work file.

View File

@ -20,7 +20,7 @@ import (
)
var cmdUse = &base.Command{
UsageLine: "go work use [-r] [moddirs]",
UsageLine: "go work use [-r] moddirs",
Short: "add modules to workspace file",
Long: `Use provides a command-line interface for adding
directories, optionally recursively, to a go.work file.
@ -101,6 +101,9 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
keepDirs[absDir] = dir
}
if len(args) == 0 {
base.Fatalf("go: 'go work use' requires one or more directory arguments")
}
for _, useDir := range args {
if !*useR {
lookDir(useDir)

View File

@ -0,0 +1,11 @@
# For now, 'go work use' requires arguments.
# (Eventually, we may may it implicitly behave like 'go work use .'.
! go work use
stderr '^go: ''go work use'' requires one or more directory arguments'
! go work use -r
stderr '^go: ''go work use'' requires one or more directory arguments'
-- go.work --
go 1.18