pflag icon indicating copy to clipboard operation
pflag copied to clipboard

StringToString usage output is unstable

Open SOF3 opened this issue 2 years ago • 1 comments

Since Go maps have randomized iteration order, FlagUsages() returns inconsistent output:

playground

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"

	"github.com/spf13/pflag"
)

func main() {
	counter := map[string]int{}

	for i := 0; i < 100; i++ {
		fs := pflag.NewFlagSet("t", pflag.ExitOnError)
		fs.StringToString("foo", map[string]string{"a": "b", "c": "d"}, "usage")
		output := fs.FlagUsages()
		counter[output]++
	}

	fmt.Print(counter)
}
map[      --foo stringToString   usage (default [a=b,c=d])
:88       --foo stringToString   usage (default [c=d,a=b])
:12]

In projects with a workflow that writes the command usage to a file (e.g. README), this causes unnecessary diff all the time.

SOF3 avatar Oct 07 '22 06:10 SOF3

It's a general issue concerning reproducibility. If you want to create an OS package including docs, say, man pages, it won't be reproducible since the man pages will differ on each build.

twz123 avatar Nov 22 '23 08:11 twz123