From eec1950a8dd6549438092927b4176c5bf0fc5995 Mon Sep 17 00:00:00 2001 From: yxshee <84263676+yxshee@users.noreply.github.com> Date: Sun, 15 Nov 2026 05:02:19 +0530 Subject: [PATCH] fix: keep overridden flag aliases out of descendant commands 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. --- command_parse.go | 2 +- command_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/command_parse.go b/command_parse.go index d1e09735a8..e446be8502 100644 --- a/command_parse.go +++ b/command_parse.go @@ -54,7 +54,7 @@ func (cmd *Command) parseFlags(args Args) (Args, error) { applyPersistentFlag := true for _, name := range flNames { - if cmd.lFlag(name) != nil { + if cmd.lookupAppliedFlag(name) != nil { applyPersistentFlag = false break } diff --git a/command_test.go b/command_test.go index 263ad82cde..55569250c1 100644 --- a/command_test.go +++ b/command_test.go @@ -4322,6 +4322,57 @@ func TestPersistentFlag(t *testing.T) { assert.Equal(t, int64(2), persistentFlagActionCount, "Expected persistent flag action to be called 2 times") } +func TestPersistentFlagOverrideInDescendants(t *testing.T) { + tests := []struct { + name string + args []string + wantError bool + }{ + {name: "alias on overriding command", args: []string{"deploy", "-c", "prod"}, wantError: true}, + {name: "alias on child", args: []string{"deploy", "service", "-c", "prod"}, wantError: true}, + {name: "alias on grandchild", args: []string{"deploy", "service", "restart", "-c", "prod"}, wantError: true}, + {name: "override on child", args: []string{"deploy", "service", "--config", "prod"}}, + {name: "override on grandchild", args: []string{"deploy", "service", "restart", "--config", "prod"}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + rootConfig := &StringFlag{Name: "config", Aliases: []string{"c"}} + deployConfig := &StringFlag{Name: "config"} + actionRan := false + action := func(_ context.Context, cmd *Command) error { + actionRan = true + assert.Equal(t, "prod", cmd.String("config")) + return nil + } + cmd := &Command{ + Name: "app", + Writer: io.Discard, + ErrWriter: io.Discard, + Flags: []Flag{rootConfig}, + Commands: []*Command{{ + Name: "deploy", + Flags: []Flag{deployConfig}, + Action: action, + Commands: []*Command{{ + Name: "service", + Action: action, + Commands: []*Command{{Name: "restart", Action: action}}, + }}, + }}, + } + err := cmd.Run(buildTestContext(t), append([]string{"app"}, test.args...)) + if test.wantError { + require.ErrorContains(t, err, "flag provided but not defined: -c") + assert.False(t, actionRan) + } else { + require.NoError(t, err) + assert.True(t, actionRan) + } + assert.Equal(t, "", rootConfig.Get()) + }) + } +} + func TestPersistentFlagIsSet(t *testing.T) { result := "" resultIsSet := false