Skip to content

Commit

Permalink
pkg: sort input packages in a consistent order when generating
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Sep 19, 2024
1 parent 7ec0e5b commit 9583e29
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"flag"
"fmt"
Expand All @@ -41,6 +42,7 @@ import (
"math/big"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9583e29

Please sign in to comment.