Skip to content

Commit 4a530aa

Browse files
feat(crates_repository): customizable repin instructions (#2756)
Add customizable instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default. This follows the logic of bazel-contrib/rules_jvm_external#1000 --------- Co-authored-by: Daniel Wagner-Hall <[email protected]>
1 parent 6b10ce3 commit 4a530aa

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

crate_universe/private/crates_repository.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def _crates_repository_impl(repository_ctx):
5050
splicing_manifest = splicing_manifest,
5151
cargo = cargo_path,
5252
rustc = rustc_path,
53+
repin_instructions = repository_ctx.attr.repin_instructions,
5354
)
5455

5556
# If re-pinning is enabled, gather additional inputs for the generator
@@ -281,6 +282,9 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
281282
"generate the value for this field. If unset, the defaults defined there will be used."
282283
),
283284
),
285+
"repin_instructions": attr.string(
286+
doc = "Instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default.",
287+
),
284288
"rust_toolchain_cargo_template": attr.string(
285289
doc = (
286290
"The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " +

crate_universe/private/generate_utils.bzl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def get_lockfiles(repository_ctx):
336336
bazel = repository_ctx.path(repository_ctx.attr.lockfile) if repository_ctx.attr.lockfile else None,
337337
)
338338

339-
def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_manifest, cargo, rustc):
339+
def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_manifest, cargo, rustc, repin_instructions = None):
340340
"""Use the `cargo-bazel` binary to determine whether or not dpeendencies need to be re-pinned
341341
342342
Args:
@@ -347,6 +347,7 @@ def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_m
347347
lockfile_path (path): The path to a "lock" file for reproducible outputs.
348348
cargo (path): The path to a Cargo binary.
349349
rustc (path): The path to a Rustc binary.
350+
repin_instructions (optional string): Instructions to re-pin dependencies in your repository. Will be shown when re-pinning is required.
350351
351352
Returns:
352353
bool: True if dependencies need to be re-pinned
@@ -403,14 +404,22 @@ def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_m
403404
# flag indicating repinning was requested, an error is raised
404405
# since repinning should be an explicit action
405406
if result.return_code:
406-
fail(("\n".join([
407-
result.stderr,
408-
(
409-
"The current `lockfile` is out of date for '{}'. Please re-run " +
410-
"bazel using `CARGO_BAZEL_REPIN=true` if this is expected " +
411-
"and the lockfile should be updated."
412-
).format(repository_ctx.name),
413-
])))
407+
if repin_instructions:
408+
msg = ("\n".join([
409+
result.stderr,
410+
"The current `lockfile` is out of date for '{}'.".format(repository_ctx.name),
411+
repin_instructions,
412+
]))
413+
else:
414+
msg = ("\n".join([
415+
result.stderr,
416+
(
417+
"The current `lockfile` is out of date for '{}'. Please re-run " +
418+
"bazel using `CARGO_BAZEL_REPIN=true` if this is expected " +
419+
"and the lockfile should be updated."
420+
).format(repository_ctx.name),
421+
]))
422+
fail(msg)
414423

415424
return False
416425

docs/crate_universe.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ str: A json encoded string of the parameters provided
752752
crates_repository(<a href="#crates_repository-name">name</a>, <a href="#crates_repository-annotations">annotations</a>, <a href="#crates_repository-cargo_config">cargo_config</a>, <a href="#crates_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#crates_repository-generate_binaries">generate_binaries</a>,
753753
<a href="#crates_repository-generate_build_scripts">generate_build_scripts</a>, <a href="#crates_repository-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#crates_repository-generator">generator</a>,
754754
<a href="#crates_repository-generator_sha256s">generator_sha256s</a>, <a href="#crates_repository-generator_urls">generator_urls</a>, <a href="#crates_repository-isolated">isolated</a>, <a href="#crates_repository-lockfile">lockfile</a>, <a href="#crates_repository-manifests">manifests</a>, <a href="#crates_repository-packages">packages</a>, <a href="#crates_repository-quiet">quiet</a>,
755-
<a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
755+
<a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repin_instructions">repin_instructions</a>, <a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
756756
<a href="#crates_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#crates_repository-rust_version">rust_version</a>, <a href="#crates_repository-splicing_config">splicing_config</a>,
757757
<a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>)
758758
</pre>
@@ -867,6 +867,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
867867
| <a id="crates_repository-packages"></a>packages | A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
868868
| <a id="crates_repository-quiet"></a>quiet | If stdout and stderr should not be printed to the terminal. | Boolean | optional | `True` |
869869
| <a id="crates_repository-render_config"></a>render_config | The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\%render_config` to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | `""` |
870+
| <a id="crates_repository-repin_instructions"></a>repin_instructions | Instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default. | String | optional | `""` |
870871
| <a id="crates_repository-repo_mapping"></a>repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
871872
| <a id="crates_repository-rust_toolchain_cargo_template"></a>rust_toolchain_cargo_template | The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present. | String | optional | `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"` |
872873
| <a id="crates_repository-rust_toolchain_rustc_template"></a>rust_toolchain_rustc_template | The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'cargo.exe') will be replaced in the string if present. | String | optional | `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"` |

0 commit comments

Comments
 (0)