Skip to content

Commit e099df2

Browse files
committed
Auto merge of #9108 - CPerezz:locked_warn, r=alexcrichton
Impl warn for locked install without Cargo.lock If we're installing in --locked mode and there's no `Cargo.lock` published ie. the bin was published before #7026 the cargo install errors were not stating that it was due to the lack of the `Cargo.lock` file. Instead, the error seemed completely unrelated. Therefore, this tries to address this by adding a warn in the stderr output. Closes #9106 I will need some help on the testing side (assuming the code I added for the warning is correct). It looks to me that the publish function implemented for testing purposes does not publish `Cargo.lock` which is the actual convention. Should this be updated too? See #7026
2 parents c6f2815 + b526fad commit e099df2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ fn install_one(
254254
};
255255

256256
let (mut ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.clone())?;
257+
// If we're installing in --locked mode and there's no `Cargo.lock` published
258+
// ie. the bin was published before https://github.com/rust-lang/cargo/pull/7026
259+
if config.locked() && !ws.root().join("Cargo.lock").exists() {
260+
config.shell().warn(format!(
261+
"no Cargo.lock file published in {}",
262+
pkg.to_string()
263+
))?;
264+
}
257265
let pkg = if source_id.is_git() {
258266
// Don't use ws.current() in order to keep the package source as a git source so that
259267
// install tracking uses the correct source.

src/cargo/util/config/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ impl Config {
806806
self.frozen
807807
}
808808

809+
pub fn locked(&self) -> bool {
810+
self.locked
811+
}
812+
809813
pub fn lock_update_allowed(&self) -> bool {
810814
!self.frozen && !self.locked
811815
}

tests/testsuite/install.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ fn git_repo() {
682682
.with_stderr(
683683
"\
684684
[UPDATING] git repository `[..]`
685+
[WARNING] no Cargo.lock file published in foo v0.1.0 ([..])
685686
[INSTALLING] foo v0.1.0 ([..])
686687
[COMPILING] foo v0.1.0 ([..])
687688
[FINISHED] release [optimized] target(s) in [..]
@@ -1665,3 +1666,14 @@ workspace: [..]/foo/Cargo.toml
16651666
.run();
16661667
assert_has_installed_exe(cargo_home(), "foo");
16671668
}
1669+
1670+
#[cargo_test]
1671+
fn locked_install_without_published_lockfile() {
1672+
Package::new("foo", "0.1.0")
1673+
.file("src/main.rs", "//! Some docs\nfn main() {}")
1674+
.publish();
1675+
1676+
cargo_process("install foo --locked")
1677+
.with_stderr_contains("[WARNING] no Cargo.lock file published in foo v0.1.0")
1678+
.run();
1679+
}

0 commit comments

Comments
 (0)