Skip to content

Commit

Permalink
Teach bazelisk.py about .bazeliskrc
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri committed Aug 25, 2023
1 parent 70e3e87 commit 274c2b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ You can control the user agent that Bazelisk sends in all HTTP requests by setti

# .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.
A `.bazeliskrc` file in the root directory of a workspace or the user home directory allows users to set environment variables persistently.

Example file content:

Expand All @@ -171,21 +171,21 @@ BAZELISK_GITHUB_TOKEN=abc
The following variables can be set:

- `BAZELISK_BASE_URL`
- `BAZELISK_CLEAN`
- `BAZELISK_GITHUB_TOKEN`
- `BAZELISK_CLEAN` (Go version only)
- `BAZELISK_GITHUB_TOKEN` (Go version only)
- `BAZELISK_HOME`
- `BAZELISK_INCOMPATIBLE_FLAGS`
- `BAZELISK_SHUTDOWN`
- `BAZELISK_SKIP_WRAPPER`
- `BAZELISK_USER_AGENT`
- `BAZELISK_VERIFY_SHA256`
- `BAZELISK_INCOMPATIBLE_FLAGS` (Go version only)
- `BAZELISK_SHUTDOWN` (Go version only)
- `BAZELISK_SKIP_WRAPPER` (Go version only)
- `BAZELISK_USER_AGENT` (Go version only)
- `BAZELISK_VERIFY_SHA256` (Go version only)
- `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`
* Variables defined in the user home `.bazeliskrc` (Go version only)

## Requirements

Expand Down
40 changes: 33 additions & 7 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@
BAZEL_UPSTREAM = "bazelbuild"


def get_env_or_config(name, default=None):
"""Reads a configuration value from the environment, but falls back to
reading it from .bazeliskrc in the workspace root."""
if name in os.environ:
return os.environ[name]
env_files = []
root = find_workspace_root()
if root:
env_files.append(os.path.join(root, ".bazeliskrc"))
for env_file in env_files:
try:
with open(env_file, "r") as f:
for line in f.readlines():
line = line.split("#", maxsplit=1)[0].strip()
if not line:
continue
some_name, some_value = line.split("=", maxsplit=1)
if some_name == name:
return some_value
except Exception:
pass
return default


def decide_which_bazel_version_to_use():
# Check in this order:
# - env var "USE_BAZEL_VERSION" is set to a specific version.
Expand All @@ -79,8 +103,9 @@ def decide_which_bazel_version_to_use():
# - workspace_root/.bazelversion exists -> read contents, that version.
# - workspace_root/WORKSPACE contains a version -> that version. (TODO)
# - fallback: latest release
if "USE_BAZEL_VERSION" in os.environ:
return os.environ["USE_BAZEL_VERSION"]
use_bazel_version = get_env_or_config("USE_BAZEL_VERSION")
if use_bazel_version is not None:
return use_bazel_version

workspace_root = find_workspace_root()
if workspace_root:
Expand Down Expand Up @@ -226,7 +251,7 @@ def determine_bazel_filename(version):

filename_suffix = determine_executable_filename_suffix()
bazel_flavor = "bazel"
if os.environ.get("BAZELISK_NOJDK", "0") != "0":
if get_env_or_config("BAZELISK_NOJDK", "0") != "0":
bazel_flavor = "bazel_nojdk"
return "{}-{}-{}-{}{}".format(bazel_flavor, version, operating_system, machine, filename_suffix)

Expand Down Expand Up @@ -277,8 +302,9 @@ def determine_url(version, is_commit, bazel_filename):
# Example: '0.19.1' -> ('0.19.1', None), '0.20.0rc1' -> ('0.20.0', 'rc1')
(version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups()

if "BAZELISK_BASE_URL" in os.environ:
return "{}/{}/{}".format(os.environ["BAZELISK_BASE_URL"], version, bazel_filename)
bazelisk_base_url = get_env_or_config("BAZELISK_BASE_URL")
if bazelisk_base_url is not None:
return "{}/{}/{}".format(bazelisk_base_url, version, bazel_filename)
else:
return "https://releases.bazel.build/{}/{}/{}".format(
version, rc if rc else "release", bazel_filename
Expand Down Expand Up @@ -342,7 +368,7 @@ def download_bazel_into_directory(version, is_commit, directory):
def download(url, destination_path):
sys.stderr.write("Downloading {}...\n".format(url))
request = Request(url)
if "BAZELISK_BASE_URL" in os.environ:
if get_env_or_config("BAZELISK_BASE_URL") is not None:
parts = urlparse(url)
creds = None
try:
Expand All @@ -357,7 +383,7 @@ def download(url, destination_path):


def get_bazelisk_directory():
bazelisk_home = os.environ.get("BAZELISK_HOME")
bazelisk_home = get_env_or_config("BAZELISK_HOME")
if bazelisk_home is not None:
return bazelisk_home

Expand Down

0 comments on commit 274c2b4

Please sign in to comment.