Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Getting Started

Alex Eagle edited this page Feb 15, 2018 · 2 revisions

This page is a one-time guide to get up and running with Bazel in your project.

Installing

Bazel will install its own toolchain for builds, so you don’t need much installed even on a new computer.

Bazel

ibazel (iterative Bazel, aka. watch mode)

Install it with your package manager, eg. npm install --save-dev @bazel/ibazel or yarn add -D @bazel/ibazel

Note: we don’t recommend global install, because it’s so non-hermetic: who knows which version you or your teammate are running?

To use it, just replace bazel with ibazel in any command. This assumes ibazel is on your path: see https://github.com/bazelbuild/bazel-watcher/issues/45 for comments about how to set this up.

graphviz (optional)

This is a useful tool for visualizing the dependency graph.

  • Mac: brew install graphviz
  • Others: TODO

Configuration

Bazel takes options on the command line, but any option can be placed in a unix-style RC file instead.

We recommend adding a tools/bazel.rc file in your workspace root, which will be checked into version control and shared with your team and CI. If you have personal preferences for this project, you can add these to the .bazelrc file in the project root, and add that file to your .gitignore. If you have personal preferences that you'd like to apply globally, put them in the .bazelrc file in your home directory. See bazelrc for the full docs.

In your personal .bazelrc we recommend using these flags:

# Globally cache downloaded artifacts.
build --experimental_repository_cache=/Users/[me]/.bazel_cache/
test --experimental_repository_cache=/Users/[me]/.bazel_cache/
run --experimental_repository_cache=/Users/[me]/.bazel_cache/

And to share with your team, here is a template tools/bazel.rc file to get you started:

###############################
# Directory structure         #
###############################

# Don't create bazel-* symlinks in the WORKSPACE directory.
# These require .gitignore and may scare users.
# Also, it's a workaround for https://github.com/bazelbuild/rules_typescript/issues/12
# which affects the common case of having `tsconfig.json` in the WORKSPACE directory.
#
# Instead, you should run `bazel info bazel-bin` to find out where the outputs went.
build --symlink_prefix=/

# Another good choice is to create a dist/ directory. Then you can use
#
# build --symlink_prefix=dist/
#
# to get folders like dist/bin
# But be aware, this will still create a bazel-out symlink in your project directory.
# You may still need to exclude that, eg. from the editor's search path.

###############################
# Output                      #
###############################

# A more useful default output mode for bazel query
# Prints eg. "ng_module rule //foo:bar" rather than just "//foo:bar"
query --output=label_kind

# Don't print every dependency in :node_modules, for example
query --noimplicit_deps

# By default, failing tests don't print any output, it goes to the log file
test --test_output=errors

# Show which actions are run under workers,
# and print all the actions running in parallel.
# Helps to demonstrate that bazel uses all the cores on the machine.
build --experimental_ui
test --experimental_ui

###############################
# Typescript / Angular / Sass #
###############################
# Make TypeScript and Angular compilation fast, by keeping a few copies of the compiler
# running as daemons, and cache SourceFile AST's to reduce parse time.
build --strategy=TypeScriptCompile=worker --strategy=AngularTemplateCompile=worker

# Enable debugging tests with --config=debug
test:debug --test_arg=--node_options=--inspect-brk --test_output=streamed --test_strategy=exclusive --test_timeout=9999 --nocache_test_results