Skip to content

Commit cd17472

Browse files
committed
Auto merge of #6839 - ehuss:new-error-causes, r=alexcrichton
Improve warning in `cargo new` with parse error. 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.
2 parents 22ea412 + 3c06262 commit cd17472

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use git2::Config as GitConfig;
99
use git2::Repository as GitRepository;
1010

1111
use crate::core::{compiler, Workspace};
12-
use crate::util::errors::{CargoResult, CargoResultExt};
12+
use crate::util::errors::{self, CargoResult, CargoResultExt};
1313
use crate::util::{existing_vcs_repo, internal, FossilRepo, GitRepo, HgRepo, PijulRepo};
1414
use crate::util::{paths, validate_package_name, Config};
1515

@@ -677,7 +677,7 @@ mod tests {
677677
let msg = format!(
678678
"compiling this new crate may not work due to invalid \
679679
workspace configuration\n\n{}",
680-
e
680+
errors::display_causes(&e)
681681
);
682682
config.shell().warn(msg)?;
683683
}

src/cargo/util/config.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::core::profiles::ConfigProfiles;
2525
use crate::core::shell::Verbosity;
2626
use crate::core::{CliUnstable, Shell, SourceId, Workspace};
2727
use crate::ops;
28-
use crate::util::errors::{internal, CargoResult, CargoResultExt};
28+
use crate::util::errors::{self, internal, CargoResult, CargoResultExt};
2929
use crate::util::toml as cargo_toml;
3030
use crate::util::Filesystem;
3131
use crate::util::Rustc;
@@ -932,12 +932,7 @@ impl std::error::Error for ConfigError {}
932932
// `cause` and avoid doing the cause formatting here.
933933
impl fmt::Display for ConfigError {
934934
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
935-
let message = self
936-
.error
937-
.iter_chain()
938-
.map(|e| e.to_string())
939-
.collect::<Vec<_>>()
940-
.join("\nCaused by:\n ");
935+
let message = errors::display_causes(&self.error);
941936
if let Some(ref definition) = self.definition {
942937
write!(f, "error in {}: {}", definition, message)
943938
} else {

src/cargo/util/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,11 @@ pub fn internal<S: fmt::Display>(error: S) -> failure::Error {
388388
fn _internal(error: &dyn fmt::Display) -> failure::Error {
389389
Internal::new(failure::format_err!("{}", error)).into()
390390
}
391+
392+
pub fn display_causes(error: &Error) -> String {
393+
error
394+
.iter_chain()
395+
.map(|e| e.to_string())
396+
.collect::<Vec<_>>()
397+
.join("\nCaused by:\n ")
398+
}

tests/testsuite/workspaces.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,25 @@ root: [..]
988988
.run();
989989
}
990990

991+
#[test]
992+
fn new_warning_with_corrupt_ws() {
993+
let p = project().file("Cargo.toml", "asdf").build();
994+
p.cargo("new bar")
995+
.with_stderr(
996+
"\
997+
[WARNING] compiling this new crate may not work due to invalid workspace configuration
998+
999+
failed to parse manifest at `[..]foo/Cargo.toml`
1000+
Caused by:
1001+
could not parse input as TOML
1002+
Caused by:
1003+
expected an equals, found eof at line 1
1004+
Created binary (application) `bar` package
1005+
",
1006+
)
1007+
.run();
1008+
}
1009+
9911010
#[test]
9921011
fn lock_doesnt_change_depending_on_crate() {
9931012
let p = project()

0 commit comments

Comments
 (0)