Tim Allclair
Tim Allclair

Reputation: 7817

Why does my go binary change each time I rebuild?

Why does rerunning go build ./ on some projects yield different results each time?

For example:

$ cd go/src/github.com/google/cadvisor
$ go build ./
$ sh1sum cadvisor
cdfc3c38898e2decd5df35b661737f7cc4f514ed  cadvisor
$ go build ./
$ sha1sum cadvisor
a94db96d684225e7a45cc68c5341fe6f57897c23  cadvisor

Full isolated setup:

$ go version
go version go1.6.2 linux/amd64
$ export GOPATH=$(mktemp -d)
$ cd $GOPATH
$ go get github.com/tools/godep
$ go get github.com/google/cadvisor
package github.com/influxdb/influxdb/client: code in directory /tmp/tmp.2MxFdNmdDe/src/github.com/influxdb/influxdb/client expects import "github.com/influxdata/influxdb/client"
$ cd src/github.com/google/cadvisor
$ $GOPATH/bin/godep restore
godep: WARNING: Go version (go1.6) & $GO15VENDOREXPERIMENT= wants to enable the vendor experiment, but disabling because a Godep workspace (Godeps/_workspace) exists
$ go build ./
...

Upvotes: 2

Views: 543

Answers (2)

user6169399
user6169399

Reputation:

1- This is sample code which changes itself every time you build it (also because of embedded __DATE__ and __TIME__ which change on every build):

package main

/*
#include<stdint.h>
#include<string.h>
void getCompileDateTime(uint8_t  dt[12],uint8_t tm[9]){
  strcpy(dt, __DATE__); //Mmm dd yyyy
  strcpy(tm, __TIME__);  //hh:mm:ss
}
*/
import "C"
import (
    "fmt"
    "unsafe"
)

func main() {
    dt := make([]byte, 12)
    tm := make([]byte, 10)
    C.getCompileDateTime((*C.uint8_t)(unsafe.Pointer(&dt[0])), (*C.uint8_t)(unsafe.Pointer(&tm[0])))
    dts, tms := string(dt), string(tm)
    fmt.Println(dts, tms)
}

2-

once cgo is used, the binary will contain $WORK path in the DWARF sections
Current problems:
1. $WORK path leakage in cgo builds.
2. $WORK path leakage in -cover and tests.

ref: https://github.com/golang/go/issues/9206


3- Building a pure Go program multiple times will generate identical binaries.


I hope this helps.

Upvotes: 0

OneOfOne
OneOfOne

Reputation: 99371

It's because you're using CGO, Go creates a tmp directory that gets used to compile the C code and the path gets embedded in the binary.

There was an issue about it but I can't find it now.

Issues 9206 and 13247.

Upvotes: 2

Related Questions