Skip to content

Commit 9d11cd1

Browse files
committed
Auto merge of #7819 - ehuss:fix-replay-newlines, r=alexcrichton
Fix cache replay including extra newlines. The compiler output cache replay was changed in #7737 to use `BufReader::read_line` instead of `str::lines`. `read_line`, unlike `lines`, includes the trailing line ending. The code is written assuming that the line endings are stripped, so make sure they are stripped here, too. This only happens for non-JSON messages, like `RUSTC_LOG`.
2 parents f6449ba + 7be3c2f commit 9d11cd1

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,8 @@ fn replay_output_cache(
12571257
if length == 0 {
12581258
break;
12591259
}
1260-
on_stderr_line(state, line.as_str(), package_id, &target, &mut options)?;
1260+
let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
1261+
on_stderr_line(state, trimmed, package_id, &target, &mut options)?;
12611262
line.clear();
12621263
}
12631264
Ok(())

tests/testsuite/cache_messages.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Tests for caching compiler diagnostics.
22
33
use cargo_test_support::{
4-
clippy_is_available, is_coarse_mtime, process, project, registry::Package, sleep_ms,
4+
basic_manifest, clippy_is_available, is_coarse_mtime, process, project, registry::Package,
5+
sleep_ms,
56
};
67
use std::path::Path;
78

@@ -390,3 +391,49 @@ fn doesnt_create_extra_files() {
390391
p.cargo("build").run();
391392
assert_eq!(p.glob("target/debug/.fingerprint/foo-*/output").count(), 1);
392393
}
394+
395+
#[cargo_test]
396+
fn replay_non_json() {
397+
// Handles non-json output.
398+
let rustc = project()
399+
.at("rustc")
400+
.file("Cargo.toml", &basic_manifest("rustc_alt", "1.0.0"))
401+
.file(
402+
"src/main.rs",
403+
r#"
404+
fn main() {
405+
eprintln!("line 1");
406+
eprintln!("line 2");
407+
let r = std::process::Command::new("rustc")
408+
.args(std::env::args_os().skip(1))
409+
.status();
410+
std::process::exit(r.unwrap().code().unwrap_or(2));
411+
}
412+
"#,
413+
)
414+
.build();
415+
rustc.cargo("build").run();
416+
let p = project().file("src/lib.rs", "").build();
417+
p.cargo("check")
418+
.env("RUSTC", rustc.bin("rustc_alt"))
419+
.with_stderr(
420+
"\
421+
[CHECKING] foo [..]
422+
line 1
423+
line 2
424+
[FINISHED] dev [..]
425+
",
426+
)
427+
.run();
428+
429+
p.cargo("check")
430+
.env("RUSTC", rustc.bin("rustc_alt"))
431+
.with_stderr(
432+
"\
433+
line 1
434+
line 2
435+
[FINISHED] dev [..]
436+
",
437+
)
438+
.run();
439+
}

0 commit comments

Comments
 (0)