Skip to content

Commit 02c7b5e

Browse files
committed
feat: --lockfile-path add install support
1 parent 59b74f2 commit 02c7b5e

File tree

6 files changed

+85
-36
lines changed

6 files changed

+85
-36
lines changed

src/bin/cargo/commands/install.rs

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub fn cli() -> Command {
9898
.arg_target_triple("Build for the target triple")
9999
.arg_target_dir()
100100
.arg_timings()
101+
.arg_lockfile_path()
101102
.after_help(color_print::cstr!(
102103
"Run `<cyan,bold>cargo help install</>` for more detailed information.\n"
103104
))
@@ -204,6 +205,13 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
204205
if args.dry_run() {
205206
gctx.cli_unstable().fail_if_stable_opt("--dry-run", 11123)?;
206207
}
208+
209+
let requested_lockfile_path = args.lockfile_path(gctx)?;
210+
// 14421: lockfile path should imply --locked on running `install`
211+
if requested_lockfile_path.is_some() {
212+
gctx.set_locked(true);
213+
}
214+
207215
if args.flag("list") {
208216
ops::install_list(root, gctx)?;
209217
} else {
@@ -217,6 +225,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
217225
args.flag("force"),
218226
args.flag("no-track"),
219227
args.dry_run(),
228+
requested_lockfile_path.as_deref(),
220229
)?;
221230
}
222231
Ok(())

src/cargo/core/workspace.rs

+4
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,10 @@ impl<'gctx> Workspace<'gctx> {
662662
self.requested_lockfile_path = path;
663663
}
664664

