From e08aa72569084897a277fd21223f0dcea5f4b571 Mon Sep 17 00:00:00 2001 From: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> Date: Mon, 19 Oct 2026 19:35:05 +0530 Subject: [PATCH] cmd/docker: fail on unknown command with --version Running "docker nonexistent --version" printed the CLI version and exited 0, so it couldn't be used to check whether a command or plugin is available. Without "--version" the same invocation fails with an "unknown command" error. When no built-in command or plugin matches, runDocker falls through to executing the root command, and cobra handles the "--version" flag before the root command reports the unknown command. Unset the root command's version in that fallback, so the standard unknown-command error is returned. A global "--version" flag ("docker --version nonexistent") is parsed before the command name and still prints the version, as do "docker --version" and "docker nonexistent --help". Signed-off-by: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> --- cmd/docker/docker.go | 9 +++++++++ e2e/cli-plugins/run_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 92dfce8cdd2c..9ce3b53e5bbb 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -580,6 +580,15 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { } // Other missing or invalid plugins fall through to cmd.ExecuteContext(), // which reports the standard unknown-command error. + // + // Cobra handles "--version" before it checks for unknown commands, + // so unset it to make "docker nonexistent --version" fail instead + // of printing the version. A global "--version" flag (as in + // "docker --version nonexistent") was already parsed, and still + // prints the version. + if !cmd.Flags().Changed("version") { + cmd.Version = "" + } } } diff --git a/e2e/cli-plugins/run_test.go b/e2e/cli-plugins/run_test.go index db3628802fc3..9ef9545dc5ea 100644 --- a/e2e/cli-plugins/run_test.go +++ b/e2e/cli-plugins/run_test.go @@ -62,6 +62,22 @@ func TestNonexistingHelp(t *testing.T) { }) } +// TestNonexistingVersion ensures correct behaviour when invoking a +// nonexistent plugin with `--version`. +func TestNonexistingVersion(t *testing.T) { + run, _, cleanup := prepare(t) + defer cleanup() + + for _, flag := range []string{"--version", "-v"} { + res := icmd.RunCmd(run("nonexistent", flag)) + res.Assert(t, icmd.Expected{ + ExitCode: 1, + Out: icmd.None, + }) + golden.Assert(t, res.Stderr(), "docker-nonexistent-err.golden") + } +} + // TestRunBad ensures correct behaviour when running an existent but invalid plugin func TestRunBad(t *testing.T) { run, _, cleanup := prepare(t)