Skip to content

Commit 3529a4e

Browse files
authored
Make Cargo.lock overwrite optional (#3526)
Progress towards #3522
1 parent bb32364 commit 3529a4e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

crate_universe/extensions.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ def _generate_hub_and_spokes(
558558
render_config,
559559
splicing_config,
560560
lockfile,
561+
skip_cargo_lockfile_overwrite,
561562
cargo_lockfile = None,
562563
manifests = {},
563564
packages = {}):
@@ -571,6 +572,9 @@ def _generate_hub_and_spokes(
571572
render_config (dict): The render config to use.
572573
splicing_config (dict): The splicing config to use.
573574
lockfile (path): The path to the crate_universe lock file, if one was provided.
575+
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
576+
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
577+
But you probably don't want to set this.
574578
cargo_lockfile (path): Path to Cargo.lock, if we have one.
575579
manifests (dict): The set of Cargo.toml manifests that apply to this closure, if any, keyed by path.
576580
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(
672676
nonhermetic_root_bazel_workspace_dir = nonhermetic_root_bazel_workspace_dir,
673677
paths_to_track_file = paths_to_track_file,
674678
warnings_output_file = warnings_output_file,
679+
skip_cargo_lockfile_overwrite = skip_cargo_lockfile_overwrite,
675680
**kwargs
676681
)
677682

@@ -1018,6 +1023,7 @@ def _crate_impl(module_ctx):
10181023
splicing_config = splicing_config,
10191024
manifests = manifests,
10201025
packages = packages,
1026+
skip_cargo_lockfile_overwrite = cfg.skip_cargo_lockfile_overwrite,
10211027
)
10221028

10231029
metadata_kwargs = {}
@@ -1051,6 +1057,14 @@ _FROM_COMMON_ATTRS = {
10511057
"If set, this file must exist within the workspace (but can be empty) before this rule will work."
10521058
),
10531059
),
1060+
"skip_cargo_lockfile_overwrite": attr.bool(
1061+
doc = (
1062+
"Whether to skip writing the cargo lockfile back after resolving. " +
1063+
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
1064+
"But you probably don't want to set this."
1065+
),
1066+
default = False,
1067+
),
10541068
"supported_platform_triples": attr.string_list(
10551069
doc = "A set of all platform triples to consider when generating dependencies.",
10561070
default = SUPPORTED_PLATFORM_TRIPLES,

crate_universe/private/crates_repository.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def _crates_repository_impl(repository_ctx):
112112
nonhermetic_root_bazel_workspace_dir = repository_ctx.workspace_root,
113113
paths_to_track_file = paths_to_track_file,
114114
warnings_output_file = warnings_output_file,
115+
skip_cargo_lockfile_overwrite = repository_ctx.attr.skip_cargo_lockfile_overwrite,
115116
# sysroot = tools.sysroot,
116117
**kwargs
117118
)
@@ -354,6 +355,14 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
354355
doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`",
355356
default = rust_common.default_version,
356357
),
358+
"skip_cargo_lockfile_overwrite": attr.bool(
359+
doc = (
360+
"Whether to skip writing the cargo lockfile back after resolving. " +
361+
"You may want to set this if your dependency versions are maintained externally through a non-trivial set-up. " +
362+
"But you probably don't want to set this."
363+
),
364+
default = False,
365+
),
357366
"splicing_config": attr.string(
358367
doc = (
359368
"The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " +

crate_universe/private/generate_utils.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ def execute_generator(
438438
nonhermetic_root_bazel_workspace_dir,
439439
paths_to_track_file,
440440
warnings_output_file,
441+
skip_cargo_lockfile_overwrite,
441442
metadata = None,
442443
generator_label = None):
443444
"""Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files.
@@ -452,6 +453,9 @@ def execute_generator(
452453
nonhermetic_root_bazel_workspace_dir (path): The path to the current workspace root
453454
paths_to_track_file (path): Path to file where generator should write which files should trigger re-generating as a JSON list.
454455
warnings_output_file (path): Path to file where generator should write warnings to print.
456+
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
457+
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
458+
But you probably don't want to set this.
455459
generator_label (Label): The label of the `generator` parameter.
456460
metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to
457461
the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and
@@ -490,6 +494,11 @@ def execute_generator(
490494
lockfile_path,
491495
])
492496

497+
if skip_cargo_lockfile_overwrite:
498+
args.extend([
499+
"--skip-cargo-lockfile-overwrite",
500+
])
501+
493502
# Some components are not required unless re-pinning is enabled
494503
if metadata:
495504
args.extend([

crate_universe/src/cli/generate.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ pub struct GenerateOptions {
9696
/// so this provides a way for the repository rule to force printing.
9797
#[clap(long)]
9898
pub warnings_output_path: PathBuf,
99+
100+
/// Whether to skip writing the cargo lockfile back after resolving.
101+
/// You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
102+
/// But you probably don't want to set this.
103+
#[clap(long)]
104+
pub skip_cargo_lockfile_overwrite: bool,
99105
}
100106

101107
pub fn generate(opt: GenerateOptions) -> Result<()> {
@@ -213,7 +219,9 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
213219
write_lockfile(lock_content, &lockfile, opt.dry_run)?;
214220
}
215221

216-
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;
222+
if !opt.skip_cargo_lockfile_overwrite {
223+
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;
224+
}
217225

218226
Ok(())
219227
}

0 commit comments

Comments
 (0)