cmd/docker: print command error before running plugin hooks#6976
Open
olabie2 wants to merge 1 commit into
Open
cmd/docker: print command error before running plugin hooks#6976olabie2 wants to merge 1 commit into
olabie2 wants to merge 1 commit 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
🐳