From 1fb802cf55111126b4a22256958d0512e1d1a2a0 Mon Sep 17 00:00:00 2001 From: Maximilian Goisser Date: Wed, 25 Jan 2023 19:15:15 +0100 Subject: [PATCH 1/2] Add `CARGO_BAZEL_REPIN_ONLY` repinning allowlist --- crate_universe/private/common_utils.bzl | 4 ++++ crate_universe/private/crates_repository.bzl | 10 ++++++++++ crate_universe/private/generate_utils.bzl | 12 ++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crate_universe/private/common_utils.bzl b/crate_universe/private/common_utils.bzl index 02b9447d14..22d27b5a92 100644 --- a/crate_universe/private/common_utils.bzl +++ b/crate_universe/private/common_utils.bzl @@ -10,11 +10,15 @@ CARGO_BAZEL_ISOLATED = "CARGO_BAZEL_ISOLATED" CARGO_BAZEL_REPIN = "CARGO_BAZEL_REPIN" REPIN = "REPIN" +CARGO_BAZEL_REPIN_ONLY = "CARGO_BAZEL_REPIN_ONLY" + REPIN_ENV_VARS = [ CARGO_BAZEL_REPIN, REPIN, ] +REPIN_ALLOWLIST_ENV_VAR = CARGO_BAZEL_REPIN_ONLY + _EXECUTE_ERROR_MESSAGE = """\ Command {args} failed with exit code {exit_code}. STDOUT ------------------------------------------------------------------------ diff --git a/crate_universe/private/crates_repository.bzl b/crate_universe/private/crates_repository.bzl index 6300cde056..f431779dd1 100644 --- a/crate_universe/private/crates_repository.bzl +++ b/crate_universe/private/crates_repository.bzl @@ -116,6 +116,7 @@ Environment Variables: | `CARGO_BAZEL_GENERATOR_URL` | The URL of a cargo-bazel binary. This variable takes precedence over attributes and can use `file://` for local paths | | `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration | | `CARGO_BAZEL_REPIN` | An indicator that the dependencies represented by the rule should be regenerated. `REPIN` may also be used. See [Repinning / Updating Dependencies](#crates_repository_repinning_updating_dependencies) for more details. | +| `CARGO_BAZEL_REPIN_ONLY` | A comma-delimited allowlist for rules to execute repinning. Can be useful if multiple instances of the repository rule are used in a Bazel workspace, but repinning should be limited to one of them. | Example: @@ -185,6 +186,15 @@ that is called behind the scenes to update dependencies. | `package_name` | `cargo upgrade --package package_name` | | `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` | +If the `crates_repository` is used multiple times in the same Bazel workspace (e.g. for multiple independent +Rust workspaces), it may additionally be useful to use the `CARGO_BAZEL_REPIN_ONLY` environment variable, which +limits execution of the repinning to one or multiple instances of the `crates_repository` rule via a comma-delimited +allowlist: + +```shell +CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_index +``` + """, implementation = _crates_repository_impl, attrs = { diff --git a/crate_universe/private/generate_utils.bzl b/crate_universe/private/generate_utils.bzl index 5c891580b5..988f25374d 100644 --- a/crate_universe/private/generate_utils.bzl +++ b/crate_universe/private/generate_utils.bzl @@ -1,6 +1,6 @@ """Utilities directly related to the `generate` step of `cargo-bazel`.""" -load(":common_utils.bzl", "CARGO_BAZEL_ISOLATED", "REPIN_ENV_VARS", "cargo_environ", "execute") +load(":common_utils.bzl", "CARGO_BAZEL_ISOLATED", "REPIN_ALLOWLIST_ENV_VAR", "REPIN_ENV_VARS", "cargo_environ", "execute") CARGO_BAZEL_GENERATOR_SHA256 = "CARGO_BAZEL_GENERATOR_SHA256" CARGO_BAZEL_GENERATOR_URL = "CARGO_BAZEL_GENERATOR_URL" @@ -11,6 +11,8 @@ GENERATOR_ENV_VARS = [ ] CRATES_REPOSITORY_ENVIRON = GENERATOR_ENV_VARS + REPIN_ENV_VARS + [ + REPIN_ALLOWLIST_ENV_VAR, +] + [ CARGO_BAZEL_ISOLATED, ] @@ -330,7 +332,13 @@ def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_m # If a repin environment variable is set, always repin for var in REPIN_ENV_VARS: if var in repository_ctx.os.environ and repository_ctx.os.environ[var].lower() not in ["false", "no", "0", "off"]: - return True + # If a repin allowlist is present only force repin if name is in list + if REPIN_ALLOWLIST_ENV_VAR in repository_ctx.os.environ: + indices_to_repin = repository_ctx.os.environ[REPIN_ALLOWLIST_ENV_VAR].split(",") + if repository_ctx.name in indices_to_repin: + return True + else: + return True # If a deterministic lockfile was not added then always repin if not lockfile_path: From c847eed379605786c9340518983fdd8e8c1c9eb4 Mon Sep 17 00:00:00 2001 From: Maximilian Goisser Date: Wed, 25 Jan 2023 19:49:37 +0100 Subject: [PATCH 2/2] Regenerate documentation --- docs/crate_universe.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/crate_universe.md b/docs/crate_universe.md index 1fb7acca47..04f78dd7ac 100644 --- a/docs/crate_universe.md +++ b/docs/crate_universe.md @@ -282,6 +282,7 @@ Environment Variables: | `CARGO_BAZEL_GENERATOR_URL` | The URL of a cargo-bazel binary. This variable takes precedence over attributes and can use `file://` for local paths | | `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration | | `CARGO_BAZEL_REPIN` | An indicator that the dependencies represented by the rule should be regenerated. `REPIN` may also be used. See [Repinning / Updating Dependencies](#crates_repository_repinning_updating_dependencies) for more details. | +| `CARGO_BAZEL_REPIN_ONLY` | A comma-delimited allowlist for rules to execute repinning. Can be useful if multiple instances of the repository rule are used in a Bazel workspace, but repinning should be limited to one of them. | Example: @@ -351,6 +352,15 @@ that is called behind the scenes to update dependencies. | `package_name` | `cargo upgrade --package package_name` | | `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` | +If the `crates_repository` is used multiple times in the same Bazel workspace (e.g. for multiple independent +Rust workspaces), it may additionally be useful to use the `CARGO_BAZEL_REPIN_ONLY` environment variable, which +limits execution of the repinning to one or multiple instances of the `crates_repository` rule via a comma-delimited +allowlist: + +```shell +CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_index +``` + **ATTRIBUTES**