From 222b6899d0a0b80b4fac9c29e1dfb51680461d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 24 Sep 2024 12:45:05 +0100 Subject: [PATCH] cmd/cue: fix `cue help cmd mycmd ./mypkg` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I2208fa8411f1122ede863b8dc6a5d81805562f30 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201709 Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe --- cmd/cue/cmd/help.go | 10 ++++++---- cmd/cue/cmd/testdata/script/help_cmd.txtar | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/cue/cmd/help.go b/cmd/cue/cmd/help.go index 7156724347f..bf77410d19d 100644 --- a/cmd/cue/cmd/help.go +++ b/cmd/cue/cmd/help.go @@ -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) diff --git a/cmd/cue/cmd/testdata/script/help_cmd.txtar b/cmd/cue/cmd/testdata/script/help_cmd.txtar index 4a4e96ead39..df43f549a48 100644 --- a/cmd/cue/cmd/testdata/script/help_cmd.txtar +++ b/cmd/cue/cmd/testdata/script/help_cmd.txtar @@ -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:$'