Closed bobheadxi closed 4 months ago
@bobheadxi Can you provide an example of what you are doing in the completion func ?
Can you provide an example of what you are doing in the completion func ?
The bug occurs even if you don't do anything in the completion func directly (see description - it seems to happen in urfave/cli before the handler actually executes), but https://github.com/sourcegraph/sourcegraph/pull/58569 has a concrete example
The reason I ask is that I dont see the issue when the completion func is not set. Let me investigate further. I have specified an invalid flag on the command line and then tried completion of a valid flag and it works just fine
$ issue_1822 command subcommand --stss --
--help --stringflag
$ issue_1822 command subcommand --stss --stringflag
Hm, now that I'm taking a closer look it seems I can't reproduce it either:
if err := (&cli.App{
Name: "sg",
EnableBashCompletion: true,
Commands: []*cli.Command{{
Name: "bar",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "stringflag",
Value: "vcs",
},
},
// internal helper used in my panic scenario
BashComplete: completions.CompletePositionalArgs(func(args cli.Args) (options []string) {
return []string{args.First()}
}),
Action: func(ctx *cli.Context) error {
println("action!")
return nil
},
}},
}).RunContext(context.Background(), os.Args); err != nil {
println(err.Error())
os.Exit(1)
}
However, in our full CLI app, the panic continues to point to the same place in the issue description, i.e. https://github.com/urfave/cli/blob/c023d9bc5a3122830c9355a0a8c17137e0c8556f/help.go#L467 , and the stacktrace looks like the one I shared previously. We do some pretty extensive setup so there might be an issue deep in there (though we skip most setup if we detect the bash completion flag), but the stacktrace doesn't seem to indicate that - let me dig into this some more
Cant reproduce
My urfave/cli version is
https://github.com/urfave/cli/releases/tag/v2.25.7
Checklist
Dependency Management
Describe the bug
When providing a custom completions func for a subcommand, and typing an invalid flag, the completion will panic. This seems to be caused here, where
flagSet
may benil
iferr != nil
, butflagSet
is referenced incheckCompletions
:https://github.com/urfave/cli/blob/c023d9bc5a3122830c9355a0a8c17137e0c8556f/command.go#L155-L160
To reproduce
In a subcommand:
Attempt tab completion on:
Observed behavior
Expected behavior
Not a panic :) If anything, would be nice to be able to provide completions on string flag
Run
go version
and paste its output here