You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use a wrapper build tool buck that invokes the Go compile and link tools to build a go binary. We observed that go binaries are not reproducible if the ordering of files to the compile tool is not deterministic.
At first glance I don't see any reason to expect the compiler to produce the same results regardless of input file order. It certainly essential for the compiler to produce the same results given the same arguments. But I'm not sure why it's important to keep the results the same if the input files are in a different order.
This is working as intended, at least as far as the compiler is concerned. Here are some relevant sections from the spec:
The declaration order of variables declared in multiple files is determined by the order in which the files are presented to the compiler: Variables declared in the first file are declared before any of the variables declared in the second file, and so on.
A package with no imports is initialized by assigning initial values to all its package-level variables followed by calling all init functions in the order they appear in the source, possibly in multiple files, as presented to the compiler.
To ensure reproducible initialization behavior, build systems are encouraged to present multiple files belonging to the same package in lexical file name order to a compiler.
(https://tip.golang.org/ref/spec#Package_initialization)
In short, the order in which files are presented to the compiler matters. It is the responsibility of the build tool to provide the files in some reproducible order.
I am going to close this since the spec is pretty clear. I believe the go tool sorts the filenames for reproducible behavior (cc: @bcmills for input).
@griesemer@ianlancetaylor thanks for prompt response and clarification. The only reason this example was interesting was due to init. We were under the impression that it would have an effect in ordering based on what you pointed out as well.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
Tried on go1.13.1 as well
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
We use a wrapper build tool
buck
that invokes the Go compile and link tools to build a go binary. We observed that go binaries are not reproducible if the ordering of files to the compile tool is not deterministic.Create 2 files
And run the compile step
Change the order of files:
What did you expect to see?
We expected to see the md5 hash of the .a object file to be the same
What did you see instead?
We observed the hashes are different if the ordering of files are different.
If this is not a compile tool bug, we could may be update the go compile tool doc ?
cc @codyohlsen
The text was updated successfully, but these errors were encountered: