Skip to content

Commit 9891268

Browse files
committed
Introduce strip_internal_dependencies_from_cargo_lockfile where required
1 parent b11d64a commit 9891268

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

crate_universe/extensions.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ def _generate_hub_and_spokes(
559559
splicing_config,
560560
lockfile,
561561
skip_cargo_lockfile_overwrite,
562+
strip_internal_dependencies_from_cargo_lockfile,
562563
cargo_lockfile = None,
563564
manifests = {},
564565
packages = {}):
@@ -575,6 +576,11 @@ def _generate_hub_and_spokes(
575576
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
576577
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
577578
But you probably don't want to set this.
579+
strip_internal_dependencies_from_cargo_lockfile (bool): Whether to strip internal dependencies from the cargo lockfile.
580+
You may want to use this if you want to maintain a cargo lockfile for bazel only.
581+
Bazel only requires external dependencies to be present in the lockfile.
582+
By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts
583+
in other lockfiles where the cargo lockfile's sha is stored.
578584
cargo_lockfile (path): Path to Cargo.lock, if we have one.
579585
manifests (dict): The set of Cargo.toml manifests that apply to this closure, if any, keyed by path.
580586
packages (dict): The set of extra cargo crate tags that apply to this closure, if any, keyed by package name.
@@ -684,6 +690,7 @@ def _generate_hub_and_spokes(
684690
paths_to_track_file = paths_to_track_file,
685691
warnings_output_file = warnings_output_file,
686692
skip_cargo_lockfile_overwrite = skip_cargo_lockfile_overwrite,
693+
strip_internal_dependencies_from_cargo_lockfile = strip_internal_dependencies_from_cargo_lockfile,
687694
**kwargs
688695
)
689696

@@ -1169,6 +1176,7 @@ def _crate_impl(module_ctx):
11691176
manifests = manifests,
11701177
packages = packages,
11711178
skip_cargo_lockfile_overwrite = cfg.skip_cargo_lockfile_overwrite,
1179+
strip_internal_dependencies_from_cargo_lockfile = cfg.strip_internal_dependencies_from_cargo_lockfile,
11721180
)
11731181

11741182
metadata_kwargs = {}
@@ -1210,6 +1218,16 @@ _FROM_COMMON_ATTRS = {
12101218
),
12111219
default = False,
12121220
),
1221+
"strip_internal_dependencies_from_cargo_lockfile": attr.bool(
1222+
doc = (
1223+
"Whether to strip internal dependencies from the cargo lockfile. " +
1224+
"You may want to use this if you want to maintain a cargo lockfile for bazel only. " +
1225+
"Bazel only requires external dependencies to be present in the lockfile. " +
1226+
"By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts " +
1227+
"in other lockfiles where the cargo lockfile's sha is stored."
1228+
),
1229+
default = False,
1230+
),
12131231
"supported_platform_triples": attr.string_list(
12141232
doc = "A set of all platform triples to consider when generating dependencies.",
12151233
default = SUPPORTED_PLATFORM_TRIPLES,

crate_universe/private/crates_repository.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def _crates_repository_impl(repository_ctx):
122122
paths_to_track_file = paths_to_track_file,
123123
warnings_output_file = warnings_output_file,
124124
skip_cargo_lockfile_overwrite = repository_ctx.attr.skip_cargo_lockfile_overwrite,
125+
strip_internal_dependencies_from_cargo_lockfile = repository_ctx.attr.strip_internal_dependencies_from_cargo_lockfile,
125126
# sysroot = tools.sysroot,
126127
**kwargs
127128
)
@@ -377,6 +378,16 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i
377378
"generate the value for this field. If unset, the defaults defined there will be used."
378379
),
379380
),
381+
"strip_internal_dependencies_from_cargo_lockfile": attr.bool(
382+
doc = (
383+
"Whether to strip internal dependencies from the cargo lockfile. " +
384+
"You may want to use this if you want to maintain a cargo lockfile for bazel only. " +
385+
"Bazel only requires external dependencies to be present in the lockfile. " +
386+
"By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts " +
387+
"in other lockfiles where the cargo lockfile's sha is stored."
388+
),
389+
default = False,
390+
),
380391
"supported_platform_triples": attr.string_list(
381392
doc = "A set of all platform triples to consider when generating dependencies.",
382393
default = SUPPORTED_PLATFORM_TRIPLES,

crate_universe/private/generate_utils.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ def execute_generator(
441441
paths_to_track_file,
442442
warnings_output_file,
443443
skip_cargo_lockfile_overwrite,
444+
strip_internal_dependencies_from_cargo_lockfile,
444445
metadata = None,
445446
generator_label = None):
446447
"""Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files.
@@ -458,6 +459,11 @@ def execute_generator(
458459
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
459460
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
460461
But you probably don't want to set this.
462+
strip_internal_dependencies_from_cargo_lockfile (bool): Whether to strip internal dependencies from the cargo lockfile.
463+
You may want to use this if you want to maintain a cargo lockfile for bazel only.
464+
Bazel only requires external dependencies to be present in the lockfile.
465+
By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts
466+
in other lockfiles where the cargo lockfile's sha is stored.
461467
generator_label (Label): The label of the `generator` parameter.
462468
metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to
463469
the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and
@@ -499,6 +505,9 @@ def execute_generator(
499505
if skip_cargo_lockfile_overwrite:
500506
args.append("--skip-cargo-lockfile-overwrite")
501507

508+
if strip_internal_dependencies_from_cargo_lockfile:
509+
args.append("--strip-internal-dependencies-from-cargo-lockfile")
510+
502511
# Some components are not required unless re-pinning is enabled
503512
if metadata:
504513
args.extend([

crate_universe/src/cli/generate.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ pub struct GenerateOptions {
102102
/// But you probably don't want to set this.
103103
#[clap(long)]
104104
pub skip_cargo_lockfile_overwrite: bool,
105+
106+
/// Whether to strip internal dependencies from the cargo lockfile.
107+
/// You may want to use this if you want to maintain a cargo lockfile for bazel only.
108+
/// Bazel only requires external dependencies to be present in the lockfile.
109+
/// By removing internal dependencies, the lockfile changes less frequently which reduces merge conflicts
110+
/// in other lockfiles where the cargo lockfile's sha is stored.
111+
#[clap(long)]
112+
pub strip_internal_dependencies_from_cargo_lockfile: bool,
105113
}
106114

107115
pub fn generate(opt: GenerateOptions) -> Result<()> {
@@ -222,7 +230,12 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
222230
}
223231

224232
if !opt.skip_cargo_lockfile_overwrite {
225-
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile)?;
233+
let cargo_lockfile_to_write = if opt.strip_internal_dependencies_from_cargo_lockfile {
234+
remove_internal_dependencies_from_cargo_lockfile(cargo_lockfile)
235+
} else {
236+
cargo_lockfile
237+
};
238+
update_cargo_lockfile(&opt.cargo_lockfile, cargo_lockfile_to_write)?;
226239
}
227240

228241
Ok(())

0 commit comments

Comments
 (0)