Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,11 +1095,14 @@ fn add_error_format_and_color(build_runner: &BuildRunner<'_, '_>, cmd: &mut Proc
cmd.arg("--error-format=json");
let mut json = String::from("--json=diagnostic-rendered-ansi,artifacts,future-incompat");

match build_runner.bcx.build_config.message_format {
MessageFormat::Short | MessageFormat::Json { short: true, .. } => {
json.push_str(",diagnostic-short");
}
_ => {}
if let MessageFormat::Short | MessageFormat::Json { short: true, .. } =
build_runner.bcx.build_config.message_format
{
json.push_str(",diagnostic-short");
} else if build_runner.bcx.gctx.shell().err_unicode()
&& build_runner.bcx.gctx.cli_unstable().rustc_unicode
{
json.push_str(",diagnostic-unicode");
}

if enable_timings {
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ unstable_cli_options!(
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
root_dir: Option<PathBuf> = ("Set the root directory relative to which paths are printed (defaults to workspace root)"),
rustc_unicode: bool = ("Enable `rustc`'s unicode error format in Cargo's error messages"),
rustdoc_depinfo: bool = ("Use dep-info files in rustdoc rebuild detection"),
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
Expand Down Expand Up @@ -1411,6 +1412,7 @@ impl CliUnstable {
"trim-paths" => self.trim_paths = parse_empty(k, v)?,
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,
"root-dir" => self.root_dir = v.map(|v| v.into()),
"rustc-unicode" => self.rustc_unicode = parse_empty(k, v)?,
"rustdoc-depinfo" => self.rustdoc_depinfo = parse_empty(k, v)?,
"rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?,
"rustdoc-scrape-examples" => self.rustdoc_scrape_examples = parse_empty(k, v)?,
Expand Down
34 changes: 33 additions & 1 deletion src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt;
use std::io::IsTerminal;
use std::io::prelude::*;

use annotate_snippets::renderer::DecorStyle;
use annotate_snippets::{Renderer, Report};
use anstream::AutoStream;
use anstyle::Style;
Expand Down Expand Up @@ -57,6 +58,7 @@ impl Shell {
stdout_unicode: supports_unicode(&std::io::stdout()),
stderr_unicode: supports_unicode(&std::io::stderr()),
stderr_term_integration: supports_term_integration(&std::io::stderr()),
unstable_flags_rustc_unicode: false,
},
verbosity: Verbosity::Verbose,
needs_clear: false,
Expand Down Expand Up @@ -380,6 +382,27 @@ impl Shell {
Some(url)
}

fn unstable_flags_rustc_unicode(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream {
unstable_flags_rustc_unicode,
..
} => *unstable_flags_rustc_unicode,
}
}

pub(crate) fn set_unstable_flags_rustc_unicode(&mut self, yes: bool) -> CargoResult<()> {
if let ShellOut::Stream {
unstable_flags_rustc_unicode,
..
} = &mut self.output
{
*unstable_flags_rustc_unicode = yes;
}
Ok(())
}

/// Prints a message to stderr and translates ANSI escape code into console colors.
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
if self.needs_clear {
Expand Down Expand Up @@ -419,7 +442,15 @@ impl Shell {
.err_width()
.diagnostic_terminal_width()
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH);
let rendered = Renderer::styled().term_width(term_width).render(report);
let decor_style = if self.err_unicode() && self.unstable_flags_rustc_unicode() {
DecorStyle::Unicode
} else {
DecorStyle::Ascii
};
let rendered = Renderer::styled()
.term_width(term_width)
.decor_style(decor_style)
.render(report);
self.err().write_all(rendered.as_bytes())?;
self.err().write_all(b"\n")?;
Ok(())
Expand All @@ -446,6 +477,7 @@ enum ShellOut {
stdout_unicode: bool,
stderr_unicode: bool,
stderr_term_integration: bool,
unstable_flags_rustc_unicode: bool,
},
}

Expand Down
3 changes: 3 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,9 @@ impl GlobalContext {
let cli_target_dir = target_dir.as_ref().map(|dir| Filesystem::new(dir.clone()));
self.target_dir = cli_target_dir;

self.shell()
.set_unstable_flags_rustc_unicode(self.unstable_flags.rustc_unicode)?;

Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Each new feature described below should explain how to use it.
* [unit-graph](#unit-graph) --- Emits JSON for Cargo's internal graph structure.
* [`cargo rustc --print`](#rustc---print) --- Calls rustc with `--print` to display information from rustc.
* [Build analysis](#build-analysis) --- Record and persist detailed build metrics across runs, with new commands to query past builds.
* [`rustc-unicode`](#rustc-unicode) --- Enables `rustc`'s unicode error format in Cargo's error messages
* Configuration
* [config-include](#config-include) --- Adds the ability for config files to include other files.
* [`cargo config`](#cargo-config) --- Adds a new subcommand for viewing config files.
Expand Down Expand Up @@ -2028,6 +2029,12 @@ cargo +nightly build --compile-time-deps -Z unstable-options
cargo +nightly check --compile-time-deps --all-targets -Z unstable-options
```

# `rustc-unicode`
* Tracking Issue: [rust#148607](https://github.com/rust-lang/rust/issues/148607)

Enable `rustc`'s unicode error format in Cargo's error messages


# Stabilized and removed features

## Compile progress
Expand Down
36 changes: 19 additions & 17 deletions tests/testsuite/cargo/z_help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/testsuite/lints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,42 @@ workspace = true
"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zrustc-unicode is unstable")]
fn unicode_report() {
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["test-dummy-unstable"]

[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []
im-a-teapot = true

[lints.cargo]
im_a_teapot = { level = "warn", priority = 10 }
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Zcargo-lints -Zrustc-unicode")
.masquerade_as_nightly_cargo(&["cargo-lints", "rustc-unicode", "test-dummy-unstable"])
.with_stderr_data(str![[r#"
[WARNING] `im_a_teapot` is specified
╭▸ Cargo.toml:9:1
9 │ im-a-teapot = true
│ ━━━━━━━━━━━━━━━━━━
╰ [NOTE] `cargo::im_a_teapot` is set to `warn` in `[lints]`
[CHECKING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}
36 changes: 36 additions & 0 deletions tests/testsuite/message_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,42 @@ failures:
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s


"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zrustc-unicode is unstable")]
fn cargo_passes_unicode_output() {
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
"#,
)
.file(
"src/lib.rs",
"\
mod tests {
#[test]
fn t1() {
use std::io;
}
}
",
)
.build();

foo.cargo("check -v -Zrustc-unicode")
.masquerade_as_nightly_cargo(&["rustc-unicode"])
.with_stderr_data(str![[r#"
[CHECKING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc [..] --json=diagnostic-rendered-ansi,artifacts,future-incompat,diagnostic-unicode [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}