Skip to content

Commit ffe6587

Browse files
committed
Auto merge of #6560 - ehuss:better-install-error, r=Eh2406
Better error message for bad manifest with `cargo install`. The old code assumed that any error loading a manifest meant that the manifest didn't exist, but there are many other reasons it may fail. Add a few helpful messages for some common cases. First noticed at #5654 (comment)
2 parents 10e3230 + 3378a5a commit ffe6587

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/cargo/ops/cargo_install.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,30 @@ fn install_one(
174174
)?
175175
} else if source_id.is_path() {
176176
let mut src = path_source(source_id, config)?;
177-
src.update().chain_err(|| {
178-
failure::format_err!(
179-
"`{}` is not a crate root; specify a crate to \
180-
install from crates.io, or use --path or --git to \
181-
specify an alternate source",
177+
if !src.path().is_dir() {
178+
failure::bail!(
179+
"`{}` is not a directory. \
180+
--path must point to a directory containing a Cargo.toml file.",
182181
src.path().display()
183182
)
184-
})?;
183+
}
184+
if !src.path().join("Cargo.toml").exists() {
185+
if from_cwd {
186+
failure::bail!(
187+
"`{}` is not a crate root; specify a crate to \
188+
install from crates.io, or use --path or --git to \
189+
specify an alternate source",
190+
src.path().display()
191+
);
192+
} else {
193+
failure::bail!(
194+
"`{}` does not contain a Cargo.toml file. \
195+
--path must point to a directory containing a Cargo.toml file.",
196+
src.path().display()
197+
)
198+
}
199+
}
200+
src.update()?;
185201
select_pkg(src, krate, vers, config, false, &mut |path| {
186202
path.read_packages()
187203
})?

tests/testsuite/install.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,27 @@ fn bad_version() {
160160
}
161161

162162
#[test]
163-
fn no_crate() {
163+
fn bad_paths() {
164164
cargo_process("install")
165165
.with_status(101)
166-
.with_stderr(
167-
"\
168-
[ERROR] `[..]` is not a crate root; specify a crate to install [..]
166+
.with_stderr("[ERROR] `[CWD]` is not a crate root; specify a crate to install [..]")
167+
.run();
169168

170-
Caused by:
171-
failed to read `[..]Cargo.toml`
169+
cargo_process("install --path .")
170+
.with_status(101)
171+
.with_stderr("[ERROR] `[CWD]` does not contain a Cargo.toml file[..]")
172+
.run();
172173

173-
Caused by:
174-
[..] (os error [..])
175-
",
176-
)
174+
let toml = paths::root().join("Cargo.toml");
175+
fs::write(toml, "").unwrap();
176+
cargo_process("install --path Cargo.toml")
177+
.with_status(101)
178+
.with_stderr("[ERROR] `[CWD]/Cargo.toml` is not a directory[..]")
179+
.run();
180+
181+
cargo_process("install --path .")
182+
.with_status(101)
183+
.with_stderr_contains("[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`")
177184
.run();
178185
}
179186

tests/testsuite/support/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,8 @@ enum MatchKind {
12491249
/// - There is a wide range of macros (such as `[COMPILING]` or `[WARNING]`)
12501250
/// to match cargo's "status" output and allows you to ignore the alignment.
12511251
/// See `substitute_macros` for a complete list of macros.
1252+
/// - `[ROOT]` is `/` or `[..]:\` on Windows.
1253+
/// - `[CWD]` is the working directory of the process that was run.
12521254
pub fn lines_match(expected: &str, actual: &str) -> bool {
12531255
// Let's not deal with / vs \ (windows...)
12541256
// First replace backslash-escaped backslashes with forward slashes

0 commit comments

Comments
 (0)