Skip to content

fix: skip After hook when help is displayed on subcommand#2363

Merged
dearchap merged 2 commits into
urfave:mainfrom
dearchap:issue_2250
Jun 14, 2026
Merged

fix: skip After hook when help is displayed on subcommand#2363
dearchap merged 2 commits into
urfave:mainfrom
dearchap:issue_2250

Conversation

@dearchap

@dearchap dearchap commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2250 — 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 violates the expected invariant that After should only be called when Before was also called.

Root Cause

In command_run.go, the After hook is registered as a deferred call at line 227, before the subcommand dispatch (line 298) and the Before chain (line 312). When a subcommand receives --help, the subcommand's run() returns early at the help check (line 205), never reaching the Before chain. The parent's deferred After then fires without the corresponding Before.

Fix

Track help invocation via a helpShownKey in the context. When --help is detected (both in the success path and the parse-error-then-help path), set the key in the context before returning. The deferred After hook checks this key and returns early if help was the reason for early exit.

Tests Added

  • TestCommand_AfterNotCalledOnSubcommandHelp — verifies After is NOT called when app sub --help is used
  • TestCommand_AfterStillCalledOnNormalSubcommand — verifies After IS called for normal app sub execution

@dearchap dearchap requested a review from a team as a code owner June 8, 2026 19:52
@abitrolly

Copy link
Copy Markdown
Contributor

I don't know. Maybe there should be a separate helpMode traversal|walk which is (docu|imple)mented as not triggering any hooks at all?

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 meatballhat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree with @abitrolly but given that this is internal I think a more tidy implementation could be in a follow up.

@dearchap dearchap merged commit 183b3a2 into urfave:main Jun 14, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Imbalance between Before and After calls at printing help

3 participants