Skip to content

Commit

Permalink
Update .bazelrc (#168)
Browse files Browse the repository at this point in the history
Configure .bazelrc according to [aspect's
guidance](https://docs.aspect.build/guides/bazelrc/).
Import a few vendored files and use those to set up.

Misc.
- noticed `emboss` wasn't represented canonically, so switched to commit
instead of tag to avoid warning.
- specify `allow_empty=True` in a few glob rules

See #172 for related followups on a few things that didn't work in my
current setup.
See #173 for possible modifications to my remote cache

See these for more reference:
https://docs.aspect.build/guides/bazelrc/
https://bazel.build/run/bazelrc

https://github.com/bazel-contrib/bazel-lib/tree/5853e2e441df850dc332910a0808e9dee2ed9e27/.aspect/bazelrc
https://bazel.build/reference/command-line-reference#flag--host_jvm_args
  • Loading branch information
michael-christen authored Dec 2, 2024
1 parent f26d0e9 commit 4c83275
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 28 deletions.
24 changes: 24 additions & 0 deletions .aspect/bazelrc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"Aspect bazelrc presets; see https://docs.aspect.build/guides/bazelrc"

# Use the following command to update the .bazelrc files in this workspace to
# the latest versions maintained by Aspect. More details are in the guide
# linked above.

# bazel run //.aspect/bazelrc:update_aspect_bazelrc_presets

load("@aspect_bazel_lib//lib:bazelrc_presets.bzl", "write_aspect_bazelrc_presets")

package(default_visibility = ["//visibility:private"])

write_aspect_bazelrc_presets(
name = "update_aspect_bazelrc_presets",
presets = [
"bazel7",
"ci",
"convenience",
"correctness",
"debug",
"javascript",
"performance",
],
)
15 changes: 15 additions & 0 deletions .aspect/bazelrc/bazel7.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Speed up all builds by not checking if external repository files have been modified.
# Docs: https://github.com/bazelbuild/bazel/blob/1af61b21df99edc2fc66939cdf14449c2661f873/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java#L244
common --noexperimental_check_external_repository_files

# Don't report when the root module's lower bound for a dependency happens to be less than the resolved version.
# This is expected and should NOT prompt an engineer to update our lower bound to match.
# WARNING: For repository 'aspect_bazel_lib', the root module requires module version [email protected],
# but got [email protected] in the resolved dependency graph.
common --check_direct_dependencies=off

# Directories used by sandboxed non-worker execution may be reused to avoid unnecessary setup costs.
# Save time on Sandbox creation and deletion when many of the same kind of action run during the
# build.
# Docs: https://bazel.build/reference/command-line-reference#flag--reuse_sandbox_directories
build --reuse_sandbox_directories
66 changes: 66 additions & 0 deletions .aspect/bazelrc/ci.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Set this flag to enable re-tries of failed tests on CI.
# When any test target fails, try one or more times. This applies regardless of whether the "flaky"
# tag appears on the target definition.
# This is a tradeoff: legitimately failing tests will take longer to report,
# but we can paper over flaky tests that pass most of the time.
# The alternative is to mark every flaky test with the `flaky = True` attribute, but this requires
# the buildcop to make frequent code edits.
# Not recommended for local builds so that the flakiness is observed during development and thus
# is more likely to get fixed.
# Note that when passing after the first attempt, Bazel will give a special "FLAKY" status.
# Docs: https://bazel.build/docs/user-manual#flaky-test-attempts
test --flaky_test_attempts=2

# Announce all announces command options read from the bazelrc file(s) when starting up at the
# beginning of each Bazel invocation. This is very useful on CI to be able to inspect what Bazel rc
# settings are being applied on each run.
# Docs: https://bazel.build/docs/user-manual#announce-rc
build --announce_rc

# Add a timestamp to each message generated by Bazel specifying the time at which the message was
# displayed.
# Docs: https://bazel.build/docs/user-manual#show-timestamps
build --show_timestamps

# Only show progress every 60 seconds on CI.
# We want to find a compromise between printing often enough to show that the build isn't stuck,
# but not so often that we produce a long log file that requires a lot of scrolling.
# https://bazel.build/reference/command-line-reference#flag--show_progress_rate_limit
build --show_progress_rate_limit=60

# Use cursor controls in screen output.
# Docs: https://bazel.build/docs/user-manual#curses
build --curses=yes

# Use colors to highlight output on the screen. Set to `no` if your CI does not display colors.
# Docs: https://bazel.build/docs/user-manual#color
build --color=yes

# The terminal width in columns. Configure this to override the default value based on what your CI system renders.
# Docs: https://github.com/bazelbuild/bazel/blob/1af61b21df99edc2fc66939cdf14449c2661f873/src/main/java/com/google/devtools/build/lib/runtime/UiOptions.java#L151
build --terminal_columns=143

######################################
# Generic remote cache configuration #
######################################

# Only download remote outputs of top level targets to the local machine.
# Docs: https://bazel.build/reference/command-line-reference#flag--remote_download_toplevel
build --remote_download_toplevel

# The maximum amount of time to wait for remote execution and cache calls.
# https://bazel.build/reference/command-line-reference#flag--remote_timeout
build --remote_timeout=3600

# Upload locally executed action results to the remote cache.
# Docs: https://bazel.build/reference/command-line-reference#flag--remote_upload_local_results
build --remote_upload_local_results

# Fall back to standalone local execution strategy if remote execution fails. If the grpc remote
# cache connection fails, it will fail the build, add this so it falls back to the local cache.
# Docs: https://bazel.build/reference/command-line-reference#flag--remote_local_fallback
build --remote_local_fallback

# Fixes builds hanging on CI that get the TCP connection closed without sending RST packets.
# Docs: https://bazel.build/reference/command-line-reference#flag--grpc_keepalive_time
build --grpc_keepalive_time=30s
28 changes: 28 additions & 0 deletions .aspect/bazelrc/convenience.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Attempt to build & test every target whose prerequisites were successfully built.
# Docs: https://bazel.build/docs/user-manual#keep-going
build --keep_going

# Output test errors to stderr so users don't have to `cat` or open test failure log files when test
# fail. This makes the log noisier in exchange for reducing the time-to-feedback on test failures for
# users.
# Docs: https://bazel.build/docs/user-manual#test-output
test --test_output=errors

# Show the output files created by builds that requested more than one target. This helps users
# locate the build outputs in more cases
# Docs: https://bazel.build/docs/user-manual#show-result
build --show_result=20

# Bazel picks up host-OS-specific config lines from bazelrc files. For example, if the host OS is
# Linux and you run bazel build, Bazel picks up lines starting with build:linux. Supported OS
# identifiers are `linux`, `macos`, `windows`, `freebsd`, and `openbsd`. Enabling this flag is
# equivalent to using `--config=linux` on Linux, `--config=windows` on Windows, etc.
# Docs: https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
common --enable_platform_specific_config

# Output a heap dump if an OOM is thrown during a Bazel invocation
# (including OOMs due to `--experimental_oom_more_eagerly_threshold`).
# The dump will be written to `<output_base>/<invocation_id>.heapdump.hprof`.
# You may need to configure CI to capture this artifact and upload for later use.
# Docs: https://bazel.build/reference/command-line-reference#flag--heap_dump_on_oom
common --heap_dump_on_oom
64 changes: 64 additions & 0 deletions .aspect/bazelrc/correctness.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Do not upload locally executed action results to the remote cache.
# This should be the default for local builds so local builds cannot poison the remote cache.
# It should be flipped to `--remote_upload_local_results` on CI
# by using `--bazelrc=.aspect/bazelrc/ci.bazelrc`.
# Docs: https://bazel.build/reference/command-line-reference#flag--remote_upload_local_results
build --noremote_upload_local_results

# Don't allow network access for build actions in the sandbox.
# Ensures that you don't accidentally make non-hermetic actions/tests which depend on remote
# services.
# Developers should tag targets with `tags=["requires-network"]` to opt-out of the enforcement.
# Docs: https://bazel.build/reference/command-line-reference#flag--sandbox_default_allow_network
build --sandbox_default_allow_network=false

# Warn if a test's timeout is significantly longer than the test's actual execution time.
# Bazel's default for test_timeout is medium (5 min), but most tests should instead be short (1 min).
# While a test's timeout should be set such that it is not flaky, a test that has a highly
# over-generous timeout can hide real problems that crop up unexpectedly.
# For instance, a test that normally executes in a minute or two should not have a timeout of
# ETERNAL or LONG as these are much, much too generous.
# Docs: https://bazel.build/docs/user-manual#test-verbose-timeout-warnings
test --test_verbose_timeout_warnings

# Allow the Bazel server to check directory sources for changes. Ensures that the Bazel server
# notices when a directory changes, if you have a directory listed in the srcs of some target.
# Recommended when using
# [copy_directory](https://github.com/bazel-contrib/bazel-lib/blob/main/docs/copy_directory.md) and
# [rules_js](https://github.com/aspect-build/rules_js) since npm package are source directories
# inputs to copy_directory actions.
# Docs: https://bazel.build/reference/command-line-reference#flag--host_jvm_args
startup --host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1

# Allow exclusive tests to run in the sandbox. Fixes a bug where Bazel doesn't enable sandboxing for
# tests with `tags=["exclusive"]`.
# Docs: https://bazel.build/reference/command-line-reference#flag--incompatible_exclusive_test_sandboxed
test --incompatible_exclusive_test_sandboxed

# Use a static value for `PATH` and does not inherit `LD_LIBRARY_PATH`. Doesn't let environment
# variables like `PATH` sneak into the build, which can cause massive cache misses when they change.
# Use `--action_env=ENV_VARIABLE` if you want to inherit specific environment variables from the
# client, but note that doing so can prevent cross-user caching if a shared cache is used.
# Docs: https://bazel.build/reference/command-line-reference#flag--incompatible_strict_action_env
build --incompatible_strict_action_env

# Do not automatically create `__init__.py` files in the runfiles of Python targets. Fixes the wrong
# default that comes from Google's internal monorepo by using `__init__.py` to delimit a Python
# package. Precisely, when a `py_binary` or `py_test` target has `legacy_create_init` set to `auto (the
# default), it is treated as false if and only if this flag is set. See
# https://github.com/bazelbuild/bazel/issues/10076.
# Docs: https://bazel.build/reference/command-line-reference#flag--incompatible_default_to_explicit_init_py
build --incompatible_default_to_explicit_init_py

# Set default value of `allow_empty` to `False` in `glob()`. This prevents a common mistake when
# attempting to use `glob()` to match files in a subdirectory that is opaque to the current package
# because it contains a BUILD file. See https://github.com/bazelbuild/bazel/issues/8195.
# Docs: https://bazel.build/reference/command-line-reference#flag--incompatible_disallow_empty_glob
common --incompatible_disallow_empty_glob

# Always download coverage files for tests from the remote cache. By default, coverage files are not
# downloaded on test result cache hits when --remote_download_minimal is enabled, making it impossible
# to generate a full coverage report.
# Docs: https://bazel.build/reference/command-line-reference#flag--experimental_fetch_all_coverage_outputs
# detching remote cache results
test --experimental_fetch_all_coverage_outputs
19 changes: 19 additions & 0 deletions .aspect/bazelrc/debug.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
############################################################
# Use `bazel test --config=debug` to enable these settings #
############################################################

# Stream stdout/stderr output from each test in real-time.
# Docs: https://bazel.build/docs/user-manual#test-output
test:debug --test_output=streamed

# Run one test at a time.
# Docs: https://bazel.build/reference/command-line-reference#flag--test_strategy
test:debug --test_strategy=exclusive

# Prevent long running tests from timing out.
# Docs: https://bazel.build/docs/user-manual#test-timeout
test:debug --test_timeout=9999

# Always run tests even if they have cached results.
# Docs: https://bazel.build/docs/user-manual#cache-test-results
test:debug --nocache_test_results
27 changes: 27 additions & 0 deletions .aspect/bazelrc/java.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Aspect recommended Bazel flags when using rules_java and rules_jvm_external

# Pin java versions to desired language level
# See https://bazel.build/docs/bazel-and-java#java-versions
# and https://en.wikipedia.org/wiki/Java_version_history

# What version of Java are the source files in this repo?
# See https://bazel.build/docs/user-manual#java-language-version
common --java_language_version=17

# The Java language version used to build tools that are executed during a build
# See https://bazel.build/docs/user-manual#tool-java-language-version
common --tool_java_language_version=17

# The version of JVM to use to execute the code and run the tests.
# NB: The default value is local_jdk which is non-hermetic.
# See https://bazel.build/docs/user-manual#java-runtime-version
common --java_runtime_version=remotejdk_17

# The version of JVM used to execute tools that are needed during a build.
# See https://bazel.build/docs/user-manual#tool-java-runtime-version
common --tool_java_runtime_version=remotejdk_17

# Repository rules, such as rules_jvm_external: put Bazel's JDK on the path.
# Avoids non-hermeticity from dependency on a JAVA_HOME pointing at a system JDK
# see https://github.com/bazelbuild/rules_jvm_external/issues/445
common --repo_env=JAVA_HOME=../bazel_tools/jdk
11 changes: 11 additions & 0 deletions .aspect/bazelrc/javascript.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Aspect recommended Bazel flags when using Aspect's JavaScript rules: https://github.com/aspect-build/rules_js
# Docs for Node.js flags: https://nodejs.org/en/docs/guides/debugging-getting-started/#command-line-options

# Support for debugging Node.js tests. Use bazel run with `--config=debug` to turn on the NodeJS
# inspector agent. The node process will break before user code starts and wait for the debugger to
# connect. Pass the --inspect-brk option to all tests which enables the node inspector agent. See
# https://nodejs.org/de/docs/guides/debugging-getting-started/#command-line-options for more
# details.
# Docs: https://nodejs.org/en/docs/guides/debugging-getting-started/#command-line-options
run:debug -- --node_options=--inspect-brk
test:debug --test_env=NODE_OPTIONS=--inspect-brk
27 changes: 27 additions & 0 deletions .aspect/bazelrc/performance.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Directories used by sandboxed non-worker execution may be reused to avoid unnecessary setup costs.
# Save time on Sandbox creation and deletion when many of the same kind of action run during the
# build.
# No longer experimental in Bazel 6: https://github.com/bazelbuild/bazel/commit/c1a95501a5611878e5cc43a3cc531f2b9e47835b
# Docs: https://bazel.build/reference/command-line-reference#flag--reuse_sandbox_directories
build --experimental_reuse_sandbox_directories

# Do not build runfiles symlink forests for external repositories under
# `.runfiles/wsname/external/repo` (in addition to `.runfiles/repo`). This reduces runfiles &
# sandbox creation times & prevents accidentally depending on this feature which may flip to off by
# default in the future. Note, some rules may fail under this flag, please file issues with the rule
# author.
# Docs: https://bazel.build/reference/command-line-reference#flag--legacy_external_runfiles
build --nolegacy_external_runfiles

# Avoid creating a runfiles tree for binaries or tests until it is needed.
# Docs: https://bazel.build/reference/command-line-reference#flag--build_runfile_links
# See https://github.com/bazelbuild/bazel/issues/6627
#
# This may break local workflows that `build` a binary target, then run the resulting program
# outside of `bazel run`. In those cases, the script will need to call
# `bazel build --build_runfile_links //my/binary:target` and then execute the resulting program.
build --nobuild_runfile_links

# Needed prior to Bazel 8; see
# https://github.com/bazelbuild/bazel/issues/20577
coverage --build_runfile_links
53 changes: 38 additions & 15 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
# Import Apsect bazelrc presets
# From https://docs.aspect.build/guides/bazelrc/
# & https://github.com/bazel-contrib/bazel-lib/tree/5853e2e441df850dc332910a0808e9dee2ed9e27/.aspect/bazelrc
import %workspace%/.aspect/bazelrc/bazel7.bazelrc
import %workspace%/.aspect/bazelrc/convenience.bazelrc
import %workspace%/.aspect/bazelrc/correctness.bazelrc
import %workspace%/.aspect/bazelrc/debug.bazelrc
import %workspace%/.aspect/bazelrc/java.bazelrc
import %workspace%/.aspect/bazelrc/javascript.bazelrc
import %workspace%/.aspect/bazelrc/performance.bazelrc
#
# NOTE for:
# build --incompatible_default_to_explicit_init_py
#
# This also prevents spurious package name collisions at import time, and
# should be the default (https://github.com/bazelbuild/bazel/issues/7386). It's
# particularly helpful for Pigweed, because we have many potential package name
# collisions due to a profusion of stuttering paths like
# pw_transfer/py/pw_transfer.
#
# NOTE for:
# build --incompatible_strict_action_env
#
# Don't inherit system PATH. Improves hermeticity and cache hit rates. Should
# be true by default one day (https://github.com/bazelbuild/bazel/issues/7026).

# My overrides of the above imports
# TODO(#172): Update these
#
# pw_protobuf/BUILD.bazel, llvm_toolchain
common --noincompatible_disallow_empty_glob
# loud / I don't want to set `timeout=short` on all tests
test --notest_verbose_timeout_warnings
# No module named 'pw_protobuf_codegen_protos' in pw_protobuf options
build --legacy_external_runfiles

# See https://bazel.build/docs/user-manual for option definitions
#
# Standard Pigweed flags
Expand All @@ -20,19 +56,6 @@ build --incompatible_enable_cc_toolchain_resolution
common --experimental_exclude_defines_from_exec_config
common --experimental_exclude_starlark_flags_from_exec_config

# Don't automatically create __init__.py files.
#
# This prevents spurious package name collisions at import time, and should be
# the default (https://github.com/bazelbuild/bazel/issues/7386). It's
# particularly helpful for Pigweed, because we have many potential package name
# collisions due to a profusion of stuttering paths like
# pw_transfer/py/pw_transfer.
common --incompatible_default_to_explicit_init_py

# Don't inherit system PATH. Improves hermeticity and cache hit rates. Should
# be true by default one day (https://github.com/bazelbuild/bazel/issues/7026).
common --incompatible_strict_action_env

# Expose exec toolchains for Python. We use these toolchains in some rule
# implementations (git grep for
# "@rules_python//python:exec_tools_toolchain_type").
Expand All @@ -59,8 +82,6 @@ build --@pico-sdk//bazel/config:PICO_STDIO_UART=True
# ===========
# Error output settings.
common --verbose_failures
# View failed test output
test --test_output=errors

# Platform configuration
# ======================
Expand Down Expand Up @@ -158,4 +179,6 @@ build --output_groups=+mypy
# note: this should be at the bottom of the file, so that user-specified
# options override anything in this file
# (https://bazel.build/run/bazelrc#imports)
#
# Note that ci configures this file to specify ci overrides, etc.
try-import %workspace%/user.bazelrc
1 change: 1 addition & 0 deletions .github/actions/bazel-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ runs:
run: |
cat <<EOF > user.bazelrc
build --remote_header=x-buildbuddy-api-key="$BUILDBUDDY_API_KEY"
import %workspace%/.aspect/bazelrc/ci.bazelrc
EOF
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ npm_link_all_packages(name = "node_modules")

filegroup(
name = "python_source",
srcs = glob(["*.py"]) + [
srcs = glob(
["*.py"],
allow_empty = True,
) + [
"//apps:python_source",
"//examples/basic:python_source",
"//examples/bazel:python_source",
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module(
########################################
# Generic deps
########################################
bazel_dep(name = "aspect_bazel_lib", version = "2.9.3")
bazel_dep(name = "aspect_bazel_lib", version = "2.9.4")

########################################
# Set up rules_python and pip
Expand Down
Loading

0 comments on commit 4c83275

Please sign in to comment.