paketo-buildpacks/go-build

Changing build-time value of GOFLAGS env var does not cause app to be rebuilt

fg-j opened this issue · 2 comments

fg-j commented

What happened?

I wanted to rebuild my go app via pack build with the -race flag so I specified it via the build-time env var GOFLAGS. The app executable was not rebuilt. Instead, the cached executable layer was reused, and I didn't see any race condition warning when I ran the app.

Does the buildpack actually support using the GOFLAGS env var at app build-time? If it does, adding the "-race" flag should trigger a rebuild.

Please provide some details about the task you are trying to accomplish and
what went wrong.

  • What were you attempting to do?
pack build build-race -b gcr.io/paketo-buildpacks/go:0.2.5 --builder paketobuildpacks/builder:0.1.17-base --clear-cache

and then, later (without changing app source at all)

 pack build build-race -b gcr.io/paketo-buildpacks/go:0.2.5 --builder paketobuildpacks/builder:0.1.17-base --env GOFLAGS="-race"
  • What did you expect to happen?

I expected the executable to be rebuilt by the buildpack, with the data race detection feature enabled.

  • What was the actual behavior? Please provide log output, if possible.
pack build build-race -b gcr.io/paketo-buildpacks/go:0.2.5 --builder paketobuildpacks/builder:0.1.17-base --clear-cache

Succeeds, rebuilds all app layers.
Running the app, does not produce a race detection warning (as expected)

docker run -it --env PORT=8080 --publish 8080:8080 build-race
5
5
5
5
5

Then, rebuilding:

pack build build-race -b gcr.io/paketo-buildpacks/go:0.2.5 --builder paketobuildpacks/builder:0.1.17-base --env GOFLAGS="-race"

Succeeds, logs include lines:

Paketo Go Build Buildpack 0.1.0
  Reusing cached layer /layers/paketo-buildpacks_go-build/targets

And running the app produces:

docker run -it --env PORT=8080 --publish 8080:8080 build-race
5
5
5
5
5

However, if I add the --clear-cache flag to my build:

pack build build-race -b gcr.io/paketo-buildpacks/go:0.2.5 --builder paketobuildpacks/builder:0.1.17-base --env GOFLAGS="-race" --clear-cache

then the resulting app runs with race detection:

docker run -it --env PORT=8080 --publish 8080:8080 build-race
2
==================
WARNING: DATA RACE
Read at 0x00c000018048 by goroutine 7:
  main.main.func1()
      /workspace/main.go:13 +0x3c

Previous write at 0x00c000018048 by main goroutine:
  main.main()
      /workspace/main.go:11 +0x104

Goroutine 7 (running) created at:
  main.main()
      /workspace/main.go:12 +0xdc
==================
2
3
4
5

This indicates that the executable wasn't rebuilt with the desired race detection flag.

Build Configuration

Please provide some details about your build configuration.

  • What platform (pack, kpack, tekton buildpacks plugin, etc.) are you using? Please include a version.
pack version
0.13.1+git-4134cc6.build-1135
  • Can you provide a sample app or relevant configuration (buildpack.yml,
    nginx.conf, etc.)?

Created a basic app containing only the race on loop counter example race condition
And added a buildpack.yml containing:

---
go:
  build:
    flags:
    - -buildmode=default

Checklist

Please confirm the following:

  • I have included log output.
  • The log output includes an error message.
  • I have included steps for reproduction.

I see. So it looks like the env var is being respected but setting a new env var on a rebuild is not causing us to reset and a rebuild the app layer. We will have to take a look into what the best UX for this is but I am in general agreement that a build configuration change should cause a rebuild.

fg-j commented

With the latest release of the Go buildpack, this issue is resolved.

Using a simple app that contains only

main.go

func main() {
	var wg sync.WaitGroup
	wg.Add(5)
	for i := 0; i < 5; i++ {
		go func() {
			fmt.Println(i) // Not the 'i' you are looking for.
			wg.Done()
		}()
	}
	wg.Wait()
}

and

buildpack.yml

---
go:
  build:
    flags:
    - -buildmode=default

Build 1

pack build build-race -b gcr.io/paketo-buildpacks/go:0.3.2 --clear-cache
...
...
...
docker run -it --env PORT=8080 --publish 8080:8080 build-race
5
5
5
5
5

Build 2

pack build build-race -b gcr.io/paketo-buildpacks/go:0.3.2 --env GOFLAGS="-race"
...
...
...
docker run -it --env PORT=8080 --publish 8080:8080 build-race
2
3
==================
WARNING: DATA RACE
Read at 0x00c000018048 by goroutine 7:
  main.main.func1()
      /tmp/gopath146928843/src/workspace/main.go:13 +0x3c

Previous write at 0x00c000018048 by main goroutine:
  main.main()
      /tmp/gopath146928843/src/workspace/main.go:11 +0x104

Goroutine 7 (running) created at:
  main.main()
      /tmp/gopath146928843/src/workspace/main.go:12 +0xdc
==================
3
4
5
Found 1 data race(s)