diff --git a/crate_universe/extensions.bzl b/crate_universe/extensions.bzl index f2ba17f580..e6f1348e7f 100644 --- a/crate_universe/extensions.bzl +++ b/crate_universe/extensions.bzl @@ -558,6 +558,7 @@ def _generate_hub_and_spokes( render_config, splicing_config, lockfile, + skip_cargo_lockfile_overwrite, cargo_lockfile = None, manifests = {}, packages = {}): @@ -571,6 +572,9 @@ def _generate_hub_and_spokes( render_config (dict): The render config to use. splicing_config (dict): The splicing config to use. lockfile (path): The path to the crate_universe lock file, if one was provided. + skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving. + You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. + But you probably don't want to set this. cargo_lockfile (path): Path to Cargo.lock, if we have one. manifests (dict): The set of Cargo.toml manifests that apply to this closure, if any, keyed by path. packages (dict): The set of extra cargo crate tags that apply to this closure, if any, keyed by package name. @@ -672,6 +676,7 @@ def _generate_hub_and_spokes( nonhermetic_root_bazel_workspace_dir = nonhermetic_root_bazel_workspace_dir, paths_to_track_file = paths_to_track_file, warnings_output_file = warnings_output_file, + skip_cargo_lockfile_overwrite = skip_cargo_lockfile_overwrite, **kwargs ) @@ -1018,6 +1023,7 @@ def _crate_impl(module_ctx): splicing_config = splicing_config, manifests = manifests, packages = packages, + skip_cargo_lockfile_overwrite = cfg.skip_cargo_lockfile_overwrite, ) metadata_kwargs = {} @@ -1051,6 +1057,14 @@ _FROM_COMMON_ATTRS = { "If set, this file must exist within the workspace (but can be empty) before this rule will work." ), ), + "skip_cargo_lockfile_overwrite": attr.bool( + doc = ( + "Whether to skip writing the cargo lockfile back after resolving. " + + "You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " + + "But you probably don't want to set this." + ), + default = False, + ), "supported_platform_triples": attr.string_list( doc = "A set of all platform triples to consider when generating dependencies.", default = SUPPORTED_PLATFORM_TRIPLES, diff --git a/crate_universe/private/crates_repository.bzl b/crate_universe/private/crates_repository.bzl index c3a29619bf..58d887cf43 100644 --- a/crate_universe/private/crates_repository.bzl +++ b/crate_universe/private/crates_repository.bzl @@ -112,6 +112,7 @@ def _crates_repository_impl(repository_ctx): nonhermetic_root_bazel_workspace_dir = repository_ctx.workspace_root, paths_to_track_file = paths_to_track_file, warnings_output_file = warnings_output_file, + skip_cargo_lockfile_overwrite = repository_ctx.attr.skip_cargo_lockfile_overwrite, # sysroot = tools.sysroot, **kwargs ) @@ -354,6 +355,14 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`", default = rust_common.default_version, ), + "skip_cargo_lockfile_overwrite": attr.bool( + doc = ( + "Whether to skip writing the cargo lockfile back after resolving. " + + "You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " + + "But you probably don't want to set this." + ), + default = False, + ), "splicing_config": attr.string( doc = ( "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " + diff --git a/crate_universe/private/generate_utils.bzl b/crate_universe/private/generate_utils.bzl index 54517fb0d1..15b94fa512 100644 --- a/crate_universe/private/generate_utils.bzl +++ b/crate_universe/private/generate_utils.bzl @@ -438,6 +438,7 @@ def execute_generator( nonhermetic_root_bazel_workspace_dir, paths_to_track_file, warnings_output_file, + skip_cargo_lockfile_overwrite, metadata = None, generator_label = None): """Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files. @@ -452,6 +453,9 @@ def execute_generator( nonhermetic_root_bazel_workspace_dir (path): The path to the current workspace root paths_to_track_file (path): Path to file where generator should write which files should trigger re-generating as a JSON list. warnings_output_file (path): Path to file where generator should write warnings to print. + skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving. + You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. + But you probably don't want to set this. generator_label (Label): The label of the `generator` parameter. metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and @@ -490,6 +494,11 @@ def execute_generator( lockfile_path, ]) + if skip_cargo_lockfile_overwrite: + args.extend([ + "--skip-cargo-lockfile-overwrite", + ]) + # Some components are not required unless re-pinning is enabled if metadata: args.extend([ diff --git a/crate_universe/src/cli/generate.rs b/crate_universe/src/cli/generate.rs index 1769683fc0..70324a911c 100644 --- a/crate_universe/src/cli/generate.rs +++ b/crate_universe/src/cli/generate.rs @@ -96,6 +96,12 @@ pub struct GenerateOptions { /// so this provides a way for the repository rule to force printing. #[clap(long)] pub warnings_output_path: PathBuf, + + /// Whether to skip writing the cargo lockfile back after resolving. + /// You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. + /// But you probably don't want to set this. + #[clap(long)] + pub skip_cargo_lockfile_overwrite: bool, } pub fn generate(opt: GenerateOptions) -> Result<()> { @@ -213,7 +219,9 @@ pub fn generate(opt: GenerateOptions) -> Result<()> { write_lockfile(lock_content, &lockfile, opt.dry_run)?; } - update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?; + if !opt.skip_cargo_lockfile_overwrite { + update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?; + } Ok(()) }