-
Notifications
You must be signed in to change notification settings - Fork 569
Orders source files before compilation to ensure reproducible output #742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package compiler | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/build" | ||
"go/parser" | ||
"go/token" | ||
"go/types" | ||
"testing" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
"golang.org/x/tools/go/loader" | ||
) | ||
|
||
func TestOrder(t *testing.T) { | ||
fileA := ` | ||
package foo | ||
|
||
var Avar = "a" | ||
|
||
type Atype struct{} | ||
|
||
func Afunc() int { | ||
var varA = 1 | ||
var varB = 2 | ||
return varA+varB | ||
} | ||
` | ||
|
||
fileB := ` | ||
package foo | ||
|
||
var Bvar = "b" | ||
|
||
type Btype struct{} | ||
|
||
func Bfunc() int { | ||
var varA = 1 | ||
var varB = 2 | ||
return varA+varB | ||
} | ||
` | ||
files := []source{{"fileA.go", []byte(fileA)}, {"fileB.go", []byte(fileB)}} | ||
|
||
compare(t, "foo", files, false) | ||
compare(t, "foo", files, true) | ||
|
||
} | ||
|
||
func compare(t *testing.T, path string, sourceFiles []source, minify bool) { | ||
outputNormal, err := compile(path, sourceFiles, minify) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// reverse the array | ||
for i, j := 0, len(sourceFiles)-1; i < j; i, j = i+1, j-1 { | ||
sourceFiles[i], sourceFiles[j] = sourceFiles[j], sourceFiles[i] | ||
} | ||
|
||
outputReversed, err := compile(path, sourceFiles, minify) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(outputNormal) != string(outputReversed) { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(string(outputNormal), string(outputReversed), true) | ||
fmt.Println(dmp.DiffPrettyText(diffs)) | ||
t.Fatal("files in different order produces differens JS") | ||
} | ||
} | ||
|
||
type source struct { | ||
name string | ||
contents []byte | ||
} | ||
|
||
func compile(path string, sourceFiles []source, minify bool) ([]byte, error) { | ||
conf := loader.Config{} | ||
conf.Fset = token.NewFileSet() | ||
conf.ParserMode = parser.ParseComments | ||
|
||
context := build.Default // make a copy of build.Default | ||
conf.Build = &context | ||
conf.Build.BuildTags = []string{"js"} | ||
|
||
var astFiles []*ast.File | ||
for _, sourceFile := range sourceFiles { | ||
astFile, err := parser.ParseFile(conf.Fset, sourceFile.name, sourceFile.contents, parser.ParseComments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
astFiles = append(astFiles, astFile) | ||
} | ||
conf.CreateFromFiles(path, astFiles...) | ||
prog, err := conf.Load() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
archiveCache := map[string]*Archive{} | ||
var importContext *ImportContext | ||
importContext = &ImportContext{ | ||
Packages: make(map[string]*types.Package), | ||
Import: func(path string) (*Archive, error) { | ||
|
||
// find in local cache | ||
if a, ok := archiveCache[path]; ok { | ||
return a, nil | ||
} | ||
|
||
pi := prog.Package(path) | ||
importContext.Packages[path] = pi.Pkg | ||
|
||
// compile package | ||
a, err := Compile(path, pi.Files, prog.Fset, importContext, minify) | ||
if err != nil { | ||
return nil, err | ||
} | ||
archiveCache[path] = a | ||
return a, nil | ||
}, | ||
} | ||
|
||
a, err := importContext.Import(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b, err := renderPackage(a) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
} | ||
|
||
func renderPackage(archive *Archive) ([]byte, error) { | ||
|
||
selection := make(map[*Decl]struct{}) | ||
for _, d := range archive.Declarations { | ||
selection[d] = struct{}{} | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
if err := WritePkgCode(archive, selection, false, &SourceMapFilter{Writer: buf}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused: doesn't
fullPath
already includename
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it does, but fullPath is dependant on the goroot of the machine you're building this on... So some machines these files will come before the other files, and some after.
So on Andy's machine the file order would be:
... but on Zoe's machine it would be:
See the issue for an explanation.
With this change the order will be identical on any machine because they'll be like this: