• Stars
    star
    2,060
  • Rank 22,444 (Top 0.5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A user-friendly launcher for Bazel.

Bazelisk

A user-friendly launcher for Bazel.

About Bazelisk

Bazelisk is a wrapper for Bazel written in Go. It automatically picks a good version of Bazel given your current working directory, downloads it from the official server (if required) and then transparently passes through all command-line arguments to the real Bazel binary. You can call it just like you would call Bazel.

Installation

On macOS: brew install bazelisk.

On Windows: choco install bazelisk.

Each adds bazelisk to the PATH as both bazelisk and bazel.

On Linux: You can download Bazelisk binary on our Releases page and add it to your PATH manually, which also works on macOS and Windows.

Bazelisk is also published to npm. Frontend developers may want to install it with npm install -g @bazel/bazelisk.

You will notice that it serves an analogous function for Bazel as the nvm utility which manages your version of Node.js.

Some ideas how to use it:

  • Install it as the bazel binary in your PATH (e.g. copy it to /usr/local/bin/bazel). Never worry about upgrading Bazel to the latest version again.
  • Check it into your repository and recommend users to build your software via ./bazelisk build //my:software. That way, even someone who has never used Bazel or doesn't have it installed can build your software.
  • As a company using Bazel or as a project owner, add a .bazelversion file to your repository. This will tell Bazelisk to use the exact version specified in the file when running in your workspace. The fact that it's versioned inside your repository will then allow for atomic upgrades of Bazel including all necessary changes. If you install Bazelisk as bazel on your CI machines, too, you can even test Bazel upgrades via a normal presubmit / pull request. It will also ensure that users will not try to build your project with an incompatible version of Bazel, which is often a cause for frustration and failing builds. (But see the note below about ensuring your developers install Bazelisk.)

Before Bazelisk was rewritten in Go, it was a Python script. This still works and has the advantage that you can run it on any platform that has a Python interpreter, but is currently unmaintained and it doesn't support as many features. The documentation below describes the newer Go version only.

How does Bazelisk know which Bazel version to run?

It uses a simple algorithm:

  • If the environment variable USE_BAZEL_VERSION is set, it will use the version specified in the value.
  • Otherwise, if a .bazeliskrc file exists in the workspace root and contains the USE_BAZEL_VERSION variable, this version will be used.
  • Otherwise, if a .bazelversion file exists in the current directory or recursively any parent directory, it will read the file and use the version specified in it.
  • Otherwise, if the environment variable USE_BAZEL_FALLBACK_VERSION is set to one of the following formats:
    • If set to a value starting with error:, it will report an error and version detection will fail.
    • If set to a value starting with warn:, it will report a warning and use the version specified after the prefix.
    • If set to a value starting with silent:, it will use the version specified after the prefix.
  • Otherwise, it will use the official latest Bazel release.

A version can optionally be prefixed with a fork name. The fork and version should be separated by slash: <FORK>/<VERSION>. Please see the next section for how to work with forks.

Bazelisk currently understands the following formats for version labels:

  • latest means the latest stable (LTS) version of Bazel as released on GitHub. Previous releases can be specified via latest-1, latest-2 etc.
  • A version number like 0.17.2 means that exact version of Bazel. It can also be a release candidate version like 0.20.0rc3, or a rolling release version like 5.0.0-pre.20210317.1.
  • A floating version identifier like 4.x that returns the latest release from the LTS series started by Bazel 4.0.0.
  • The hash of a Git commit. Please note that Bazel binaries are only available for commits that passed Bazel CI.

Additionally, a few special version names are supported for our official releases only (these formats do not work when using a fork):

  • last_green refers to the Bazel binary that was built at the most recent commit that passed Bazel CI. Ideally this binary should be very close to Bazel-at-head.
  • last_downstream_green points to the most recent Bazel binary that builds and tests all downstream projects successfully.
  • last_rc points to the most recent release candidate. If there is no active release candidate, Bazelisk uses the latest Bazel release instead.
  • rolling refers to the latest rolling release (even if there is a newer LTS release).

Where does Bazelisk get Bazel from?

By default Bazelisk retrieves Bazel releases, release candidates and binaries built at green commits from Google Cloud Storage. The downloaded artifacts are validated against the SHA256 value recorded in BAZELISK_VERIFY_SHA256 if this variable is set in the configuration file.

As mentioned in the previous section, the <FORK>/<VERSION> version format allows you to use your own Bazel fork hosted on GitHub:

If you want to create a fork with your own releases, you should follow the naming conventions that we use in bazelbuild/bazel for the binary file names as this results in predictable URLs that are similar to the official ones. The URL format looks like https://github.com/<FORK>/bazel/releases/download/<VERSION>/<FILENAME>.

You can also override the URL by setting the environment variable $BAZELISK_BASE_URL. Bazelisk will then append /<VERSION>/<FILENAME> to the base URL instead of using the official release server. Bazelisk will read file ~/.netrc for credentials for Basic authentication.

If for any reason none of this works, you can also override the URL format altogether by setting the environment variable $BAZELISK_FORMAT_URL. This variable takes a format-like string with placeholders and performs the following replacements to compute the download URL:

  • %e: Extension suffix, such as the empty string or .exe.
  • %h: Value of BAZELISK_VERIFY_SHA256, respecting uppercase/lowercase characters.
  • %m: Machine architecture name, such as arm64 or x86_64.
  • %o: Operating system name, such as darwin or linux.
  • %v: Bazel version as determined by Bazelisk.
  • %%: Literal % for escaping purposes.
  • All other characters after % are reserved for future use and result in a processing error.

Ensuring that your developers use Bazelisk rather than Bazel

Bazel installers typically provide Bazel's shell wrapper script as the bazel on the PATH.

When installed this way, Bazel checks the .bazelversion file itself, but the failure when it mismatches with the actual version of Bazel can be quite confusing to developers. You may find yourself having to explain the difference between Bazel and Bazelisk (especially when you upgrade the pinned version). To avoid this, you can add a check in your tools/bazel wrapper. Since Bazelisk is careful to avoid calling itself in a loop, it always calls the wrapper with the environment variable BAZELISK_SKIP_WRAPPER set to `true'. You can check for the presence of that variable, and when not found, report a useful error to your users about how to install Bazelisk.

Note that if users directly downloaded a Bazel binary and put it in their PATH, rather than running an installer, then tools/bazel and .bazelversion are not checked. You could call the versions.check starlark module from the beginning of your WORKSPACE to require users update their bazel.

Other features

The Go version of Bazelisk offers three new flags.

--strict

--strict expands to the set of incompatible flags which may be enabled for the given version of Bazel.

bazelisk --strict build //...

--migrate

--migrate will run Bazel multiple times to help you identify compatibility issues. If the code fails with --strict, the flag --migrate will run Bazel with each one of the flag separately, and print a report at the end. This will show you which flags can safely enabled, and which flags require a migration.

--bisect

--bisect flag allows you to bisect Bazel versions to find which version introduced a build failure. You can specify the range of versions to bisect with --bisect=<GOOD>..<BAD>, where GOOD is the last known working Bazel version and BAD is the first known non-working Bazel version. Bazelisk uses GitHub's compare API to get the list of commits to bisect. When GOOD is not an ancestor of BAD, GOOD is reset to their merge base commit.

Examples:

# Bisect between 6.0.0 and Bazel at HEAD
bazelisk --bisect=6.0.0..HEAD test //foo:bar_test

# Bisect between 6.1.0 and the second release candidate of Bazel 6.2.0
bazelisk --bisect=6.1.0..release-6.2.0rc2 test //foo:bar_test

# Bisect between two commits on the main branch (or branches with `release-` prefix) of the Bazel GitHub repository.
bazelisk --bisect=<good commit hash>..<bad commit hash> test //foo:bar_test

Note that, Bazelisk uses prebuilt Bazel binaries at commits on the main and release branches, therefore you cannot bisect your local commits.

Useful environment variables for --migrate and --bisect

You can set BAZELISK_INCOMPATIBLE_FLAGS to set a list of incompatible flags (separated by ,) to be tested, otherwise Bazelisk tests all flags starting with --incompatible_.

You can set BAZELISK_GITHUB_TOKEN to set a GitHub access token to use for API requests to avoid rate limiting when on shared networks.

You can set BAZELISK_SHUTDOWN to run shutdown between builds when migrating or bisecting if you suspect this affects your results.

You can set BAZELISK_CLEAN to run clean --expunge between builds when migrating or bisecting if you suspect this affects your results.

tools/bazel

If tools/bazel exists in your workspace root and is executable, Bazelisk will run this file, instead of the Bazel version it downloaded. It will set the environment variable BAZEL_REAL to the path of the downloaded Bazel binary. This can be useful, if you have a wrapper script that e.g. ensures that environment variables are set to known good values. This behavior can be disabled by setting the environment variable BAZELISK_SKIP_WRAPPER to any value (except the empty string) before launching Bazelisk.

You can control the user agent that Bazelisk sends in all HTTP requests by setting BAZELISK_USER_AGENT to the desired value.

.bazeliskrc configuration file

The Go version supports a .bazeliskrc file in the root directory of a workspace and the user home directory. This file allows users to set environment variables persistently.

Example file content:

USE_BAZEL_VERSION=0.19.0
BAZELISK_GITHUB_TOKEN=abc

The following variables can be set:

  • BAZELISK_BASE_URL
  • BAZELISK_CLEAN
  • BAZELISK_GITHUB_TOKEN
  • BAZELISK_HOME
  • BAZELISK_INCOMPATIBLE_FLAGS
  • BAZELISK_SHUTDOWN
  • BAZELISK_SKIP_WRAPPER
  • BAZELISK_USER_AGENT
  • BAZELISK_VERIFY_SHA256
  • USE_BAZEL_VERSION

Configuration variables are evaluated with precedence order. The preferred values are derived in order from highest to lowest precedence as follows:

  • Variables defined in the environment
  • Variables defined in the workspace root .bazeliskrc
  • Variables defined in the user home .bazeliskrc

Requirements

For ease of use, the Python version of Bazelisk is written to work with Python 2.7 and 3.x and only uses modules provided by the standard library.

The Go version can be compiled to run natively on Linux, macOS and Windows. You need at least Go 1.11 to build Bazelisk, otherwise you'll run into errors like undefined: os.UserCacheDir.

To install the Go version, type:

go get github.com/bazelbuild/bazelisk

With Go 1.17 or later, the recommended way to install it is:

go install github.com/bazelbuild/bazelisk@latest

To add it to your PATH:

export PATH=$PATH:$(go env GOPATH)/bin

For more information, you may read about the GOPATH environment variable.

Ideas for the future

  • Add support for checked-in Bazel binaries.
  • When the version label is set to a commit hash, first download a matching binary version of Bazel, then build Bazel automatically at that commit and use the resulting binary.
  • Add support to automatically bisect a build failure to a culprit commit in Bazel. If you notice that you could successfully build your project using version X, but not using version X+1, then Bazelisk should be able to figure out the commit that caused the breakage and the Bazel team can easily fix the problem.

FAQ

Where does Bazelisk store the downloaded versions of Bazel?

It creates a directory called "bazelisk" inside your user cache directory and will store them there. Feel free to delete this directory at any time, as it can be regenerated automatically when required.

More Repositories

1

bazel

a fast, scalable, multi-language and extensible build system
Java
22,794
star
2

starlark

Starlark Language
Starlark
2,435
star
3

rules_go

Go rules for Bazel
Go
1,356
star
4

bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Go
1,156
star
5

rules_docker

Rules for building and handling Docker images with Bazel
Starlark
1,074
star
6

buildtools

A bazel BUILD file formatter and editor
Go
1,007
star
7

examples

Examples for Bazel
Starlark
831
star
8

intellij

IntelliJ plugin for Bazel projects
Java
762
star
9

rules_rust

Rust rules for Bazel
Starlark
660
star
10

bazel-buildfarm

Bazel remote caching and execution service
Java
650
star
11

tulsi

An Xcode Project Generator For Bazel
Swift
547
star
12

rules_python

Bazel Python Rules
Starlark
521
star
13

rules_apple

Bazel rules to build apps for Apple platforms.
Starlark
509
star
14

bazel-watcher

Tools for building Bazel targets when source files change.
Go
440
star
15

bazel-skylib

Common useful functions and rules for Bazel
Starlark
393
star
16

sandboxfs

A virtual file system for sandboxing
Rust
371
star
17

rules_scala

Scala rules for Bazel
Starlark
360
star
18

rules_kotlin

Bazel rules for Kotlin
Kotlin
331
star
19

remote-apis

An API for caching and execution of actions on a remote system.
Go
328
star
20

rules_swift

Bazel rules to build Swift on Apple and Linux platforms
Starlark
311
star
21

rules_k8s

This repository contains rules for interacting with Kubernetes configurations / clusters.
Starlark
289
star
22

rules_typescript

MOVED to https://github.com/bazelbuild/rules_nodejs/tree/3.x/third_party/github.com/bazelbuild/rules_typescript
TypeScript
275
star
23

continuous-integration

Bazel's Continuous Integration Setup
Python
258
star
24

bazel-central-registry

The central registry of Bazel modules for the Bzlmod external dependency system.
Starlark
247
star
25

rules_pkg

Bazel rules for creating packages of many types (zip, tar, deb, rpm, ...)
Starlark
225
star
26

rules_cc

C++ Rules for Bazel
Starlark
184
star
27

bazel-toolchains

Repository that hosts Bazel toolchain configs for remote execution and related support tools.
Go
183
star
28

rules_android

Android rules for Bazel
Starlark
175
star
29

rules_proto

Protocol buffer rules for Bazel
Starlark
163
star
30

rules_closure

Closure rules for Bazel
Java
153
star
31

vim-bazel

Vim support for Bazel
Vim Script
144
star
32

platforms

Constraint values for specifying platforms and toolchains
Starlark
108
star
33

stardoc

Stardoc: Starlark Documentation Generator
Java
107
star
34

proposals

Index of all Bazel proposals and design documents
106
star
35

rules_webtesting

Bazel rules to allow testing against a browser with WebDriver.
Go
96
star
36

apple_support

Apple support for Bazel rules
Starlark
83
star
37

emacs-bazel-mode

Emacs mode for Bazel
Emacs Lisp
80
star
38

rules_license

Starlark
77
star
39

rules_java

Java rules for Bazel
Starlark
72
star
40

reclient

Go
65
star
41

setup-bazelisk

Set up your GitHub Actions workflow with a specific version of Bazelisk
TypeScript
56
star
42

rules_sass

Sass rules for Bazel
Starlark
51
star
43

remote-apis-sdks

This repository contains client libraries for the Remote Execution API https://github.com/bazelbuild/remote-apis
Go
48
star
44

bazel-federation

Starlark
47
star
45

skydoc

Documentation generator for Skylark
Python
46
star
46

migration-tooling

Migration tools for Bazel
Java
44
star
47

codelabs

Shell
39
star
48

BUILD_file_generator

Generate BUILD files for your Java files
Java
39
star
49

eclipse

Eclipse For Bazel (deprecated, see https://github.com/salesforce/bazel-eclipse instead)
Java
32
star
50

bazel-integration-testing

Framework for integration tests that call Bazel
Java
32
star
51

rules_appengine

AppEngine rules for Bazel
Starlark
30
star
52

rules_android_ndk

Starlark
29
star
53

tools_remote

Java
28
star
54

bazel-bench

Benchmarking tool for bazel
Python
27
star
55

vim-ft-bzl

Vim Script
27
star
56

rules_perl

Perl rules for Bazel
Starlark
25
star
57

tools_android

Tools for use with building Android apps with Bazel
Starlark
24
star
58

homebrew-tap

This repository contains a collection of Homebrew (aka, Brew) "formulae" for Bazel
24
star
59

rules_d

D rules for Bazel
Starlark
24
star
60

bazel-blog

Content of the Bazel blog
HTML
20
star
61

bzlmod

Go
20
star
62

rules_testing

Starlark testing framework and utility libraries
Starlark
20
star
63

rules_gwt

Bazel rules for GWT
Starlark
19
star
64

bazel-website

Website for Bazel, a fast, scalable, multi-language and extensible build system
HTML
17
star
65

bazelcon

Artifacts from BazelCon
15
star
66

java_tools

Python
12
star
67

rules_groovy

Groovy rules for Bazel
Starlark
11
star
68

rules_postcss

PostCSS rules for Bazel
Starlark
10
star
69

rules_platform

Starlark
8
star
70

bazel_metrics

Python
7
star
71

community

Resources for community management efforts, such as SIGs
6
star
72

rules_angular

OBSOLETE see https://github.com/angular/angular/tree/master/packages/bazel
Python
5
star
73

gmaven_rules

This repository is deprecated. Please instead use https://github.com/bazelbuild/rules_jvm_external
Python
3
star
74

rules_utp

Starlark
2
star
75

.allstar

1
star
76

.github

1
star
77

bazel-worker-api

Java
1
star
78

build-event-stream

1
star
79

rules_sh

1
star