Skip to content

Commit f6d30e9

Browse files
committed
Cache-messages: address some review comments.
- Add some comments and cleanup. - Error out when using `short`.
1 parent dcd4999 commit f6d30e9

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ impl BuildConfig {
107107
/// Whether or not Cargo should cache compiler messages on disk.
108108
pub fn cache_messages(&self) -> bool {
109109
self.cache_messages
110-
&& match self.message_format {
111-
MessageFormat::Human | MessageFormat::Json => true,
112-
// short is currently not supported
113-
MessageFormat::Short => false,
114-
}
115110
}
116111

117112
/// Whether or not the *user* wants JSON output. Whether or not rustc

src/cargo/core/compiler/mod.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod unit;
1414
use std::env;
1515
use std::ffi::{OsStr, OsString};
1616
use std::fs::{self, File};
17-
use std::io::{BufRead, BufReader, Write};
17+
use std::io::Write;
1818
use std::path::{Path, PathBuf};
1919
use std::sync::Arc;
2020

@@ -769,17 +769,21 @@ fn add_error_format(
769769
if supports_termcolor {
770770
cmd.arg("--json-rendered=termcolor");
771771
}
772+
if cx.bcx.build_config.message_format == MessageFormat::Short {
773+
// FIXME(rust-lang/rust#60419): right now we have no way of
774+
// turning on JSON messages from the compiler and also asking
775+
// the rendered field to be in the `short` format.
776+
bail!(
777+
"currently `--message-format short` is incompatible with {}",
778+
if pipelined {
779+
"pipelined compilation"
780+
} else {
781+
"cached output"
782+
}
783+
);
784+
}
772785
if pipelined {
773786
cmd.arg("-Zemit-artifact-notifications");
774-
if cx.bcx.build_config.message_format == MessageFormat::Short {
775-
// FIXME(rust-lang/rust#60419): right now we have no way of
776-
// turning on JSON messages from the compiler and also asking
777-
// the rendered field to be in the `short` format.
778-
bail!(
779-
"currently `--message-format short` is incompatible with \
780-
pipelined compilation"
781-
);
782-
}
783787
}
784788
} else {
785789
match cx.bcx.build_config.message_format {
@@ -1130,10 +1134,13 @@ fn on_stderr_line(
11301134
color: bool,
11311135
cache_cell: &mut Option<(PathBuf, LazyCell<File>)>,
11321136
) -> CargoResult<()> {
1137+
// Check if caching is enabled.
11331138
if let Some((path, cell)) = cache_cell {
1139+
// Cache the output, which will be replayed later when Fresh.
11341140
// The file is created lazily so that in the normal case, lots of
11351141
// empty files are not created.
11361142
let f = cell.try_borrow_mut_with(|| File::create(path))?;
1143+
debug_assert!(!line.contains('\n'));
11371144
f.write_all(line.as_bytes())?;
11381145
f.write_all(&[b'\n'])?;
11391146
}
@@ -1179,9 +1186,11 @@ fn on_stderr_line(
11791186
let rendered = if color {
11801187
error.rendered
11811188
} else {
1189+
// Strip only fails if the the Writer fails, which is Cursor
1190+
// on a Vec, which should never fail.
11821191
strip_ansi_escapes::strip(&error.rendered)
11831192
.map(|v| String::from_utf8(v).expect("utf8"))
1184-
.unwrap_or(error.rendered)
1193+
.expect("strip should never fail")
11851194
};
11861195
state.stderr(rendered);
11871196
return Ok(());
@@ -1265,25 +1274,22 @@ fn replay_output_cache(
12651274
MessageFormat::Short => false,
12661275
};
12671276
Work::new(move |state| {
1268-
if path.exists() {
1269-
let mut f = BufReader::new(File::open(&path)?);
1270-
let mut line = String::new();
1271-
loop {
1272-
if f.read_line(&mut line)? == 0 {
1273-
break;
1274-
}
1275-
on_stderr_line(
1276-
state,
1277-
&line,
1278-
package_id,
1279-
&target,
1280-
extract_rendered_messages,
1281-
false, // look_for_metadata_directive
1282-
color,
1283-
&mut None,
1284-
)?;
1285-
line.clear();
1286-
}
1277+
if !path.exists() {
1278+
// No cached output, probably didn't emit anything.
1279+
return Ok(());
1280+
}
1281+
let contents = fs::read_to_string(&path)?;
1282+
for line in contents.lines() {
1283+
on_stderr_line(
1284+
state,
1285+
&line,
1286+
package_id,
1287+
&target,
1288+
extract_rendered_messages,
1289+
false, // look_for_metadata_directive
1290+
color,
1291+
&mut None,
1292+
)?;
12871293
}
12881294
Ok(())
12891295
})

tests/testsuite/cache_messages.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,15 @@ fn very_verbose() {
334334
.with_stderr_contains("[..]not_used[..]")
335335
.run();
336336
}
337+
338+
#[test]
339+
fn short_incompatible() {
340+
let p = project().file("src/lib.rs", "").build();
341+
p.cargo("check -Zcache-messages --message-format=short")
342+
.masquerade_as_nightly_cargo()
343+
.with_stderr(
344+
"[ERROR] currently `--message-format short` is incompatible with cached output",
345+
)
346+
.with_status(101)
347+
.run();
348+
}

0 commit comments

Comments
 (0)