Skip to content

Commit 5218d04

Browse files
committed
Auto merge of #6933 - ehuss:cache-output, r=alexcrichton
Add message caching. The `cache-messages` feature causes Cargo to cache the messages generated by the compiler. This is primarily useful if a crate compiles successfully with warnings. Previously, re-running Cargo would not display any output. With the `cache-messages` feature, it will quickly redisplay the previous warnings. ``` cargo +nightly check -Z cache-messages ``` Notes: - `short` messages do not work correctly. - rustdoc does not support `--json-rendered=termcolor`, so its output is currently uncolored. - This approach to rendering should address some output issues like #6848.
2 parents 0ef35b9 + 9ba6812 commit 5218d04

File tree

15 files changed

+626
-89
lines changed

15 files changed

+626
-89
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ matrix:
3939

4040
before_script:
4141
- rustup target add $ALT
42+
- rustup component add clippy || echo "clippy not available"
4243
script:
4344
- cargo test --features=deny-warnings
4445

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ serde = { version = "1.0.82", features = ['derive'] }
5454
serde_ignored = "0.0.4"
5555
serde_json = { version = "1.0.30", features = ["raw_value"] }
5656
shell-escape = "0.1.4"
57+
strip-ansi-escapes = "0.1.0"
5758
tar = { version = "0.4.18", default-features = false }
5859
tempfile = "3.0"
5960
termcolor = "1.0"

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ install:
99
- rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly
1010
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
1111
- if defined OTHER_TARGET rustup target add %OTHER_TARGET%
12+
- rustup component add clippy || exit 0
1213
- rustc -V
1314
- cargo -V
1415
- git submodule update --init

src/bin/cargo/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Available unstable (nightly-only) flags:
3535
-Z unstable-options -- Allow the usage of unstable options such as --registry
3636
-Z config-profile -- Read profiles from .cargo/config files
3737
-Z install-upgrade -- `cargo install` will upgrade instead of failing
38+
-Z cache-messages -- Cache compiler messages
3839
3940
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
4041
);

src/cargo/core/compiler/build_config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct BuildConfig {
2727
/// An optional wrapper, if any, used to wrap rustc invocations
2828
pub rustc_wrapper: Option<ProcessBuilder>,
2929
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
30+
/// Whether or not Cargo should cache compiler output on disk.
31+
cache_messages: bool,
3032
}
3133

3234
impl BuildConfig {
@@ -87,6 +89,7 @@ impl BuildConfig {
8789
}
8890
let cfg_jobs: Option<u32> = config.get("build.jobs")?;
8991
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
92+
9093
Ok(BuildConfig {
9194
requested_target: target,
9295
jobs,
@@ -97,10 +100,18 @@ impl BuildConfig {
97100
build_plan: false,
98101
rustc_wrapper: None,
99102
rustfix_diagnostic_server: RefCell::new(None),
103+
cache_messages: config.cli_unstable().cache_messages,
100104
})
101105
}
102106

103-
pub fn json_messages(&self) -> bool {
107+
/// Whether or not Cargo should cache compiler messages on disk.
108+
pub fn cache_messages(&self) -> bool {
109+
self.cache_messages
110+
}
111+
112+
/// Whether or not the *user* wants JSON output. Whether or not rustc
113+
/// actually uses JSON is decided in `add_error_format`.
114+
pub fn emit_json(&self) -> bool {
104115
self.message_format == MessageFormat::Json
105116
}
106117

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
192192
self.layout(unit.kind).fingerprint().join(dir)
193193
}
194194

195+
/// Path where compiler output is cached.
196+
pub fn message_cache_path(&self, unit: &Unit<'a>) -> PathBuf {
197+
self.fingerprint_dir(unit).join("output")
198+
}
199+
195200
/// Returns the directory where a compiled build script is stored.
196201
/// `/path/to/target/{debug,release}/build/PKG-HASH`
197202
pub fn build_script_dir(&self, unit: &Unit<'a>) -> PathBuf {

src/cargo/core/compiler/custom_build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn emit_build_output(state: &JobState<'_>, output: &BuildOutput, package_id: Pac
112112
env: &output.env,
113113
}
114114
.to_json_string();
115-
state.stdout(&msg);
115+
state.stdout(msg);
116116
}
117117

118118
fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Job> {
@@ -248,7 +248,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
248248
);
249249
let build_scripts = super::load_build_deps(cx, unit);
250250
let kind = unit.kind;
251-
let json_messages = bcx.build_config.json_messages();
251+
let json_messages = bcx.build_config.emit_json();
252252
let extra_verbose = bcx.config.extra_verbose();
253253
let (prev_output, prev_script_out_dir) = prev_build_output(cx, unit);
254254

@@ -315,13 +315,13 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
315315
.exec_with_streaming(
316316
&mut |stdout| {
317317
if extra_verbose {
318-
state.stdout(&format!("{}{}", prefix, stdout));
318+
state.stdout(format!("{}{}", prefix, stdout));
319319
}
320320
Ok(())
321321
},
322322
&mut |stderr| {
323323
if extra_verbose {
324-
state.stderr(&format!("{}{}", prefix, stderr));
324+
state.stderr(format!("{}{}", prefix, stderr));
325325
}
326326
Ok(())
327327
},

src/cargo/core/compiler/job_queue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ impl<'a> JobState<'a> {
105105
.send(Message::BuildPlanMsg(module_name, cmd, filenames));
106106
}
107107

108-
pub fn stdout(&self, stdout: &str) {
109-
drop(self.tx.send(Message::Stdout(stdout.to_string())));
108+
pub fn stdout(&self, stdout: String) {
109+
drop(self.tx.send(Message::Stdout(stdout)));
110110
}
111111

112-
pub fn stderr(&self, stderr: &str) {
113-
drop(self.tx.send(Message::Stderr(stderr.to_string())));
112+
pub fn stderr(&self, stderr: String) {
113+
drop(self.tx.send(Message::Stderr(stderr)));
114114
}
115115

116116
/// A method used to signal to the coordinator thread that the rmeta file

0 commit comments

Comments
 (0)