bazel-contrib / bazel-contrib/rules_go

Changing `build_tags`/`gotags` or race detection invalidates the cache

Open
#3,012 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
760
Avg merge
1d 11h
Merged PRs (30d)
12

Description

### What version of rules_go are you using?

0.26

### What version of gazelle are you using?

`0ac66c98675a24d58f89a614b84dcd920a7e1762`

### What version of Bazel are you using?

4.1.0

### Does this issue reproduce with the latest releases of all the above?

Yes

### What operating system and processor architecture are you using?

macOS Big Sur 11.16.1 x86_64

### Any other potentially useful information about your toolchain?

clang 10.0.0

### What did you do?

Using https://github.com/cockroachdb/cockroach: first `bazel build pkg/cmd/cockroach-short`, then `bazel build pkg/cmd/cockroach-short --@io_bazel_rules_go//go/config:race`, then `bazel build pkg/cmd/cockroach-short` (without the `race` configuration) again. Alternatively, first do a normal build, then w/ `--define gotags=bazel`, then another build without overriding the `gotags`. (`cockroach` is a big repository but presumably this reproduces on any project of any size?)

### What did you expect to see?

It makes sense that the `race` build needs to re-compile everything, since it can't reuse any Go artifacts from the previous build which didn't have the race detector enabled. However, it is very surprising that this apparently invalidates all cached artifacts from the previous build, and that doing another `bazel build` without the race detector enabled requires re-compiling everything and can't reuse any artifacts from two builds ago. The same is true for `gotags`: it makes sense that you need to recompile everything when you change the `gotags` you're building with, but it's surprising that the cache cannot retain artifacts for both sets of `gotags`. Note this is a regression over baseline `go build`, which can have artifacts for both sets of build tags in its cache, so there's no thrashing if you swap between race and non-race or between sets of build tags.

At `cockroach` we've added a couple hacks to reduce thrashing due to our liberal use of build tags: see https://github.com/cockroachdb/cockroach/pull/72838. This approach is not generalizable since `race` is, itself, a build tag.

### What did you see instead?

Thrashed cache when swapping between race- and non-race builds, or between builds with varying `gotags`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the cache thrashing with the three `bazel build pkg/cmd/cockroach-short` commands, first switching `--@io_bazel_rules_go//go/config:race` and then switching `--define gotags=bazel`. Trace the rules_go build configuration and cache-key handling; done means artifacts from both race and non-race or differing `gotags` builds can be reused without recompiling everything.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.