From 9583e293b0cfe70a33de33f41233d5c424645c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 19 Sep 2024 08:37:35 +0100 Subject: [PATCH] pkg: sort input packages in a consistent order when generating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I2798ed976918587c6c276f438f9dec6a87c1a9e3 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201473 Unity-Result: CUE porcuepine Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo --- pkg/gen.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/gen.go b/pkg/gen.go index f229e416ec5..66f3fa08c5c 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -30,6 +30,7 @@ package main import ( "bytes" + "cmp" _ "embed" "flag" "fmt" @@ -41,6 +42,7 @@ import ( "math/big" "os" "path/filepath" + "slices" "sort" "strings" "text/template" @@ -100,6 +102,13 @@ func main() { if packages.PrintErrors(pkgs) > 0 { os.Exit(1) } + // Sort the Go packages by import path; otherwise adding a new builtin package + // puts it at the very end of the list the first time it is getting added to register.go, + // as it's not imported by the root package yet. Sorting ensures consistent output. + slices.SortFunc(pkgs, func(a, b *packages.Package) int { + return cmp.Compare(a.PkgPath, b.PkgPath) + }) + regBuf := new(bytes.Buffer) fmt.Fprintf(regBuf, "// Code generated by cuelang.org/go/pkg/gen. DO NOT EDIT.\n\n") fmt.Fprintf(regBuf, "package pkg\n\n")