Skip to content

Commit 9583e29

Browse files
committed
pkg: sort input packages in a consistent order when generating
This resolves a minor quirk where adding pkg/encoding/toml would initially put it at the very end of the register.go import list. It could be fixed by a second run of `go generate`, but it's also easy to fix the generator so that it doesn't have this quirk. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I2798ed976918587c6c276f438f9dec6a87c1a9e3 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201473 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 7ec0e5b commit 9583e29

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/gen.go

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ package main
3030

3131
import (
3232
"bytes"
33+
"cmp"
3334
_ "embed"
3435
"flag"
3536
"fmt"
@@ -41,6 +42,7 @@ import (
4142
"math/big"
4243
"os"
4344
"path/filepath"
45+
"slices"
4446
"sort"
4547
"strings"
4648
"text/template"
@@ -100,6 +102,13 @@ func main() {
100102
if packages.PrintErrors(pkgs) > 0 {
101103
os.Exit(1)
102104
}
105+
// Sort the Go packages by import path; otherwise adding a new builtin package
106+
// puts it at the very end of the list the first time it is getting added to register.go,
107+
// as it's not imported by the root package yet. Sorting ensures consistent output.
108+
slices.SortFunc(pkgs, func(a, b *packages.Package) int {
109+
return cmp.Compare(a.PkgPath, b.PkgPath)
110+
})
111+
103112
regBuf := new(bytes.Buffer)
104113
fmt.Fprintf(regBuf, "// Code generated by cuelang.org/go/pkg/gen. DO NOT EDIT.\n\n")
105114
fmt.Fprintf(regBuf, "package pkg\n\n")

0 commit comments

Comments
 (0)