Skip to content

Commit

Permalink
feat: --lockfile-path add install support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifropc committed Sep 22, 2024
1 parent 59b74f2 commit 02c7b5e
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
9 changes: 9 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn cli() -> Command {
.arg_target_triple("Build for the target triple")
.arg_target_dir()
.arg_timings()
.arg_lockfile_path()
.after_help(color_print::cstr!(
"Run `<cyan,bold>cargo help install</>` for more detailed information.\n"
))
Expand Down Expand Up @@ -204,6 +205,13 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
if args.dry_run() {
gctx.cli_unstable().fail_if_stable_opt("--dry-run", 11123)?;
}

let requested_lockfile_path = args.lockfile_path(gctx)?;
// 14421: lockfile path should imply --locked on running `install`
if requested_lockfile_path.is_some() {
gctx.set_locked(true);
}

if args.flag("list") {
ops::install_list(root, gctx)?;
} else {
Expand All @@ -217,6 +225,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
args.flag("force"),
args.flag("no-track"),
args.dry_run(),
requested_lockfile_path.as_deref(),
)?;
}
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ impl<'gctx> Workspace<'gctx> {
self.requested_lockfile_path = path;
}

pub fn requested_lockfile_path(&self) -> Option<&Path> {
self.requested_lockfile_path.as_deref()
}

