Skip to content

Run Before hooks before shell completion#2349

Open
ibobgunardi wants to merge 2 commits into
urfave:mainfrom
ibobgunardi:bobi/run-before-for-completion
Open

Run Before hooks before shell completion#2349
ibobgunardi wants to merge 2 commits into
urfave:mainfrom
ibobgunardi:bobi/run-before-for-completion

Conversation

@ibobgunardi
Copy link
Copy Markdown

What type of PR is this?

  • bug

What this PR does / why we need it:

  • runs the resolved command chain's Before hooks before invoking shell completion
  • keeps completion before flag/action validation, so malformed input can still reach custom completion
  • adds a nested custom completion regression test for setup done in Before

Which issue(s) this PR fixes:

Fixes #2348

Testing

  • go test ./... -count=1

Release Notes

Shell completion now runs command `Before` hooks before custom completion callbacks.

@ibobgunardi ibobgunardi requested a review from a team as a code owner June 5, 2026 01:53
Copy link
Copy Markdown
Contributor

@dearchap dearchap left a comment

Choose a reason for hiding this comment

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

@ibobgunardi Excellent work. Very nicely done. I am just wondering if having Before execute for shell completion needs to be behind a flag. I dont know what users want. Having Before triggered by default or not. @urfave/cli please chime in.

@abitrolly
Copy link
Copy Markdown
Contributor

@dearchap I need a diagram with expanded explanation of steps like Before to remind myself what the workflow is. From the issue description it looks like there are two pathways - run as normal and run as completion, and while this feature fixes the last one, I am not sure what are typical use cases of Before in both pathways to evaluate the impact.

@ibobgunardi
Copy link
Copy Markdown
Author

Here is the split I had in mind:

normal run:
  parse args -> resolve command chain -> run Before hooks -> validate/run action

completion run:
  parse enough to resolve command/flag -> run Before hooks -> call completion callback -> exit

The practical case is shared setup that both the action and completion need: loading config, initializing command-scoped state, or preparing a client/cache that the completion callback reads from. The completion path still exits before action execution and required-flag validation; this change just gives completion the same command setup step that a normal run gets.

The regression test uses a nested command where Before fills state that the completion callback reads, because that is the smallest way to show the missing setup without pulling in external state.

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.

Custom shell completion doesn't work, root.Before() not being called

3 participants