From 48a55ca9bbbdeb4e638b7b7a19ae1b91175928c3 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Tue, 10 Dec 2024 06:59:52 -0800 Subject: [PATCH] [tools] Finish porting to bzlmod Specifically, respell the "download buildifier release binaries from github" into something bzlmod is happy with. While we're here, also remove custom upgrade scripting for `bazel`. Dependabot handles that upgrade automation for us now. --- .bazelrc | 4 --- BUILD.bazel | 1 - MODULE.bazel | 6 ++++ WORKSPACE.bzlmod | 21 ------------ tools/BUILD.bazel | 3 +- tools/buildifier_repositories.bzl | 21 ++++++++++++ tools/buildifier_version.bzl | 9 ++++++ tools/upgrade.sh | 2 +- tools/upgrade_helper.py | 54 ++++++++----------------------- tools/workspace_versions.bzl | 11 ------- 10 files changed, 53 insertions(+), 79 deletions(-) delete mode 100644 WORKSPACE.bzlmod create mode 100644 tools/buildifier_repositories.bzl create mode 100644 tools/buildifier_version.bzl delete mode 100644 tools/workspace_versions.bzl diff --git a/.bazelrc b/.bazelrc index dfae249..b8df98f 100644 --- a/.bazelrc +++ b/.bazelrc @@ -5,10 +5,6 @@ common --keep_going=yes build --test_output=errors build --test_summary=terse -# TODO(jwnimmer-tri) For the moment, we still need WORKSPACE.bzlmod. -# Once we can delete that file, we should remove the next line as well. -common --enable_workspace=true - # Add `bazel test --config=lint` shortcut for linting. build:lint --test_tag_filters=lint diff --git a/BUILD.bazel b/BUILD.bazel index f1696c7..377c760 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -24,7 +24,6 @@ bazel_lint_test( srcs = [ "BUILD.bazel", "MODULE.bazel", - "WORKSPACE.bzlmod", ], ) diff --git a/MODULE.bazel b/MODULE.bazel index d43fa0a..855631d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -49,3 +49,9 @@ http_file( ] ], ) + +buildifier_repositories = use_extension("//tools:buildifier_repositories.bzl", "buildifier_repositories") +use_repo(buildifier_repositories, "buildifier-darwin-amd64") +use_repo(buildifier_repositories, "buildifier-darwin-arm64") +use_repo(buildifier_repositories, "buildifier-linux-amd64") +use_repo(buildifier_repositories, "buildifier-linux-arm64") diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod deleted file mode 100644 index f4e2f91..0000000 --- a/WORKSPACE.bzlmod +++ /dev/null @@ -1,21 +0,0 @@ -# -*- bazel -*- -# SPDX-License-Identifier: BSD-2-Clause - -# This file provides dependencies that are not yet resolve-able via the bzlmod -# module system. TODO(jwnimmer-tri) move this into MODULE.bazel as possible. - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") -load("//tools:workspace_versions.bzl", "WORKSPACE_VERSIONS") - -[ - http_file( - name = name, - executable = True, - sha256 = sha256, - url = "https://github.com/bazelbuild/buildtools/releases/download/v{}/{}".format( - WORKSPACE_VERSIONS["buildifier"]["version"], - name, - ), - ) - for name, sha256 in WORKSPACE_VERSIONS["buildifier"]["binaries"].items() -] diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index 1f96193..f38b034 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -84,8 +84,9 @@ bazel_lint_test( name = "bazel_lint_test", srcs = [ "BUILD.bazel", + "buildifier_repositories.bzl", + "buildifier_version.bzl", "defs.bzl", - "workspace_versions.bzl", ], ) diff --git a/tools/buildifier_repositories.bzl b/tools/buildifier_repositories.bzl new file mode 100644 index 0000000..2223c27 --- /dev/null +++ b/tools/buildifier_repositories.bzl @@ -0,0 +1,21 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") +load("//tools:buildifier_version.bzl", "BUILDIFIER_VERSION") + +def _impl(_ctx): + releases = "https://github.com/bazelbuild/buildtools/releases" + version = BUILDIFIER_VERSION["version"] + for name, sha256 in BUILDIFIER_VERSION["binaries"].items(): + http_file( + name = name, + executable = True, + sha256 = sha256, + url = "{releases}/download/v{version}/{name}".format( + releases = releases, + version = version, + name = name, + ), + ) + +buildifier_repositories = module_extension( + implementation = _impl, +) diff --git a/tools/buildifier_version.bzl b/tools/buildifier_version.bzl new file mode 100644 index 0000000..6218ca7 --- /dev/null +++ b/tools/buildifier_version.bzl @@ -0,0 +1,9 @@ +BUILDIFIER_VERSION = { + "version": "7.3.1", + "binaries": { + "buildifier-darwin-amd64": "375f823103d01620aaec20a0c29c6cbca99f4fd0725ae30b93655c6704f44d71", + "buildifier-darwin-arm64": "5a6afc6ac7a09f5455ba0b89bd99d5ae23b4174dc5dc9d6c0ed5ce8caac3f813", + "buildifier-linux-amd64": "5474cc5128a74e806783d54081f581662c4be8ae65022f557e9281ed5dc88009", + "buildifier-linux-arm64": "0bf86c4bfffaf4f08eed77bde5b2082e4ae5039a11e2e8b03984c173c34a561c", + }, +} diff --git a/tools/upgrade.sh b/tools/upgrade.sh index 292d545..25f4bbb 100755 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -13,7 +13,7 @@ me=$(python3 -c 'import os; print(os.path.realpath("'"$0"'"))') cd $(dirname "$me")/.. python3 -B ./tools/upgrade_helper.py -./bazel run //tools:buildifier tools/workspace_versions.bzl +./bazel run //tools:buildifier tools/buildifier_version.bzl ./bazel run //:requirements.update -- --upgrade ./bazel run //examples:requirements.update -- --upgrade diff --git a/tools/upgrade_helper.py b/tools/upgrade_helper.py index fc8fd34..f8ab4ad 100755 --- a/tools/upgrade_helper.py +++ b/tools/upgrade_helper.py @@ -34,36 +34,22 @@ def _write_bazelisk_version(new, sha256): f.write(new_content) -def _get_current_bazel_version() -> str: - """Parses ``.bazelversion`` for the old version of bazel.""" - with open(".bazelversion", encoding="utf-8") as f: - lines = f.read().splitlines() - (line,) = lines - return line.strip() - - -def _write_bazel_version(new): - """Overwrites ``.bazelversion`` with the new version of bazel.""" - with open(".bazelversion", "w", encoding="utf-8") as f: - f.write(f"{new}\n") - - -def _get_current_workspace_versions(): - """Parses ``workspace_versions.bzl`` for the old versions.""" - with open("tools/workspace_versions.bzl", encoding="utf-8") as f: +def _get_current_buildifier_version(): + """Parses ``buildifier_version.bzl`` for the old version.""" + with open("tools/buildifier_version.bzl", encoding="utf-8") as f: content = f.read() - prefix = "WORKSPACE_VERSIONS = " + prefix = "BUILDIFIER_VERSION = " assert content.startswith(prefix) return ast.literal_eval(content.removeprefix(prefix)) -def _write_workspace_versions(new): - """Overwrites ``workspace_versions.bzl`` with the new versions. +def _write_buildifier_version(new): + """Overwrites ``buildifier_version.bzl`` with the new version. We assume that tools/update.sh will run buildifier formatting afterwards. """ - prefix = "WORKSPACE_VERSIONS = " + prefix = "BUILDIFIER_VERSION = " content = prefix + pformat(new, width=1, sort_dicts=False) - with open("tools/workspace_versions.bzl", "w", encoding="utf-8") as f: + with open("tools/buildifier_version.bzl", "w", encoding="utf-8") as f: f.write(content) @@ -111,39 +97,27 @@ def _upgrade_bazelisk(): _write_bazelisk_version(new, sha256) -def _upgrade_bazel(): - """Upgrades bazel to its latest version (if necessary).""" - old = _get_current_bazel_version() - new = _find_latest_github_release("bazelbuild/bazel") - if new == old: - print(f"bazel is already at the latest version {new}") - return - print(f"bazel will be upgraded to version {new}") - _write_bazel_version(new) - - def _upgrade_buildifier(): """Upgrades buildifier to its latest version (if necessary).""" - workspace_versions = _get_current_workspace_versions() - old = workspace_versions["buildifier"]["version"] + buildifier_version = _get_current_buildifier_version() + old = buildifier_version["version"] new = _find_latest_github_release("bazelbuild/buildtools") if new == old: print(f"buildifier is already at the latest version {new}") return print(f"buildifier will be upgraded to version {new}") - workspace_versions["buildifier"]["version"] = new - names = list(workspace_versions["buildifier"]["binaries"].keys()) + buildifier_version["version"] = new + names = list(buildifier_version["binaries"].keys()) releases = "https://github.com/bazelbuild/buildtools/releases" for name in names: - workspace_versions["buildifier"]["binaries"][name] = _get_url_checksum( + buildifier_version["binaries"][name] = _get_url_checksum( f"{releases}/download/v{new}/{name}" ) - _write_workspace_versions(workspace_versions) + _write_buildifier_version(buildifier_version) def _main(): _upgrade_bazelisk() - _upgrade_bazel() _upgrade_buildifier() diff --git a/tools/workspace_versions.bzl b/tools/workspace_versions.bzl deleted file mode 100644 index 7fe708f..0000000 --- a/tools/workspace_versions.bzl +++ /dev/null @@ -1,11 +0,0 @@ -WORKSPACE_VERSIONS = { - "buildifier": { - "version": "7.3.1", - "binaries": { - "buildifier-darwin-amd64": "375f823103d01620aaec20a0c29c6cbca99f4fd0725ae30b93655c6704f44d71", - "buildifier-darwin-arm64": "5a6afc6ac7a09f5455ba0b89bd99d5ae23b4174dc5dc9d6c0ed5ce8caac3f813", - "buildifier-linux-amd64": "5474cc5128a74e806783d54081f581662c4be8ae65022f557e9281ed5dc88009", - "buildifier-linux-arm64": "0bf86c4bfffaf4f08eed77bde5b2082e4ae5039a11e2e8b03984c173c34a561c", - }, - }, -}