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

Asking for the StringSlice value multiple times mutates it in some cases #1169

Closed
tych0 opened this issue Aug 5, 2020 · 3 comments
Closed
Labels
area/v1 relates to / is being considered for v1 help wanted please help if you can! kind/bug describes or fixes a bug

Comments

@tych0
Copy link

tych0 commented Aug 5, 2020

my urfave/cli version is

v1.22.4

package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli"
)

func doThing(ctx *cli.Context) error {
	fmt.Println("slice-arg:", ctx.StringSlice("slice-arg"))
	fmt.Println("slice-arg:", ctx.StringSlice("slice-arg"))
	fmt.Println("slice-arg:", ctx.StringSlice("slice-arg"))
	return nil
}

func main() {
	app := cli.NewApp()

	app.Commands = []cli.Command{
		cli.Command{
			Name:   "thing",
			Action: doThing,
			Flags: []cli.Flag{
				&cli.StringSliceFlag{
					Name: "slice-arg",
					Value: &cli.StringSlice{"a"},
				},
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Fprintf(os.Stderr, "error: %+v\n", err)
		os.Exit(1)
	}
}
$ GO111MODULE=off go run tester.go thing --slice-arg b
slice-arg: [b]
slice-arg: [b b]
slice-arg: [b b]
$ GO111MODULE=off go run tester.go thing --slice-arg b --slice-arg a
slice-arg: [b a]
slice-arg: [b a]
slice-arg: [b a]
$ GO111MODULE=off go run tester.go thing --slice-arg a --slice-arg b 
slice-arg: [a b]
slice-arg: [b b]
slice-arg: [b b b]
@tych0 tych0 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 Aug 5, 2020
tych0 added a commit to tych0/cli that referenced this issue Aug 5, 2020
If the default value is present, we shouldn't mutate it. Appending slices
as the code was before mutates the underlying value:

package main

import (
	"fmt"
)

func main() {
	slice := []string{"a", "b", "c"}

	fmt.Println(append(slice[:1], slice[2:]...))
	fmt.Println(slice)
}
~ go run tester2.go
[a c]
[a c c]

Let's fix this by creating and returning an entirely new slice.

Closes urfave#1169

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
@stale
Copy link

stale bot commented Nov 3, 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 Nov 3, 2020
@stale
Copy link

stale bot commented Dec 3, 2020

Closing this as it has become stale.

@stale stale bot closed this as completed Dec 3, 2020
@meatballhat meatballhat reopened this Apr 22, 2022
@meatballhat meatballhat removed the status/stale stale due to the age of it's last update label Apr 22, 2022
@meatballhat meatballhat changed the title v1 bug: Asking for the StringSlice value multiple times mutates it in some cases Asking for the StringSlice value multiple times mutates it in some cases Apr 23, 2022
@meatballhat meatballhat added this to the Release 1.22.8 milestone Apr 23, 2022
@meatballhat meatballhat added help wanted please help if you can! and removed status/triage maintainers still need to look into this labels May 5, 2022
@dearchap
Copy link
Contributor

Verified that issue does not exist in v1.22.10. Closing.

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
Projects
None yet
Development

No branches or pull requests

3 participants