Skip to content

fix: run flag actions for values set in Before - #2542

Open
yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/programmatic-flag-actions
Open

yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/programmatic-flag-actions

Conversation

@yxshee

@yxshee yxshee commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

Calling cmd.Set("region", "west") in Before updates the flag value and makes IsSet("region") true, but its flag action never runs unless the flag was already set by argv or an environment source. The same hook therefore triggers the action for app --region east but skips it for app.

Command.Set updates the flag without adding it to the command's setFlags map. Record successful sets there so the existing flag-action phase sees them. Failed sets return their existing error, and setting a flag before command setup still works with an initially nil map.

The regression covers an unset root flag, argv and environment controls, and an inherited flag set through its alias in a child command's Before hook.

Which issue(s) this PR fixes:

No existing issue found. Related completed work: #2221 made environment-sourced flags participate in the same action phase.

Special notes for your reviewer:

This keeps the existing lifecycle order. It does not invoke actions immediately inside Set or run actions for untouched defaults. Persistent flag actions retain their existing per-command behavior: if the flag is set in both a parent and a child, its action runs in both contexts. A parent argv value followed by a child Before call to Set now behaves like setting the flag via argv in both contexts.

Testing

On macOS arm64 with Go 1.26.5:

  • The regression fails on upstream d5c06cb for unset and inherited flags; argv and environment controls pass. All four pass with the fix. Stashing only the production change reproduces the two failures.
  • A standalone public-API program prints only command action: west (set=true) on upstream. With the fix it first prints flag action: west.
  • make ensure-goimports lint vet test
  • make test GFLAGS='--tags urfave_cli_no_template'
  • make check-binary-size generate v3diff diffcheck
  • golangci-lint run: 0 issues.

Both local test targets include the race detector. Native Linux/Windows and the other CI Go version have not been run locally.

Release Notes

Flags set with Command.Set in a Before hook now participate in flag actions even when no command-line argument or environment source previously set them.

@yxshee
yxshee requested a review from a team as a code owner November 15, 2026 00:44

This branch has not been deployed

No deployments
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.

1 participant