From 99c82f7a92a6c09f343bb6dbdef6ddeaec499616 Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Mon, 19 Aug 2024 23:12:01 -0700 Subject: [PATCH] fix cycle when loading externals Change-Id: Ie2978cc5e832d08fee2f53608e26d31e09a76e79 --- example/WORKSPACE.bazel | 4 +++ private/local_workspace_status.bzl | 47 ++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/example/WORKSPACE.bazel b/example/WORKSPACE.bazel index c379d96..e4e7d6c 100644 --- a/example/WORKSPACE.bazel +++ b/example/WORKSPACE.bazel @@ -2,3 +2,7 @@ local_repository( name = "rules_clang_tidy", path = "..", ) + +load("@rules_clang_tidy//:dependencies.bzl", "rules_clang_tidy_dependencies") + +rules_clang_tidy_dependencies() diff --git a/private/local_workspace_status.bzl b/private/local_workspace_status.bzl index 9bc7de0..882a234 100644 --- a/private/local_workspace_status.bzl +++ b/private/local_workspace_status.bzl @@ -2,7 +2,50 @@ Determines the workspace status used as an input to `apply_fixes` rules. """ -load("@bazel_skylib//lib:versions.bzl", "versions") +# we can't load @bazel_skylib yet since this bzl file is loaded in +# dependencies.bzl (which is responsible for loading @bazel_skylib) + +# https://github.com/bazelbuild/bazel-skylib/blob/652c8f0b2817daaa2570b7a3b2147643210f7dc7/lib/versions.bzl + +def _extract_version_number(bazel_version): + """Extracts the semantic version number from a version string + + Args: + bazel_version: the version string that begins with the semantic version + e.g. "1.2.3rc1 abc1234" where "abc1234" is a commit hash. + + Returns: + The semantic version string, like "1.2.3". + """ + for i in range(len(bazel_version)): + c = bazel_version[i] + if not (c.isdigit() or c == "."): + return bazel_version[:i] + return bazel_version + +# Parse the bazel version string from `native.bazel_version`. +# e.g. +# "0.10.0rc1 abc123d" => (0, 10, 0) +# "0.3.0" => (0, 3, 0) +def _parse_bazel_version(bazel_version): + """Parses a version string into a 3-tuple of ints + + int tuples can be compared directly using binary operators (<, >). + + For a development build of Bazel, this returns an unspecified version tuple + that compares higher than any released version. + + Args: + bazel_version: the Bazel version string + + Returns: + An int 3-tuple of a (major, minor, patch) version. + """ + + version = _extract_version_number(bazel_version) + if not version: + return (999999, 999999, 999999) + return tuple([int(n) for n in version.split(".")]) def _setup_status_file(rctx): """ @@ -83,7 +126,7 @@ BUILD_WORKSPACE_DIRECTORY = "{workspace_root}" executable = False, ) - if versions.is_at_least("7.1.0", native.bazel_version): + if _parse_bazel_version(native.bazel_version) >= _parse_bazel_version("7.1.0"): _setup_status_file(rctx) else: rctx.file(