We’re partway through a migration of our build system to Bazel. Bazel is a modern build system with better performance characteristics and correctness guarantees than we currently have with
go build. Today, you can perform almost all day-to-day CRDB dev tasks with Bazel rather than with
make is deprecated and we will remove this option for building Cockroach at some point in the next release cycle.
For the few tasks that you can't/don't know how to do with Bazel, please ask a question or contribute a solution. The Bazel migration is actively in progress and we always accept contributions even for minor/QOL improvements.
NOTE: for specific debugging tips on:
- Build failures, see “How to ensure your code builds with Bazel”
- Test failures, see “How to ensure your tests can run in the Bazel sandbox”
Note that you do need a full installation of XCode to build on macOS. (No, a command-line tools instance does not suffice.) If you’ve already installed a command-line tools instance as part of setting up Homebrew or other tools, switch to the full XCode instance and accept the XCode license agreement with:
dev is a light wrapper around Bazel that is well-suited to specific CRDB development tasks. Properly, when we say
dev, we’re referring to the Go binary whose source lives in
cockroach/pkg/cmd/dev, side-by-side with the Cockroach source. At the top level of the repo there is a wrapper script also called
dev whose only job is to build
pkg/cmd/dev and then invoke it with the passed-in arguments. This means that you can always type
./dev to run the version of
dev at the commit you’ve checked out, which is typically what you want to do.
dev is meant to supplement
bazel rather than replace it.
dev makes certain tasks easier than
bazel, but simple builds and tests are easy enough to run via
Before trying to build anything, run the following:
dev notices any issues with your system that might prevent you from being able to build, it will let you know and tell you how to fix it. Make sure to run
dev doctor before you ask for help just in case it catches something you didn’t know about.
dev will take a while to run the first time you execute it since it needs to build itself. Be patient.
First steps: building
Start by building
cockroach-short as follows:
./dev build short also works as an alias. Bazel will pretty-print build output to the terminal.
You can also build the full
cockroach binary which includes
./dev build (or equivalently,
./dev build cockroach) is a synonym for this.
dev build is a light wrapper for
bazel build that supports aliases for common targets (for example,
./dev build crlfmt instead of the harder-to-remember
bazel build @com_github_cockroachdb_crlfmt//:crlfmt).
dev also copies binaries out of the Bazel output directories for you into your workspace; for example,
bazel build pkg/cmd/cockroach-short puts the binary in
_bazel/bin/pkg/cmd/cockroach-short/cockroach-short_/cockroach-short, but if you
dev build short the binary will be staged at
./dev help build for more information about what you can do with
dev build. Note that you can pass additional arguments directly to
bazel build by adding them after
dev lets you cross-compile with the
--cross option, as in:
--cross takes an optional argument which is the platform to cross-compile to:
dev will copy the built binaries into the
artifacts directory in this case. Note that cross-building requires Docker. Cross-compiling should work on M1 Macs, but this support is experimental, so report issues if you should observe any.
For more debugging tips on building with Bazel, see “How to ensure your code builds with Bazel”.
You can run all tests in a given package (for example,
bazel test pkg/sql/... will instead run all tests under the
pkg/sql directory recursively, including all the tests under
pkg/sql/rowinfra among others.
If the test passes, you’ll see a brief message indicating which tests were run and their status:
If the test doesn’t pass, Bazel will print the location of the test’s log file:
You can examine that file to see the complete logs for the test.
./dev test is provided as a shorthand for
bazel test with some additional conveniences. Run
./dev test with the name of one or more packages to execute all tests from those packages:
Tips and tricks
dev testhas a
--stressflag for running tests under
--racefor running tests with the race detector enabled.
Next to the
test.logfile produced by your test, you can find a
test.xmlfile. This file contains specific information on each test run and its status, as well as timing information.
dev testwill result in more verbose logging as well as more detailed information written to the
test.xml. You can make this the default behavior on your machine by adding
test --test_env=GO_TEST_WRAP_TESTV=1to your
dev testallows you to pass additional arguments directly to Bazel by putting them after
--: for example,
dev test pkg/sql/types -- --verbose_failures --sandbox_debug.
To get test results printed as tests are being run add
-v -- --test_output streamedto the test command. Note that this reduces test parallelism.
For more tips on debugging test failures, see “How to ensure your tests can run in the Bazel sandbox”
This is a (non-exhaustive) 1-to-1 mapping of
dev commands to their
make equivalents. Feel free to add to this
dev script uses Bazel to build
pkg/cmd/dev before running unless another
dev binary with the same increasing integer ID has already been built. Generally
dev will invoke the
dev binary “as of” that commit, which should usually be the correct behavior. However, if the default behavior does not work for some reason, you can find all the built versions of
A (hopefully) fast and error proof
TODO(a UI developer): fill in gaps for UI dev workflow
1. Switch to a new branch
2. If your workflow involves an IDE, generate your protos
./dev gen protobuf
Your IDE relies on generated files for many tasks (e.g. code navigation, IntelliSense, debugging), and will complain unless you have re-generated those files.
If you need to re-generate all generated go files, use the slower
./dev gen go
If the above fails, run the slowest
./dev gento update all of your generated files.
You may recall that with
make, this step was not necessary. If you’re curious why, see this slack thread.
3. Write some code!
If you add new files or imports, run
./dev gen bazelbefore compiling or running a test.
compilepkg: missing strict dependencies:is usually the indicator that
./dev gen bazelneeds to be re-run.
to skip this step, see tip below on
Build the binary:
./dev build short
4. Run a test
On an IDE: your normal workflow should work if your generated files are up to date (See step 2).
From the command line:
./dev test [path/to/pkg] --filter [test_name]
5. Before opening/updating a PR:
./dev lint --short(maybe additionally
dev's linter doesn’t have 100% coverage yet)
Assert your workspace is clean by running
./dev gen bazel. If you modified other generated files, run the appropriate
./dev gen [file_type]command.
General Bazel tips
Bazel has a configuration file called .bazelrc. You can put a global configuration file at
~/.bazelrcor a per-repository file at
.bazelrc.userin the root of your
Tired of running
./dev gen bazel? Set the
ALWAYS_RUN_GAZELLEenv-var to automatically run
./dev gen bazelbefore every
dev buildincantation. Note this does add a tiny delay – noticeable when iterating on small tests through
echo 'export ALWAYS_RUN_GAZELLE=1' >> ~/.zshrc
If you have
bazelwill fail with an error like
ccache: error: Failed to create temporary file for /home/alyshanjahani/.ccache/tmp/message_li.stdout: Read-only file system. To avoid this you should get the
ccachelinks out of your
PATHmanually (i.e. uninstall
ccache), and then you might need to do
bazel clean --expunge.
Alternatively, if you would like to use Bazel with
ccache, you can enable support for writing outside the sandbox by adding the following to your
- For MacOS/Darwin:
- For Linux:
If you’re using a different ccache directory (
ccache --get-config cache_dir) point to that instead.
dev vs. Bazel
You can always use
bazel directly instead of going through
dev but there are some things you might want to keep in mind:
You should still ask
dev doctorif your machine is up-to-snuff before you try to
bazel build. The checks it performs aren’t
dev doctoralso sets up a local cache for you.
devprints out the (relevant) calls to
bazelit makes before it does so. You can therefore run
devonce just to learn how to ask Bazel to perform your build/test and then just directly call into
bazelon subsequent iterations.
When running tests under stress, race, or
devdoes the legwork to invoke with the necessary flags with bazel. This involves running under another binary (
stress), running with certain gotags (
race), or allowing certain paths outside the bazel sandbox to be written to (
testdata). Feel free to see the actual
bazelcommand invoked and tweak as necessary.
If you want to build with the UI, you must include the
--config with_uiargument to
bazel build. (
devtakes care of this for you if you are using it.)
If you want to build a test without running it, you must include the the
--config testargument to
bazel build. (
devtakes care of this for you if you are using it.)