Skip to content

Commit 3d89379

Browse files
committed
cargo install: Ignore Cargo.lock for non --path installs.
Requires `--locked` to use Cargo.lock for registry and git installs.
1 parent 27932ea commit 3d89379

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

src/cargo/core/workspace.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ pub struct Workspace<'cfg> {
7777
// A cache of loaded packages for particular paths which is disjoint from
7878
// `packages` up above, used in the `load` method down below.
7979
loaded_packages: RefCell<HashMap<PathBuf, Package>>,
80+
81+
// If `true`, then the resolver will ignore any existing `Cargo.lock`
82+
// file. This is set for `cargo install` without `--locked`.
83+
ignore_lock: bool,
8084
}
8185

8286
// Separate structure for tracking loaded packages (to avoid loading anything
@@ -148,6 +152,7 @@ impl<'cfg> Workspace<'cfg> {
148152
is_ephemeral: false,
149153
require_optional_deps: true,
150154
loaded_packages: RefCell::new(HashMap::new()),
155+
ignore_lock: false,
151156
};
152157
ws.root_manifest = ws.find_root(manifest_path)?;
153158
ws.find_members()?;
@@ -184,6 +189,7 @@ impl<'cfg> Workspace<'cfg> {
184189
is_ephemeral: true,
185190
require_optional_deps,
186191
loaded_packages: RefCell::new(HashMap::new()),
192+
ignore_lock: false,
187193
};
188194
{
189195
let key = ws.current_manifest.parent().unwrap();
@@ -320,14 +326,23 @@ impl<'cfg> Workspace<'cfg> {
320326
self.require_optional_deps
321327
}
322328

323-
pub fn set_require_optional_deps<'a>(
324-
&'a mut self,
329+
pub fn set_require_optional_deps(
330+
&mut self,
325331
require_optional_deps: bool,
326332
) -> &mut Workspace<'cfg> {
327333
self.require_optional_deps = require_optional_deps;
328334
self
329335
}
330336

337+
pub fn ignore_lock(&self) -> bool {
338+
self.ignore_lock
339+
}
340+
341+
pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'cfg> {
342+
self.ignore_lock = ignore_lock;
343+
self
344+
}
345+
331346
/// Finds the root of a workspace for the crate whose manifest is located
332347
/// at `manifest_path`.
333348
///

src/cargo/ops/cargo_install.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ fn install_one(
215215
};
216216

217217
let ws = match overidden_target_dir {
218-
Some(dir) => Workspace::ephemeral(pkg, config, Some(dir), false)?,
218+
Some(dir) => {
219+
let mut ws = Workspace::ephemeral(pkg, config, Some(dir), false)?;
220+
ws.set_ignore_lock(config.lock_update_allowed());
221+
ws
222+
}
219223
None => {
220224
let mut ws = Workspace::new(pkg.manifest_path(), config)?;
221225
ws.set_require_optional_deps(false);

src/cargo/ops/resolve.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub fn resolve_ws_with_method<'a>(
6464
}
6565
let mut add_patches = true;
6666

67-
let resolve = if ws.require_optional_deps() {
67+
let resolve = if ws.ignore_lock() {
68+
None
69+
} else if ws.require_optional_deps() {
6870
// First, resolve the root_package's *listed* dependencies, as well as
6971
// downloading and updating all remotes and such.
7072
let resolve = resolve_with_registry(ws, &mut registry, false)?;

tests/testsuite/install.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,8 @@ fn custom_target_dir_for_git_source() {
11901190

11911191
#[test]
11921192
fn install_respects_lock_file() {
1193+
// `cargo install` now requires --locked to use a Cargo.lock for non
1194+
// --path installs.
11931195
Package::new("bar", "0.1.0").publish();
11941196
Package::new("bar", "0.1.1")
11951197
.file("src/lib.rs", "not rust")
@@ -1219,7 +1221,52 @@ dependencies = [
12191221
)
12201222
.publish();
12211223

1222-
cargo_process("install foo").run();
1224+
cargo_process("install foo")
1225+
.with_stderr_contains("[..]not rust[..]")
1226+
.with_status(101)
1227+
.run();
1228+
cargo_process("install --locked foo").run();
1229+
}
1230+
1231+
#[test]
1232+
fn install_path_respects_lock_file() {
1233+
// For --path installs, always use local Cargo.lock.
1234+
Package::new("bar", "0.1.0").publish();
1235+
Package::new("bar", "0.1.1")
1236+
.file("src/lib.rs", "not rust")
1237+
.publish();
1238+
let p = project()
1239+
.file(
1240+
"Cargo.toml",
1241+
r#"
1242+
[package]
1243+
name = "foo"
1244+
version = "0.1.0"
1245+
1246+
[dependencies]
1247+
bar = "0.1"
1248+
"#,
1249+
)
1250+
.file("src/main.rs", "extern crate bar; fn main() {}")
1251+
.file(
1252+
"Cargo.lock",
1253+
r#"
1254+
[[package]]
1255+
name = "bar"
1256+
version = "0.1.0"
1257+
source = "registry+https://github.com/rust-lang/crates.io-index"
1258+
1259+
[[package]]
1260+
name = "foo"
1261+
version = "0.1.0"
1262+
dependencies = [
1263+
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
1264+
]
1265+
"#,
1266+
)
1267+
.build();
1268+
1269+
p.cargo("install --path .").run();
12231270
}
12241271

12251272
#[test]

0 commit comments

Comments
 (0)