665+
pub fn requested_lockfile_path(&self) -> Option<&Path> {
666+
self.requested_lockfile_path.as_deref()
667+
}
668+
665669
/// Get the lowest-common denominator `package.rust-version` within the workspace, if specified
666670
/// anywhere
667671
pub fn rust_version(&self) -> Option<&RustVersion> {

src/cargo/ops/cargo_install.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ struct InstallablePackage<'gctx> {
4646
vers: Option<VersionReq>,
4747
force: bool,
4848
no_track: bool,
49-
5049
pkg: Package,
5150
ws: Workspace<'gctx>,
5251
rustc: Rustc,
@@ -68,6 +67,7 @@ impl<'gctx> InstallablePackage<'gctx> {
6867
no_track: bool,
6968
needs_update_if_source_is_index: bool,
7069
current_rust_version: Option<&PartialVersion>,
70+
lockfile_path: Option<&Path>,
7171
) -> CargoResult<Option<Self>> {
7272
if let Some(name) = krate {
7373
if name == "." {
@@ -155,6 +155,7 @@ impl<'gctx> InstallablePackage<'gctx> {
155155
&root,
156156
&dst,
157157
force,
158+
lockfile_path,
158159
) {
159160
let msg = format!(
160161
"package `{}` is already installed, use --force to override",
@@ -179,8 +180,13 @@ impl<'gctx> InstallablePackage<'gctx> {
179180
}
180181
};
181182

182-
let (ws, rustc, target) =
183-
make_ws_rustc_target(gctx, &original_opts, &source_id, pkg.clone())?;
183+
let (ws, rustc, target) = make_ws_rustc_target(
184+
gctx,
185+
&original_opts,
186+
&source_id,
187+
pkg.clone(),
188+
lockfile_path.clone(),
189+
)?;
184190
// If we're installing in --locked mode and there's no `Cargo.lock` published
185191
// ie. the bin was published before https://github.com/rust-lang/cargo/pull/7026
186192
if gctx.locked() && !ws.root().join("Cargo.lock").exists() {
@@ -189,6 +195,14 @@ impl<'gctx> InstallablePackage<'gctx> {
189195
pkg.to_string()
190196
))?;
191197
}
198+
// When --lockfile-path is set, move lock file to the new location
199+
// (the new location is expected downstream and will be used during compilation)
200+
if gctx.locked() {
201+
if let Some(requested_lockfile_path) = ws.requested_lockfile_path() {
202+
paths::create_dir_all(ws.lock_root().as_path_unlocked())?;
203+
fs::rename(ws.root().join("Cargo.lock"), requested_lockfile_path)?;
204+
}
205+
}
192206
let pkg = if source_id.is_git() {
193207
// Don't use ws.current() in order to keep the package source as a git source so that
194208
// install tracking uses the correct source.
@@ -246,7 +260,6 @@ impl<'gctx> InstallablePackage<'gctx> {
246260
vers: vers.cloned(),
247261
force,
248262
no_track,
249-
250263
pkg,
251264
ws,
252265
rustc,
@@ -636,6 +649,7 @@ pub fn install(
636649
force: bool,
637650
no_track: bool,
638651
dry_run: bool,
652+
lockfile_path: Option<&Path>,
639653
) -> CargoResult<()> {
640654
let root = resolve_root(root, gctx)?;
641655
let dst = root.join("bin").into_path_unlocked();
@@ -667,6 +681,7 @@ pub fn install(
667681
no_track,
668682
true,
669683
current_rust_version.as_ref(),
684+
lockfile_path,
670685
)?;
671686
let mut installed_anything = true;
672687
if let Some(installable_pkg) = installable_pkg {
@@ -698,6 +713,7 @@ pub fn install(
698713
no_track,
699714
!did_update,
700715
current_rust_version.as_ref(),
716+
lockfile_path,
701717
) {
702718
Ok(Some(installable_pkg)) => {
703719
did_update = true;
@@ -804,6 +820,7 @@ fn installed_exact_package<T>(
804820
root: &Filesystem,
805821
dst: &Path,
806822
force: bool,
823+
lockfile_path: Option<&Path>,
807824
) -> CargoResult<Option<Package>>
808825
where
809826
T: Source,
@@ -819,7 +836,7 @@ where
819836
// best-effort check to see if we can avoid hitting the network.
820837
if let Ok(pkg) = select_dep_pkg(source, dep, gctx, false, None) {
821838
let (_ws, rustc, target) =
822-
make_ws_rustc_target(gctx, opts, &source.source_id(), pkg.clone())?;
839+
make_ws_rustc_target(gctx, opts, &source.source_id(), pkg.clone(), lockfile_path)?;
823840
if let Ok(true) = is_installed(&pkg, gctx, opts, &rustc, &target, root, dst, force) {
824841
return Ok(Some(pkg));
825842
}
@@ -832,6 +849,7 @@ fn make_ws_rustc_target<'gctx>(
832849
opts: &ops::CompileOptions,
833850
source_id: &SourceId,
834851
pkg: Package,
852+
lockfile_path: Option<&Path>,
835853
) -> CargoResult<(Workspace<'gctx>, Rustc, String)> {
836854
let mut ws = if source_id.is_git() || source_id.is_path() {
837855
Workspace::new(pkg.manifest_path(), gctx)?
@@ -841,6 +859,11 @@ fn make_ws_rustc_target<'gctx>(
841859
ws
842860
};
843861
ws.set_ignore_lock(gctx.lock_update_allowed());
862+
ws.set_requested_lockfile_path(lockfile_path.map(|p| p.to_path_buf()));
863+
// if --lockfile-path is set, imply --locked
864+
if ws.requested_lockfile_path().is_some() {
865+
ws.set_ignore_lock(false);
866+
}
844867
ws.set_require_optional_deps(false);
845868

846869
let rustc = gctx.load_global_rustc(Some(&ws))?;

src/cargo/util/context/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,10 @@ impl GlobalContext {
11421142
self.locked
11431143
}
11441144

1145+
pub fn set_locked(&mut self, locked: bool) {
1146+
self.locked = locked;
1147+
}
1148+
11451149
pub fn lock_update_allowed(&self) -> bool {
11461150
!self.frozen && !self.locked
11471151
}

tests/testsuite/cargo_install/help/stdout.term.svg

+29-27
Loading

tests/testsuite/lockfile_path.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::fs;
55
use snapbox::str;
66

77
use cargo_test_support::compare::assert_e2e;
8+
use cargo_test_support::install::assert_has_installed_exe;
89
use cargo_test_support::registry::{Package, RegistryBuilder};
910
use cargo_test_support::{
1011
basic_bin_manifest, cargo_process, cargo_test, paths, project, symlink_supported,
1112
ProjectBuilder,
1213
};
13-
1414
///////////////////////////////
1515
//// Unstable feature tests start
1616
///////////////////////////////
@@ -402,7 +402,8 @@ bar = "0.1.0"
402402
}
403403

404404
#[cargo_test]
405-
fn install_without_lockfile_path() {
405+
fn install_respects_lock_file_path() {
406+
// `cargo install` will imply --locked when lockfile path is provided
406407
Package::new("bar", "0.1.0").publish();
407408
Package::new("bar", "0.1.1")
408409
.file("src/lib.rs", "not rust")
@@ -440,10 +441,16 @@ dependencies = [
440441
"#]])
441442
.with_status(101)
442443
.run();
443-
cargo_process("install foo --locked").run();
444-
assert!(paths::root()
444+
445+
cargo_process("install foo -Zunstable-options --lockfile-path lockfile_dir/Cargo.lock")
446+
.masquerade_as_nightly_cargo(&["lockfile-path"])
447+
.run();
448+
449+
assert!(!paths::root()
445450
.join("home/.cargo/registry/src/-ec6bec4300fe98b6/foo-0.1.0/Cargo.lock")
446451
.is_file());
452+
assert!(paths::root().join("lockfile_dir/Cargo.lock").is_file());
453+
assert_has_installed_exe(paths::cargo_home(), "foo");
447454
}
448455

449456
#[cargo_test]

0 commit comments

Comments
 (0)