Skip to content

Commit

Permalink
Sort imports of durable code for output stability (#133)
Browse files Browse the repository at this point in the history
It avoids sometimes getting unexpected diffs when re-compiling the same
program on a different machine.
  • Loading branch information
pelletier authored Dec 15, 2023
2 parents edd7b06 + cc9c20b commit 9845bb3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/compile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"cmp"
"fmt"
"go/ast"
"go/build/constraint"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9845bb3

Please sign in to comment.