diff --git a/compiler/compile.go b/compiler/compile.go index 9b3d927..a735dc2 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -1,6 +1,7 @@ package compiler import ( + "cmp" "fmt" "go/ast" "go/build/constraint" @@ -402,6 +403,12 @@ func addImports(p *packages.Package, gen *ast.File) *ast.File { }) } + // Imports don't require to be sorted but it helps with output + // stability. The format pass does not take care of this. + slices.SortFunc(importspecs, func(a, b ast.Spec) int { + return cmp.Compare(a.(*ast.ImportSpec).Name.Name, b.(*ast.ImportSpec).Name.Name) + }) + gen.Decls = append([]ast.Decl{&ast.GenDecl{ Tok: token.IMPORT, Specs: importspecs,