Known issues
Windows-related Bazel issues are marked with the “area-Windows” label on GitHub. GitHub-Windows.Best practices
Avoid long path issues
Some tools have the Maximum Path Length Limitation on Windows, including the MSVC compiler. To avoid hitting this issue, you can specify a short output directory for Bazel by the —output_user_root flag. For example, add the following line to your bazelrc file:Enable symlink support
Some features require Bazel to be able to create file symlinks on Windows, either by enabling Developer Mode (on Windows 10 version 1703 or newer), or by running Bazel as an administrator. This enables the following features: To make it easier, add the following lines to your bazelrc file:--enable_runfiles flag can potentially create a large amount of file symlinks. Only enable this feature when you need it.
Running Bazel: MSYS2 shell vs. command prompt vs. PowerShell
Recommendation: Run Bazel from the command prompt (cmd.exe) or from
PowerShell.
As of 2020-01-15, do not run Bazel from bash — either
from MSYS2 shell, or Git Bash, or Cygwin, or any other Bash variant. While Bazel
may work for most use cases, some things are broken, like
interrupting the build with Ctrl+C from MSYS2).
Also, if you choose to run under MSYS2, you need to disable MSYS2’s
automatic path conversion, otherwise MSYS will convert command line arguments
that look like Unix paths (such as //foo:bar) into Windows paths. See
this StackOverflow answer
for details.
Using Bazel without Bash (MSYS2)
Using bazel build without Bash
Bazel versions before 1.0 used to require Bash to build some rules. Starting with Bazel 1.0, you can build any rule without Bash unless it is a:genrule, because genrules execute Bash commandssh_binaryorsh_testrule, because these inherently need Bash- Starlark rule that uses
ctx.actions.run_shell()orctx.resolve_command()
genrule is often used for simple tasks like
copying a file
or writing a text file.
Instead of using genrule (and depending on Bash) you may find a suitable rule
in the
bazel-skylib repository.
When built on Windows, these rules do not require Bash.
Using bazel test without Bash
Bazel versions before 1.0 used to require Bash tobazel test anything.
Starting with Bazel 1.0, you can test any rule without Bash, except when:
- you use
--run_under - the test rule itself requires Bash (because its executable is a shell script)
Using bazel run without Bash
Bazel versions before 1.0 used to require Bash tobazel run anything.
Starting with Bazel 1.0, you can run any rule without Bash, except when:
- you use
--run_underor--script_path - the test rule itself requires Bash (because its executable is a shell script)
Using sh_binary and sh_* rules, and ctx.actions.run_shell() without Bash
You need Bash to build and testsh_* rules, and to build and test Starlark
rules that use ctx.actions.run_shell() and ctx.resolve_command(). This
applies not only to rules in your project, but to rules in any of the external
repositories your project depends on (even transitively).
In the future, there may be an option to use Windows Subsystem for
Linux (WSL) to build these rules, but currently it is not a priority for
the Bazel-on-Windows subteam.
Setting environment variables
Environment variables you set in the Windows Command Prompt (cmd.exe) are only
set in that command prompt session. If you start a new cmd.exe, you need to
set the variables again. To always set the variables when cmd.exe starts, you
can add them to the User variables or System variables in the Control Panel > System Properties > Advanced > Environment Variables... dialog box.
Build on Windows
Build C++ with MSVC
To build C++ targets with MSVC, you need:- The Visual C++ compiler.
-
(Optional) The
BAZEL_VCandBAZEL_VC_FULL_VERSIONenvironment variable. Bazel automatically detects the Visual C++ compiler on your system. To tell Bazel to use a specific VC installation, you can set the following environment variables: For Visual Studio 2017 and 2019, set one ofBAZEL_VC. Additionally you may also setBAZEL_VC_FULL_VERSION.-
BAZEL_VCthe Visual C++ Build Tools installation directory -
BAZEL_VC_FULL_VERSION(Optional) Only for Visual Studio 2017 and 2019, the full version number of your Visual C++ Build Tools. You can choose the exact Visual C++ Build Tools version viaBAZEL_VC_FULL_VERSIONif more than one version are installed, otherwise Bazel will choose the latest version.
BAZEL_VC. (BAZEL_VC_FULL_VERSIONis not supported.)-
BAZEL_VCthe Visual C++ Build Tools installation directory
-
-
The Windows
SDK.
The Windows SDK contains header files and libraries you need when building
Windows applications, including Bazel itself. By default, the latest Windows SDK installed will
be used. You also can specify Windows SDK version by setting
BAZEL_WINSDK_FULL_VERSION. You can use a full Windows 10 SDK number such as 10.0.10240.0, or specify 8.1 to use the Windows 8.1 SDK (only one version of Windows 8.1 SDK is available). Please make sure you have the specified Windows SDK installed. Requirement: This is supported with VC 2017 and 2019. The standalone VC 2015 Build Tools doesn’t support selecting Windows SDK, you’ll need the full Visual Studio 2015 installation, otherwiseBAZEL_WINSDK_FULL_VERSIONwill be ignored.
@local_config_cc in MODULE.bazel with
--features=compiler_param_file.
Build C++ with Clang
From 0.29.0, Bazel supports building with LLVM’s MSVC-compatible compiler driver (clang-cl.exe).
Requirement: To build with Clang, you have to install both
LLVM and Visual C++ Build tools,
because although you use clang-cl.exe as compiler, you still need to link to
Visual C++ libraries.
Bazel can automatically detect LLVM installation on your system, or you can explicitly tell
Bazel where LLVM is installed by BAZEL_LLVM.
-
BAZEL_LLVMthe LLVM installation directory
-
In Bazel 7.0.0 and newer: Add a platform target to your
BUILD file(eg. the top levelBUILDfile):Then enable the Clang toolchain by specifying the following build flags: -
In Bazel older than 7.0.0 but newer than 0.28: Enable the Clang toolchain by
a build flag
--compiler=clang-cl. If your build sets the flag [—incompatible_enable_cc_toolchain_resolution] (https://github.com/bazelbuild/bazel/issues/7260) totrue, then use the approach for Bazel 7.0.0. - In Bazel 0.28 and older: Clang is not supported.
Build Java
To build Java targets, you need: On Windows, Bazel builds two output files forjava_binary rules:
- a
.jarfile - a
.exefile that can set up the environment for the JVM and run the binary
Build Python
To build Python targets, you need: On Windows, Bazel builds two output files forpy_binary rules:
- a self-extracting zip file
- an executable file that can launch the Python interpreter with the self-extracting zip file as the argument
.exe extension) or you can run
Python with the self-extracting zip file as the argument.
Try building a target from one of our sample
projects: