Skip to content

Commit

Permalink
Auto merge of #6933 - ehuss:cache-output, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed May 21, 2019
2 parents 0ef35b9 + 9ba6812 commit 5218d04
Show file tree
Hide file tree
Showing 15 changed files with 626 additions and 89 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ matrix:

before_script:
- rustup target add $ALT
- rustup component add clippy || echo "clippy not available"
script:
- cargo test --features=deny-warnings

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ serde = { version = "1.0.82", features = ['derive'] }
serde_ignored = "0.0.4"
serde_json = { version = "1.0.30", features = ["raw_value"] }
shell-escape = "0.1.4"
strip-ansi-escapes = "0.1.0"
tar = { version = "0.4.18", default-features = false }
tempfile = "3.0"
termcolor = "1.0"
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ install:
- rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
- if defined OTHER_TARGET rustup target add %OTHER_TARGET%
- rustup component add clippy || exit 0
- rustc -V
- cargo -V
- git submodule update --init
Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Available unstable (nightly-only) flags:
-Z unstable-options -- Allow the usage of unstable options such as --registry
-Z config-profile -- Read profiles from .cargo/config files
-Z install-upgrade -- `cargo install` will upgrade instead of failing
-Z cache-messages -- Cache compiler messages
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
);
Expand Down
13 changes: 12 additions & 1 deletion src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct BuildConfig {
/// An optional wrapper, if any, used to wrap rustc invocations
pub rustc_wrapper: Option<ProcessBuilder>,
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
/// Whether or not Cargo should cache compiler output on disk.
cache_messages: bool,
}

impl BuildConfig {
Expand Down Expand Up @@ -87,6 +89,7 @@ impl BuildConfig {
}
let cfg_jobs: Option<u32> = config.get("build.jobs")?;
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);

Ok(BuildConfig {
requested_target: target,
jobs,
Expand All @@ -97,10 +100,18 @@ impl BuildConfig {
build_plan: false,
rustc_wrapper: None,
rustfix_diagnostic_server: RefCell::new(None),
cache_messages: config.cli_unstable().cache_messages,
})
}

pub fn json_messages(&self) -> bool {
/// Whether or not Cargo should cache compiler messages on disk.
pub fn cache_messages(&self) -> bool {
self.cache_messages
}

/// Whether or not the *user* wants JSON output. Whether or not rustc
/// actually uses JSON is decided in `add_error_format`.
pub fn emit_json(&self) -> bool {
self.message_format == MessageFormat::Json
}

Expand Down
5 changes: 5 additions & 0 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
self.layout(unit.kind).fingerprint().join(dir)
}

/// Path where compiler output is cached.
pub fn message_cache_path(&self, unit: &Unit<'a>) -> PathBuf {
self.fingerprint_dir(unit).join("output")
}

/// Returns the directory where a compiled build script is stored.
/// `/path/to/target/{debug,release}/build/PKG-HASH`
pub fn build_script_dir(&self, unit: &Unit<'a>) -> PathBuf {
Expand Down
8 changes: 4 additions & 4 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn emit_build_output(state: &JobState<'_>, output: &BuildOutput, package_id: Pac
env: &output.env,
}
.to_json_string();
state.stdout(&msg);
state.stdout(msg);
}

fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Job> {
Expand Down Expand Up @@ -248,7 +248,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
);
let build_scripts = super::load_build_deps(cx, unit);
let kind = unit.kind;
let json_messages = bcx.build_config.json_messages();
let json_messages = bcx.build_config.emit_json();
let extra_verbose = bcx.config.extra_verbose();
let (prev_output, prev_script_out_dir) = prev_build_output(cx, unit);

Expand Down Expand Up @@ -315,13 +315,13 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
.exec_with_streaming(
&mut |stdout| {
if extra_verbose {
state.stdout(&format!("{}{}", prefix, stdout));
state.stdout(format!("{}{}", prefix, stdout));
}
Ok(())
},
&mut |stderr| {
if extra_verbose {
state.stderr(&format!("{}{}", prefix, stderr));
state.stderr(format!("{}{}", prefix, stderr));
}
Ok(())
},
Expand Down
8 changes: 4 additions & 4 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ impl<'a> JobState<'a> {
.send(Message::BuildPlanMsg(module_name, cmd, filenames));
}

pub fn stdout(&self, stdout: &str) {
drop(self.tx.send(Message::Stdout(stdout.to_string())));
pub fn stdout(&self, stdout: String) {
drop(self.tx.send(Message::Stdout(stdout)));
}

pub fn stderr(&self, stderr: &str) {
drop(self.tx.send(Message::Stderr(stderr.to_string())));
pub fn stderr(&self, stderr: String) {
drop(self.tx.send(Message::Stderr(stderr)));
}

/// A method used to signal to the coordinator thread that the rmeta file
Expand Down
Loading

0 comments on commit 5218d04

Please sign in to comment.