Skip to content

Commit

Permalink
Improve warning in cargo new with parse error.
Browse files Browse the repository at this point in the history
If `cargo new` fails to load a parent manifest (for whatever reason), it as not
displaying the reason why. Add the causes to provide more context.
  • Loading branch information
ehuss committed Apr 11, 2019
1 parent 811a4f0 commit 3c06262
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use git2::Config as GitConfig;
use git2::Repository as GitRepository;

use crate::core::{compiler, Workspace};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::errors::{self, CargoResult, CargoResultExt};
use crate::util::{existing_vcs_repo, internal, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{paths, validate_package_name, Config};

Expand Down Expand Up @@ -677,7 +677,7 @@ mod tests {
let msg = format!(
"compiling this new crate may not work due to invalid \
workspace configuration\n\n{}",
e
errors::display_causes(&e)
);
config.shell().warn(msg)?;
}
Expand Down
9 changes: 2 additions & 7 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::core::profiles::ConfigProfiles;
use crate::core::shell::Verbosity;
use crate::core::{CliUnstable, Shell, SourceId, Workspace};
use crate::ops;
use crate::util::errors::{internal, CargoResult, CargoResultExt};
use crate::util::errors::{self, internal, CargoResult, CargoResultExt};
use crate::util::toml as cargo_toml;
use crate::util::Filesystem;
use crate::util::Rustc;
Expand Down Expand Up @@ -932,12 +932,7 @@ impl std::error::Error for ConfigError {}
// `cause` and avoid doing the cause formatting here.
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = self
.error
.iter_chain()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\nCaused by:\n ");
let message = errors::display_causes(&self.error);
if let Some(ref definition) = self.definition {
write!(f, "error in {}: {}", definition, message)
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,11 @@ pub fn internal<S: fmt::Display>(error: S) -> failure::Error {
fn _internal(error: &dyn fmt::Display) -> failure::Error {
Internal::new(failure::format_err!("{}", error)).into()
}

pub fn display_causes(error: &Error) -> String {
error
.iter_chain()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\nCaused by:\n ")
}
19 changes: 19 additions & 0 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,25 @@ root: [..]
.run();
}

#[test]
fn new_warning_with_corrupt_ws() {
let p = project().file("Cargo.toml", "asdf").build();
p.cargo("new bar")
.with_stderr(
"\
[WARNING] compiling this new crate may not work due to invalid workspace configuration
failed to parse manifest at `[..]foo/Cargo.toml`
Caused by:
could not parse input as TOML
Caused by:
expected an equals, found eof at line 1
Created binary (application) `bar` package
",
)
.run();
}

#[test]
fn lock_doesnt_change_depending_on_crate() {
let p = project()
Expand Down

0 comments on commit 3c06262

Please sign in to comment.