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

Creating two cli.Command with the same Name results in magic #785

Closed
johnwyles opened this issue Dec 13, 2018 · 24 comments
Closed

Creating two cli.Command with the same Name results in magic #785

johnwyles opened this issue Dec 13, 2018 · 24 comments
Assignees
Labels
kind/bug describes or fixes a bug status/claimed someone has claimed this issue
Milestone

Comments

@johnwyles
Copy link

johnwyles commented Dec 13, 2018

If you name a cli.Command the same as another cli.Command only one of them will execute with no error or warning that there is also a duplicate command by the same name and with an exit code of 0. Consider the following:

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.Commands = []cli.Command{
		// This is the only command that WILL execute since it is the first "foo" command found
		cli.Command{
			Action: func(c *cli.Context) { fmt.Println("foo1") }, // Print "foo1"
			Name:   "foo", // Name is "foo"
		},
		// This command will NEVER execute since it is the second "foo" match in the array
		cli.Command{
			Action: func(c *cli.Context) { fmt.Println("foo2") }, // Print "foo2"
			Name:   "foo", // Name is ALSO "foo"
		},
	}
	app.Name = "test"

	if err := app.Run(os.Args); err != nil {
		log.Fatalf("An error occurred: %s", err)
	}
}

Running this code yields the following output:

$ go run test.go foo
foo1
$ echo $?
0

Suggest checking all cli.Commands for any duplicates and exiting with a message and non-zero exit code or running all cli.Commands that match the cli.Command.Name field.

gist link: https://gist.github.com/johnwyles/3129a05391e1f749c661b1efc98abc67

@johnwyles
Copy link
Author

I should also point out this is especially more cryptic to debug and discover (as it was for me 😄) when you have moved your cli.Command functions to separate files as you would probably do in most modest sized projects (see here).

@michaeljs1990
Copy link
Contributor

It seems reasonable that this would just blow up when this happens. It shouldn't be a hard check to add internally.

@johnwyles
Copy link
Author

johnwyles commented Feb 1, 2019

I split my commands (e.g. cli.Command) per-file and certainly when you have several if you're not careful about what you name them you can screw this up and the debugging was atrocious - iirc 2 hours of killed

@johnwyles
Copy link
Author

johnwyles commented Feb 1, 2019

Another thought I just had is that another seemingly expected behaviour would be that both would execute in some order (presumably as code reads). I am unsure if you want to blow up or execute both because then the ordering (however one expect ordering to go?) would determine their serial execution and I am unsure if that is known or unknown to the user. With this hindsight perhaps executing both is the expected behavior. Let me know if I need to clarify that point...

@johnwyles
Copy link
Author

johnwyles commented Feb 8, 2019

@michaeljs1990 what are your thoughts on this? should we disallow the same flag and bomb out or should we allow two actions for the same flag? I can easily put together a PR depending on what we think the most common expected behavior would be. As it is neither of the two happen and it will just quietly not execute arbitrarily one or the other of the duplicate flags.

@coilysiren
Copy link
Member

Hi @johnwyles! We should definitely do this

should we disallow the same flag and bomb out

I would be very happy to review a PR implementing that ✨

@coilysiren coilysiren added help wanted please help if you can! status/confirmed confirmed to be valid, but work has yet to start kind/bug describes or fixes a bug kind/feature describes a code enhancement / feature request and removed kind/bug describes or fixes a bug labels Aug 10, 2019
@coilysiren coilysiren added kind/bug describes or fixes a bug and removed kind/feature describes a code enhancement / feature request labels Sep 10, 2019
@johnwyles
Copy link
Author

@lynncyrin I had not yet dove through to doing an implementation of this but I think it wouldn't be hard if someone would take a good look at: https://github.com/urfave/cli/blob/v2/command.go#L167

and possibly somewhere here:
https://github.com/urfave/cli/blob/v2/app.go#L130-L145

I could be wrong as I am just now glancing back at this after months of working with this.

@ruzaikr
Copy link

ruzaikr commented Dec 7, 2019

I would love to do this. Pls assign it to me @lynncyrin :)

@coilysiren
Copy link
Member

@ruzaikr you got it! 🚀

@coilysiren coilysiren added status/claimed someone has claimed this issue and removed help wanted please help if you can! status/confirmed confirmed to be valid, but work has yet to start labels Dec 7, 2019
@ruzaikr
Copy link

ruzaikr commented Dec 7, 2019

@lynncyrin This is my first Github issue so I would like to quickly check if I'm going in the right direction:

v1...ruzaikr:bugfix/785-no-error-when-same-command-name-used

Do I need to create some tests now?

@coilysiren
Copy link
Member

@ruzaikr creating a draft pull request (described on this page) is slightly easier than providing a link to your branch 🙂

WRT the code itself:

  • yes you should add tests
  • also consider checking if subcommands within the same command have the same name

@ruzaikr
Copy link

ruzaikr commented Dec 8, 2019

Thanks @lynncyrin for your comment. I've given it a shot and created a PR :)

@stale
Copy link

stale bot commented Mar 7, 2020

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale stale bot added the status/stale stale due to the age of it's last update label Mar 7, 2020
@coilysiren
Copy link
Member

there's a WIP PR for this here => #984

@stale
Copy link

stale bot commented Mar 8, 2020

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

@stale stale bot removed the status/stale stale due to the age of it's last update label Mar 8, 2020
@stale
Copy link

stale bot commented Jun 6, 2020

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale stale bot added the status/stale stale due to the age of it's last update label Jun 6, 2020
@stale
Copy link

stale bot commented Jul 6, 2020

Closing this as it has become stale.

@stale stale bot closed this as completed Jul 6, 2020
@meatballhat meatballhat reopened this Apr 21, 2022
@meatballhat meatballhat removed the status/stale stale due to the age of it's last update label Apr 21, 2022
@meatballhat
Copy link
Member

@ruzaikr Hello and sorry for the big delay 😭 Any chance you're interested in picking up this work again?

@ruzaikr
Copy link

ruzaikr commented Apr 21, 2022

@meatballhat Hello, my friend! Actually, it is my bad because I didn’t follow up on the last feedback I received.

Unfortunately, I am in between jobs right now and time is a bit tight. Is 3 weeks from now acceptable?

@meatballhat
Copy link
Member

@ruzaikr Hello and thank you so much for the incredibly fast reply! 3 weeks from now is more than acceptable 🎉 I'm looking forward to seeing your work 🤘🏼

@linrl3
Copy link

linrl3 commented Aug 18, 2023

@meatballhat Hello! This bug is still open. I am using github.com/urfave/cli/v2 v2.25.7. Can I create a PR for this?

@dearchap
Copy link
Contributor

@linrl3 yes please.

@linrl3
Copy link

linrl3 commented Aug 19, 2023

@dearchap Thanks! Please take a look at this PR: #1805

@bartekpacia
Copy link
Member

Since #1805 is merged into v2-maint, I think that this issue can be closed?

cc @dearchap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug describes or fixes a bug status/claimed someone has claimed this issue
Projects
None yet
Development

No branches or pull requests

8 participants