/// Get the lowest-common denominator `package.rust-version` within the workspace, if specified
/// anywhere
pub fn rust_version(&self) -> Option<&RustVersion> {
Expand Down
33 changes: 28 additions & 5 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct InstallablePackage<'gctx> {
vers: Option<VersionReq>,
force: bool,
no_track: bool,

pkg: Package,
ws: Workspace<'gctx>,
rustc: Rustc,
Expand All @@ -68,6 +67,7 @@ impl<'gctx> InstallablePackage<'gctx> {
no_track: bool,
needs_update_if_source_is_index: bool,
current_rust_version: Option<&PartialVersion>,
lockfile_path: Option<&Path>,
) -> CargoResult<Option<Self>> {
if let Some(name) = krate {
if name == "." {
Expand Down Expand Up @@ -155,6 +155,7 @@ impl<'gctx> InstallablePackage<'gctx> {
&root,
&dst,
force,
lockfile_path,
) {
let msg = format!(
"package `{}` is already installed, use --force to override",
Expand All @@ -179,8 +180,13 @@ impl<'gctx> InstallablePackage<'gctx> {
}
};

let (ws, rustc, target) =
make_ws_rustc_target(gctx, &original_opts, &source_id, pkg.clone())?;
let (ws, rustc, target) = make_ws_rustc_target(
gctx,
&original_opts,
&source_id,
pkg.clone(),
lockfile_path.clone(),
)?;
// If we're installing in --locked mode and there's no `Cargo.lock` published
// ie. the bin was published before https://github.com/rust-lang/cargo/pull/7026
if gctx.locked() && !ws.root().join("Cargo.lock").exists() {
Expand All @@ -189,6 +195,14 @@ impl<'gctx> InstallablePackage<'gctx> {
pkg.to_string()
))?;
}
// When --lockfile-path is set, move lock file to the new location
// (the new location is expected downstream and will be used during compilation)
if gctx.locked() {
if let Some(requested_lockfile_path) = ws.requested_lockfile_path() {
paths::create_dir_all(ws.lock_root().as_path_unlocked())?;
fs::rename(ws.root().join("Cargo.lock"), requested_lockfile_path)?;
}
}
let pkg = if source_id.is_git() {
// Don't use ws.current() in order to keep the package source as a git source so that
// install tracking uses the correct source.
Expand Down Expand Up @@ -246,7 +260,6 @@ impl<'gctx> InstallablePackage<'gctx> {
vers: vers.cloned(),
force,
no_track,

pkg,
ws,
rustc,
Expand Down Expand Up @@ -636,6 +649,7 @@ pub fn install(
force: bool,
no_track: bool,
dry_run: bool,
lockfile_path: Option<&Path>,
) -> CargoResult<()> {
let root = resolve_root(root, gctx)?;
let dst = root.join("bin").into_path_unlocked();
Expand Down Expand Up @@ -667,6 +681,7 @@ pub fn install(
no_track,
true,
current_rust_version.as_ref(),
lockfile_path,
)?;
let mut installed_anything = true;
if let Some(installable_pkg) = installable_pkg {
Expand Down Expand Up @@ -698,6 +713,7 @@ pub fn install(
no_track,
!did_update,
current_rust_version.as_ref(),
lockfile_path,
) {
Ok(Some(installable_pkg)) => {
did_update = true;
Expand Down Expand Up @@ -804,6 +820,7 @@ fn installed_exact_package<T>(
root: &Filesystem,
dst: &Path,
force: bool,
lockfile_path: Option<&Path>,
) -> CargoResult<Option<Package>>
where
T: Source,
Expand All @@ -819,7 +836,7 @@ where
// best-effort check to see if we can avoid hitting the network.
if let Ok(pkg) = select_dep_pkg(source, dep, gctx, false, None) {
let (_ws, rustc, target) =
make_ws_rustc_target(gctx, opts, &source.source_id(), pkg.clone())?;
make_ws_rustc_target(gctx, opts, &source.source_id(), pkg.clone(), lockfile_path)?;
if let Ok(true) = is_installed(&pkg, gctx, opts, &rustc, &target, root, dst, force) {
return Ok(Some(pkg));
}
Expand All @@ -832,6 +849,7 @@ fn make_ws_rustc_target<'gctx>(
opts: &ops::CompileOptions,
source_id: &SourceId,
pkg: Package,
lockfile_path: Option<&Path>,
) -> CargoResult<(Workspace<'gctx>, Rustc, String)> {
let mut ws = if source_id.is_git() || source_id.is_path() {
Workspace::new(pkg.manifest_path(), gctx)?
Expand All @@ -841,6 +859,11 @@ fn make_ws_rustc_target<'gctx>(
ws
};
ws.set_ignore_lock(gctx.lock_update_allowed());
ws.set_requested_lockfile_path(lockfile_path.map(|p| p.to_path_buf()));
// if --lockfile-path is set, imply --locked
if ws.requested_lockfile_path().is_some() {
ws.set_ignore_lock(false);
}
ws.set_require_optional_deps(false);

let rustc = gctx.load_global_rustc(Some(&ws))?;
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,10 @@ impl GlobalContext {
self.locked
}

pub fn set_locked(&mut self, locked: bool) {
self.locked = locked;
}

pub fn lock_update_allowed(&self) -> bool {
!self.frozen && !self.locked
}
Expand Down
56 changes: 29 additions & 27 deletions tests/testsuite/cargo_install/help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 11 additions & 4 deletions tests/testsuite/lockfile_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::fs;
use snapbox::str;

use cargo_test_support::compare::assert_e2e;
use cargo_test_support::install::assert_has_installed_exe;
use cargo_test_support::registry::{Package, RegistryBuilder};
use cargo_test_support::{
basic_bin_manifest, cargo_process, cargo_test, paths, project, symlink_supported,
ProjectBuilder,
};

///////////////////////////////
//// Unstable feature tests start
///////////////////////////////
Expand Down Expand Up @@ -402,7 +402,8 @@ bar = "0.1.0"
}

#[cargo_test]
fn install_without_lockfile_path() {
fn install_respects_lock_file_path() {
// `cargo install` will imply --locked when lockfile path is provided
Package::new("bar", "0.1.0").publish();
Package::new("bar", "0.1.1")
.file("src/lib.rs", "not rust")
Expand Down Expand Up @@ -440,10 +441,16 @@ dependencies = [
"#]])
.with_status(101)
.run();
cargo_process("install foo --locked").run();
assert!(paths::root()

cargo_process("install foo -Zunstable-options --lockfile-path lockfile_dir/Cargo.lock")
.masquerade_as_nightly_cargo(&["lockfile-path"])
.run();

assert!(!paths::root()
.join("home/.cargo/registry/src/-ec6bec4300fe98b6/foo-0.1.0/Cargo.lock")
.is_file());
assert!(paths::root().join("lockfile_dir/Cargo.lock").is_file());
assert_has_installed_exe(paths::cargo_home(), "foo");
}

#[cargo_test]
Expand Down

0 comments on commit 02c7b5e

Please sign in to comment.