Skip to content

Commit

Permalink
cmd/cue: fix cue help cmd mycmd ./mypkg
Browse files Browse the repository at this point in the history
Jonathan Matthews correctly pointed out that a tutorial used the command
successfully up to and including CUE v0.8.0, but it broke from v0.9.0.
My refactor at https://cuelang.org/cl/1194247 was overall good,
but it did break this edge case, which in my defense had no tests.

Add tests, and fix the code. The issue was that we left args as
["cmd", "mycmd", "./mypkg"], where the valid command is only
["cmd", "mycmd"], so we treated this scenario as an unknown subcommand.

Fixes #3462.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I2208fa8411f1122ede863b8dc6a5d81805562f30
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201709
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Sep 24, 2024
1 parent 842fa25 commit 222b689
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ func newHelpCmd(c *Command) *cobra.Command {
// ["cmd", "mycmd"]
// ["cmd", "mycmd", "./mypkg"]
//
// We want to skip the first two arguments in pkgArgs.
pkgArgs := args[1:]
if len(pkgArgs) > 0 {
pkgArgs = pkgArgs[1:]
// In the third case, we want to load ["./mypkg"]
// and we want to look up the help for ["cmd", "mycmd"].
var pkgArgs []string
if len(args) > 2 {
pkgArgs = args[2:]
args = args[:2]
}

tools, err := buildTools(c, pkgArgs)
Expand Down
8 changes: 8 additions & 0 deletions cmd/cue/cmd/testdata/script/help_cmd.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ cmp stdout cue-help-cmd.stdout
exec cue help cmd hello
cmp stdout cue-help-cmd-hello.stdout

exec cue help cmd hello .
cmp stdout cue-help-cmd-hello.stdout

mkdir subdir
cp task_tool.cue subdir
exec cue help cmd hello ./subdir
cmp stdout cue-help-cmd-hello.stdout

! exec cue help cmd missing
stderr -count=1 'Unknown cmd command: missing'
stderr -count=1 '^Available Commands:$'
Expand Down

0 comments on commit 222b689

Please sign in to comment.