fix: skip After hook when help is displayed on subcommand#2363
Merged
Conversation
Contributor
|
I don't know. Maybe there should be a separate |
…ubcommand help display
When --help is passed to a subcommand, the parent command's After hook was firing even though the parent's Before hook was never called. This fix tracks help invocation via a context key and skips the After hook when help was the reason for early return. Fixes urfave#2250
meatballhat
approved these changes
Jun 14, 2026
meatballhat
left a comment
Member
There was a problem hiding this comment.
I tend to agree with @abitrolly but given that this is internal I think a more tidy implementation could be in a follow up.
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.
Summary
Fixes #2250 — when
--helpis passed to a subcommand, the parent command'sAfterhook was firing even though the parent'sBeforehook was never called. This violates the expected invariant thatAftershould only be called whenBeforewas also called.Root Cause
In
command_run.go, theAfterhook is registered as a deferred call at line 227, before the subcommand dispatch (line 298) and theBeforechain (line 312). When a subcommand receives--help, the subcommand'srun()returns early at the help check (line 205), never reaching theBeforechain. The parent's deferredAfterthen fires without the correspondingBefore.Fix
Track help invocation via a
helpShownKeyin the context. When--helpis detected (both in the success path and the parse-error-then-help path), set the key in the context before returning. The deferredAfterhook checks this key and returns early if help was the reason for early exit.Tests Added
TestCommand_AfterNotCalledOnSubcommandHelp— verifiesAfteris NOT called whenapp sub --helpis usedTestCommand_AfterStillCalledOnNormalSubcommand— verifiesAfterIS called for normalapp subexecution