cmd/docker: print command error before running plugin hooks#6976
cmd/docker: print command error before running plugin hooks#6976olabie2 wants to merge 2 commits into
Conversation
Plugin hook output (such as Gordon's "What's next:" hint) was rendered before the command's own error message because hooks were invoked inside runDocker while the error was only printed in main() after runDocker returned. Print the error to stderr before invoking the hooks, and replace the error with a status-only StatusError so main() does not print the same message a second time. The original error message is captured up-front and still passed to the plugin hooks. Closes docker#6973 Signed-off-by: Mohammed Olabie <olabiedev@gmail.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
vvoland
left a comment
There was a problem hiding this comment.
Could you please add some tests for this?
I'd need to take a deeper look into it.
|
Moved to 29.5.1 for now |
|
Sure I will add some later |
The inline error-printing block in runDocker is extracted into a printCommandError helper so the new behavior can be unit-tested directly without spinning up runDocker. TestPrintCommandError covers each branch of the helper: nil error, generic error (printed and replaced with StatusError), StatusError with/without message, context.Canceled (raw and wrapped), and errCtxSignalTerminated. Signed-off-by: Mohammed Olabie <olabiedev@gmail.com>
|
I added TestPrintCommandError covering every branch of the new behavior nil, generic error, StatusError with and without message, context.Canceled (raw + wrapped via %w), and errCtxSignalTerminated. To make this directly unit testable, I moved the inline logic from runDocker into a dedicated printCommandError helper, keeping it consistent with the existing getExitCode and cmdErrorMessage helpers. I also verified locally with go test ./cmd/docker/..., golangci-lint run ./cmd/docker/..., and end to end against the original repro from #6973: error now prints before the What's next: hint, exit code 125 preserved. The same fix also works for unrelated failures (e.g. --bogus-flag), and the cancellation path (single Ctrl+C on a long-running docker run) still produces no unexpected output. |
Plugin hook output (such as Gordon's "What's next:" hint) was rendered before the command's own error message because hooks were invoked inside
runDockerwhile the error was only printed inmain()afterrunDockerreturned.Print the error to stderr before invoking the hooks, and replace the error with a status-only
cli.StatusErrorsomain()does not print the same message a second time. The original error message is captured up-front and still passed to the plugin hooks.Closes #6973
- What I did
Fixed the rendering order in
cmd/docker/docker.goso that plugin hook output (for example, theaiplugin's "What's next: Debug this container error with Gordon …" hint) is printed after the command's own error output instead of before it.- How I did it
In
runDocker, the flow used to be:cmd.ExecuteContext(ctx)returned anerr."\nWhat's next:\n …"todockerCli.Err().runDockerreturnederrtomain(), which then wrote the error message toos.Stderr.So the actual stderr order was: hook output → error message.
The fix:
cmdErrorMessage(err)into a localerrMessagebefore mutatingerr.errtodockerCli.Err()immediately aftercmd.ExecuteContext, gated on the same conditionsmain()uses (!errdefs.IsCanceled, noterrCtxSignalTerminated, non-emptyerr.Error()), so behaviour for cancellation, signal termination, and exit-code-only errors is unchanged.errwithcli.StatusError{StatusCode: getExitCode(err)}(preserving the exit code) somain()does not print the same message a second time.errMessagetoRunCLICommandHooks, so plugin hooks still receive the real error text rather than a synthetic"exited with code N"derived from the replaced error.- How to verify it
With hooks enabled (
features.hooks: "true"in~/.docker/config.jsonorDOCKER_CLI_HOOKS=true) and a plugin registering anerror-hooksentry forrun(Docker Desktop provides this via theaiplugin):Before this change:
After this change:
Exit code is preserved (125 in this example). All existing tests in
./cmd/docker/...and./cli-plugins/...still pass.- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)
🐳