Skip to content

Commit b265c47

Browse files
committed
[cargo-bazel] Avoid regenerating cargo lockfile when splicing if not needed
1 parent 0f8b797 commit b265c47

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

crate_universe/extensions.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def _generate_hub_and_spokes(
645645
config_path = config_file,
646646
output_dir = tag_path.get_child("splicing-output"),
647647
debug_workspace_dir = tag_path.get_child("splicing-workspace"),
648+
skip_cargo_lockfile_overwrite = cfg.skip_cargo_lockfile_overwrite,
648649
repository_name = cfg.name,
649650
)
650651

crate_universe/private/crates_repository.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def _crates_repository_impl(repository_ctx):
9191
splicing_manifest = splicing_manifest,
9292
config_path = config_path,
9393
output_dir = repository_ctx.path("splicing-output"),
94+
skip_cargo_lockfile_overwrite = repository_ctx.attr.skip_cargo_lockfile_overwrite,
9495
repository_name = repository_ctx.name,
9596
)
9697

crate_universe/private/generate_utils.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,7 @@ def execute_generator(
497497
])
498498

499499
if skip_cargo_lockfile_overwrite:
500-
args.extend([
501-
"--skip-cargo-lockfile-overwrite",
502-
])
500+
args.append("--skip-cargo-lockfile-overwrite")
503501

504502
# Some components are not required unless re-pinning is enabled
505503
if metadata:

crate_universe/private/splicing_utils.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def splice_workspace_manifest(
123123
config_path,
124124
output_dir,
125125
repository_name,
126+
skip_cargo_lockfile_overwrite,
126127
debug_workspace_dir = None):
127128
"""Splice together a Cargo workspace from various other manifests and package definitions
128129
@@ -134,6 +135,9 @@ def splice_workspace_manifest(
134135
config_path (path): The path to the config file (containing `cargo_bazel::config::Config`.)
135136
output_dir (path): THe location in which to write splicing outputs.
136137
repository_name (str): Name of the repository being generated.
138+
skip_cargo_lockfile_overwrite (bool): Whether to skip writing the cargo lockfile back after resolving.
139+
You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
140+
But you probably don't want to set this.
137141
debug_workspace_dir (path): The location in which to save splicing outputs for future review.
138142
139143
Returns:
@@ -159,6 +163,9 @@ def splice_workspace_manifest(
159163
cargo_lockfile,
160164
])
161165

166+
if skip_cargo_lockfile_overwrite:
167+
arguments.append("--skip-cargo-lockfile-overwrite")
168+
162169
# Optionally set the splicing workspace directory to somewhere within the repository directory
163170
# to improve the debugging experience.
164171
if CARGO_BAZEL_DEBUG in repository_ctx.os.environ:

crate_universe/src/cli/splice.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ pub struct SpliceOptions {
6464
/// The name of the repository being generated.
6565
#[clap(long)]
6666
pub repository_name: String,
67+
68+
/// Whether to skip writing the cargo lockfile back after resolving.
69+
/// You may want to set this if your dependency versions are maintained externally through a non-trivial set-up.
70+
/// But you probably don't want to set this.
71+
#[clap(long)]
72+
pub skip_cargo_lockfile_overwrite: bool,
6773
}
6874

6975
/// Combine a set of disjoint manifests into a single workspace.
@@ -94,14 +100,22 @@ pub fn splice(opt: SpliceOptions) -> Result<()> {
94100
.splice(&splicing_dir)
95101
.with_context(|| format!("Failed to splice workspace {}", opt.repository_name))?;
96102

97-
// Generate a lockfile
98-
let cargo_lockfile = generate_lockfile(
99-
&manifest_path,
100-
&opt.cargo_lockfile,
101-
cargo.clone(),
102-
&opt.repin,
103-
)
104-
.context("Failed to generate lockfile")?;
103+
// Use the existing lockfile if possible, otherwise generate a new one.
104+
let cargo_lockfile = if opt.cargo_lockfile.is_some() && opt.skip_cargo_lockfile_overwrite {
105+
let cargo_lockfile_path = opt.cargo_lockfile.unwrap();
106+
cargo_lock::Lockfile::load(&cargo_lockfile_path).context(format!(
107+
"Failed to load lockfile: {}",
108+
cargo_lockfile_path.display()
109+
))?
110+
} else {
111+
generate_lockfile(
112+
&manifest_path,
113+
&opt.cargo_lockfile,
114+
cargo.clone(),
115+
&opt.repin,
116+
)
117+
.context("Failed to generate lockfile")?
118+
};
105119

106120
let config = Config::try_from_path(&opt.config).context("Failed to parse config")?;
107121

crate_universe/tests/cargo_integration_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ fn run(repository_name: &str, manifests: HashMap<String, String>, lockfile: &str
117117
cargo,
118118
rustc,
119119
repository_name: String::from("crates_index"),
120+
skip_cargo_lockfile_overwrite: false,
120121
})
121122
.unwrap();
122123

0 commit comments

Comments
 (0)