Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1 bug: Panic when printing completions #1093

Closed
4 of 7 tasks
VirrageS opened this issue Mar 25, 2020 · 2 comments
Closed
4 of 7 tasks

v1 bug: Panic when printing completions #1093

VirrageS opened this issue Mar 25, 2020 · 2 comments
Labels
area/v1 relates to / is being considered for v1 help wanted please help if you can! kind/bug describes or fixes a bug status/confirmed confirmed to be valid, but work has yet to start

Comments

@VirrageS
Copy link

my urfave/cli version is

v1.22.2, v1.22.3

Checklist

  • Are you running the latest v1 release? The list of releases is here.
  • Did you check the manual for your release? The v1 manual is here
  • Did you perform a search about this problem? Here's the Github guide about searching.

Dependency Management

  • My project is using go modules.
  • My project is using vendoring.
  • My project is automatically downloading the latest version.
  • I am unsure of what my dependency management setup is.

Describe the bug

I'm experiencing panic when trying to print bash completions for partially completed flag cmd subcmd --abc[TAB].

To reproduce

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.Name = "example"
	app.EnableBashCompletion = true
	app.Commands = []cli.Command{{
		Name:   "cmd",
		Action: func(c *cli.Context) error { return nil },
		Flags:  []cli.Flag{cli.StringFlag{Name: "abc1"}, cli.StringFlag{Name: "abc2"}},
		BashComplete: func(c *cli.Context) {
			fmt.Println(c.NArg())
			fmt.Println("--abc1")
			fmt.Println("--abc2")
		},
	}}
	app.Action = func(c *cli.Context) error {
		fmt.Println("Hello friend!")
		return nil
	}

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}
  1. Build code above
  2. Load completions
  3. example cmd --abc[TAB]

Observed behavior

Panic

Expected behavior

No panic. I also would expect that I will get completions for the flag:

$ example cmd --abc[TAB]
--abc1 --abc2

Additional context

cli/command.go

Lines 117 to 125 in c354cec

set, err := c.parseFlags(ctx.Args().Tail())
context := NewContext(ctx.App, set, ctx)
context.Command = c
if checkCommandCompletions(context, c.Name) {
return nil
}
if err != nil {

Code here looks very suspicious. If we have err returned in c.parseFlags then set will be nil and checkCommandCompletions will access ctx which has ctx.flagSet == nil. This means that any calls inside the checkCommandCompletions accessing flagSet will panic (like .NArg()).

There was also similar issue for the v2: #944. It would be nice to have that fix backported to the v1 as well.

Run go version and paste its output here

go version go1.14 darwin/amd64
@VirrageS VirrageS added area/v1 relates to / is being considered for v1 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Mar 25, 2020
@coilysiren
Copy link
Member

It would be nice to have that fix backported to the v1 as well.

@VirrageS PRs are welcome, if you want to take care of this yourself! The v1 code lives here => https://github.com/urfave/cli/tree/v1

@coilysiren coilysiren added help wanted please help if you can! status/confirmed confirmed to be valid, but work has yet to start and removed status/triage maintainers still need to look into this labels Mar 30, 2020
@coilysiren
Copy link
Member

released in 1.22.4! ✅ https://github.com/urfave/cli/releases/tag/v1.22.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v1 relates to / is being considered for v1 help wanted please help if you can! kind/bug describes or fixes a bug status/confirmed confirmed to be valid, but work has yet to start
Projects
None yet
Development

No branches or pull requests

2 participants