Skip to content

Commit adf5aa6

Browse files
holimanblakehhuynh
authored andcommitted
internal/flags: fix issue with stringslice migration (ethereum#25830)
This fixes a cornercase bug where the flag migration would mess up the value of StringSlice flags.
1 parent d10c8fc commit adf5aa6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

internal/flags/helpers.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,34 @@ func MigrateGlobalFlags(ctx *cli.Context) {
9494
}
9595

9696
func doMigrateFlags(ctx *cli.Context) {
97+
// Figure out if there are any aliases of commands. If there are, we want
98+
// to ignore them when iterating over the flags.
99+
var aliases = make(map[string]bool)
100+
for _, fl := range ctx.Command.Flags {
101+
for _, alias := range fl.Names()[1:] {
102+
aliases[alias] = true
103+
}
104+
}
97105
for _, name := range ctx.FlagNames() {
98106
for _, parent := range ctx.Lineage()[1:] {
99107
if parent.IsSet(name) {
100-
ctx.Set(name, parent.String(name))
108+
// When iterating across the lineage, we will be served both
109+
// the 'canon' and alias formats of all commmands. In most cases,
110+
// it's fine to set it in the ctx multiple times (one for each
111+
// name), however, the Slice-flags are not fine.
112+
// The slice-flags accumulate, so if we set it once as
113+
// "foo" and once as alias "F", then both will be present in the slice.
114+
if _, isAlias := aliases[name]; isAlias {
115+
continue
116+
}
117+
// If it is a string-slice, we need to set it as
118+
// "alfa, beta, gamma" instead of "[alfa beta gamma]", in order
119+
// for the backing StringSlice to parse it properly.
120+
if result := parent.StringSlice(name); len(result) > 0 {
121+
ctx.Set(name, strings.Join(result, ","))
122+
} else {
123+
ctx.Set(name, parent.String(name))
124+
}
101125
break
102126
}
103127
}

0 commit comments

Comments
 (0)