diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index 97ea6cba813..16e5e908131 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -250,6 +250,8 @@ fn to_run_error(gctx: &GlobalContext, err: anyhow::Error) -> CliError { if is_quiet { CliError::code(exit_code) } else { + // Ensure a newline between user and cargo's output, especially with trailing "\r" + let _ = writeln!(gctx.shell().err()); CliError::new(err, exit_code) } } diff --git a/tests/testsuite/run.rs b/tests/testsuite/run.rs index ce3abc12fef..cd2cefbd53d 100644 --- a/tests/testsuite/run.rs +++ b/tests/testsuite/run.rs @@ -277,6 +277,7 @@ fn exit_code() { [COMPILING] foo v0.0.1 ([ROOT]/foo) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `target/debug/foo[EXE]` + [ERROR] process didn't exit successfully: `target/debug/foo[EXE]` ([EXIT_STATUS]: 2) "#]] @@ -306,6 +307,7 @@ fn exit_code_verbose() { [RUNNING] `rustc [..]` [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s [RUNNING] `target/debug/foo[EXE]` + [ERROR] process didn't exit successfully: `target/debug/foo[EXE]` ([EXIT_STATUS]: 2) "#]] @@ -325,6 +327,39 @@ fn exit_code_verbose() { .run(); } +#[cargo_test] +fn exit_code_with_carriage_return() { + let p = project() + .file( + "src/main.rs", + r#"fn main() { print!("hello\r"); std::process::exit(1); }"#, + ) + .build(); + + let expected = if !cfg!(unix) { + str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] `target/debug/foo[EXE]` + +[ERROR] process didn't exit successfully: `target/debug/foo[EXE]` ([EXIT_STATUS]: 1) + +"#]] + } else { + str![[r#" +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s +[RUNNING] `target/debug/foo` + +"#]] + }; + p.cargo("run") + .with_status(1) + .with_stderr_data(expected) + .with_stdout_data("hello\r") + .run(); +} + #[cargo_test] fn no_main_file() { let p = project().file("src/lib.rs", "").build();