Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bazelrc-env-example

A minimal example repo demonstrating a workaround for bazelbuild/bazel#10904 — the long-standing request for environment variable interpolation inside .bazelrc files.

Bazel's .bazelrc does not natively expand client environment variables, which makes it awkward to thread values like build IDs, user names, or tokens from the surrounding shell into the build graph. This repo shows one way to get the effect using bazelisk's tools/bazel wrapper script with an interpolation function such as envsubst.

To use this example, you must have bazelisk and envsubst installed.

How the example works

  • .bazelrc defined the flag build --action_env=GREETING_MESSAGE="Hello $MY_MESSAGE!".
  • When bazel is invoked, bazelisk invokes the tools/bazel wrapper script, which calls envsubst to render a copy of the workspace .bashrc. In this copy, $MY_MESSAGE is interpolated from the invoking shell environment. This rendered version of .bashrc is then inserted into the command flags used by the actual bazel invocation.
  • BUILD.bazel defines a genrule that reads $GREETING_MESSAGE at action time and writes a greeting.h containing #define GREETING_MESSAGE "...".
  • hello.cc includes the generated header and prints the macro.

This results in the value of $MY_MESSAGE ending up in a string literal compiled into the //:hello binary.

Demo

MY_MESSAGE="world" bazel run //:hello
INFO: Analyzed target //:hello (68 packages loaded, 477 targets configured).
INFO: Found 1 target...
Target //:hello up-to-date:
  bazel-bin/hello
INFO: Elapsed time: 0.469s, Critical Path: 0.26s
INFO: 5 processes: 2 action cache hit, 2 internal, 3 linux-sandbox.
INFO: Build completed successfully, 5 total actions
INFO: Running command line: bazel-bin/hello
Hello world!

Change MY_MESSAGE and rerun to see the compiled string literal change accordingly:

MY_MESSAGE="from bzlmod" bazel run //:hello
...
Hello from bzlmod!

Layout

License

0BSD.

About

A minimal example repo demonstrating a workaround for `bazelbuild/bazel#10904`

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages