Skip to content

Commit

Permalink
Impl warn for locked install withoud Cargo.lock
Browse files Browse the repository at this point in the history
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
  • Loading branch information
CPerezz committed Jan 30, 2021
1 parent c3abcfe commit b526fad
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 b526fad

Please sign in to comment.