Skip to content

fix: reject overridden flag aliases in descendant commands - #2540

Open
yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/shadowed-ancestor-alias
Open

yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/shadowed-ancestor-alias

Conversation

@yxshee

@yxshee yxshee commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

A persistent flag overridden by an intermediate command becomes available again under its old alias in deeper subcommands. With a root --config/-c and a deploy command defining its own --config:

app deploy -c prod                 # rejects -c
app deploy service -c prod         # runs, but service reads config=""
app deploy service --config prod   # runs with config="prod"

The second command silently sets the root flag to prod, while cmd.String("config") reads the nearer override. That makes the same alias change behavior depending on the subcommand depth.

parseFlags gathers persistent flags from the nearest ancestor outward, but checks collisions only against flags declared on the current command. The check now includes flags already applied from nearer ancestors, so an overridden root flag stays excluded. The old alias is rejected consistently and the overriding flag still works.

Which issue(s) this PR fixes:

None filed; found while exercising nested command flag inheritance. Related completed work: #2421 and #2481.

Testing

On macOS arm64, Go 1.26.5, from upstream d5c06cb04479eeacbbb9378ea26261b6be516320:

  • TestPersistentFlagOverrideInDescendants fails on upstream for the old alias on child/grandchild commands and passes with this change. Stashing only the production change makes those cases fail again. The existing rejection on deploy and the surviving --config are also covered.
  • A standalone program using the public API reproduces the three commands above; with the fix both -c invocations fail before the action runs.
  • make ensure-goimports, make lint, make vet, make test, make test GFLAGS='--tags urfave_cli_no_template', make check-binary-size, make generate, make v3diff, and make diffcheck pass. Both test targets include -race.
  • golangci-lint run (2.15.0): 0 issues.

The Linux/Windows CI matrix and Go stable matrix were not run locally. No exported API changed.

Release Notes

An overridden persistent flag's alias no longer becomes available again in deeper subcommands and changes a different flag from the one the command reads.

A persistent flag is excluded when the current command overrides any
of its names. In a deeper descendant, the collision check only looked
at that descendant's own flags, so the root flag was added again even
when a nearer ancestor had already overridden it. Its old alias then
changed the root value instead of the configuration the action read.

Check the flags already applied to the command, including those from
nearer ancestors, before adding a more distant persistent flag. Cover
the rejected alias and the surviving override across command levels.
@yxshee
yxshee requested a review from a team as a code owner November 14, 2026 23:45

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