Skip to content

Commit

Permalink
Auto merge of #9108 - CPerezz:locked_warn, r=alexcrichton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Feb 1, 2021
2 parents c6f2815 + b526fad commit e099df2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ fn install_one(
};

let (mut ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.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 config.locked() && !ws.root().join("Cargo.lock").exists() {
config.shell().warn(format!(
"no Cargo.lock file published in {}",
pkg.to_string()
))?;
}
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
4 changes: 4 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,10 @@ impl Config {
self.frozen
}

pub fn locked(&self) -> bool {
self.locked
}

pub fn lock_update_allowed(&self) -> bool {
!self.frozen && !self.locked
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ fn git_repo() {
.with_stderr(
"\
[UPDATING] git repository `[..]`
[WARNING] no Cargo.lock file published in foo v0.1.0 ([..])
[INSTALLING] foo v0.1.0 ([..])
[COMPILING] foo v0.1.0 ([..])
[FINISHED] release [optimized] target(s) in [..]
Expand Down Expand Up @@ -1665,3 +1666,14 @@ workspace: [..]/foo/Cargo.toml
.run();
assert_has_installed_exe(cargo_home(), "foo");
}

#[cargo_test]
fn locked_install_without_published_lockfile() {
Package::new("foo", "0.1.0")
.file("src/main.rs", "//! Some docs\nfn main() {}")
.publish();

cargo_process("install foo --locked")
.with_stderr_contains("[WARNING] no Cargo.lock file published in foo v0.1.0")
.run();
}

0 comments on commit e099df2

Please sign in to comment.