Skip to content

Commit 1d8cdaa

Browse files
committed
Auto merge of #11494 - ehuss:stabilize-diagnostic-width, r=weihanglo
Stabilize terminal-width This stabilized the passing of the `--diagnostic-width` flag to rustc and rustdoc so that they will format diagnostics to fit within the terminal size. Previously they always assume the width is 140. The diagnostics are trimmed with `...` to elide parts of extra-long lines. In cases where the width isn't known (such as not when used on a tty, or with mintty), then cargo does not pass the flag and the default of 140 is still used. At this time there is no way for the user to override the width (unlike with the progress bar width). That can be added in the future if there is demand. rust-lang/rust#84673 (comment) contains some thoughts on some different ideas. Closes rust-lang/rust#84673
2 parents 9cb3941 + 1709f0a commit 1d8cdaa

File tree

5 files changed

+27
-73
lines changed

5 files changed

+27
-73
lines changed

src/cargo/core/compiler/mod.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -899,22 +899,8 @@ fn add_error_format_and_color(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) {
899899
cmd.arg(json);
900900

901901
let config = cx.bcx.config;
902-
if config.nightly_features_allowed {
903-
match (
904-
config.cli_unstable().terminal_width,
905-
config.shell().err_width().diagnostic_terminal_width(),
906-
) {
907-
// Terminal width explicitly provided - only useful for testing.
908-
(Some(Some(width)), _) => {
909-
cmd.arg(format!("--diagnostic-width={}", width));
910-
}
911-
// Terminal width was not explicitly provided but flag was provided - common case.
912-
(Some(None), Some(width)) => {
913-
cmd.arg(format!("--diagnostic-width={}", width));
914-
}
915-
// User didn't opt-in.
916-
_ => (),
917-
}
902+
if let Some(width) = config.shell().err_width().diagnostic_terminal_width() {
903+
cmd.arg(format!("--diagnostic-width={width}"));
918904
}
919905
}
920906

src/cargo/core/features.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ unstable_cli_options!(
686686
target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"),
687687
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
688688
separate_nightlies: bool = (HIDDEN),
689-
terminal_width: Option<Option<usize>> = ("Provide a terminal width to rustc for error truncation"),
690689
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
691690
unstable_options: bool = ("Allow the usage of unstable options"),
692691
skip_rustdoc_fingerprint: bool = (HIDDEN),
@@ -749,6 +748,9 @@ const STABILIZED_TIMINGS: &str = "The -Ztimings option has been stabilized as --
749748

750749
const STABILISED_MULTITARGET: &str = "Multiple `--target` options are now always available.";
751750

751+
const STABILIZED_TERMINAL_WIDTH: &str =
752+
"The -Zterminal-width option is now always enabled for terminal output.";
753+
752754
fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
753755
where
754756
D: serde::Deserializer<'de>,
@@ -862,16 +864,6 @@ impl CliUnstable {
862864
Ok(true)
863865
}
864866

865-
fn parse_usize_opt(value: Option<&str>) -> CargoResult<Option<usize>> {
866-
Ok(match value {
867-
Some(value) => match value.parse::<usize>() {
868-
Ok(value) => Some(value),
869-
Err(e) => bail!("expected a number, found: {}", e),
870-
},
871-
None => None,
872-
})
873-
}
874-
875867
let mut stabilized_warn = |key: &str, version: &str, message: &str| {
876868
warnings.push(format!(
877869
"flag `-Z {}` has been stabilized in the {} release, \
@@ -955,7 +947,7 @@ impl CliUnstable {
955947
"separate-nightlies" => self.separate_nightlies = parse_empty(k, v)?,
956948
"multitarget" => stabilized_warn(k, "1.64", STABILISED_MULTITARGET),
957949
"rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?,
958-
"terminal-width" => self.terminal_width = Some(parse_usize_opt(v)?),
950+
"terminal-width" => stabilized_warn(k, "1.68", STABILIZED_TERMINAL_WIDTH),
959951
"sparse-registry" => self.sparse_registry = parse_empty(k, v)?,
960952
"registry-auth" => self.registry_auth = parse_empty(k, v)?,
961953
"namespaced-features" => stabilized_warn(k, "1.60", STABILISED_NAMESPACED_FEATURES),

src/cargo/core/shell.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ pub enum TtyWidth {
1414
}
1515

1616
impl TtyWidth {
17-
/// Returns the width provided with `-Z terminal-width` to rustc to truncate diagnostics with
18-
/// long lines.
17+
/// Returns the width of the terminal to use for diagnostics (which is
18+
/// relayed to rustc via `--diagnostic-width`).
1919
pub fn diagnostic_terminal_width(&self) -> Option<usize> {
20+
if let Ok(width) = std::env::var("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS") {
21+
return Some(width.parse().unwrap());
22+
}
2023
match *self {
2124
TtyWidth::NoTty | TtyWidth::Guess(_) => None,
2225
TtyWidth::Known(width) => Some(width),

src/doc/src/reference/unstable.md

+6-38
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ Each new feature described below should explain how to use it.
7070
* [public-dependency](#public-dependency) — Allows dependencies to be classified as either public or private.
7171
* Output behavior
7272
* [out-dir](#out-dir) — Adds a directory where artifacts are copied to.
73-
* [terminal-width](#terminal-width) — Tells rustc the width of the terminal so that long diagnostic messages can be truncated to be more readable.
7473
* [Different binary name](#different-binary-name) — Assign a name to the built binary that is separate from the crate name.
7574
* Compile behavior
7675
* [mtime-on-use](#mtime-on-use) — Updates the last-modified timestamp on every dependency every time it is used, to provide a mechanism to delete unused artifacts.
@@ -723,43 +722,6 @@ The default value is `"remote"`.
723722

724723
The value may also take a URL for a custom location.
725724

726-
### terminal-width
727-
728-
* Tracking Issue: [#84673](https://github.com/rust-lang/rust/issues/84673)
729-
730-
This feature provides a new flag, `-Z terminal-width`, which is used to pass
731-
a terminal width to `rustc` so that error messages containing long lines
732-
can be intelligently truncated.
733-
734-
For example, passing `-Z terminal-width=20` (an arbitrarily low value) might
735-
produce the following error:
736-
737-
```text
738-
error[E0308]: mismatched types
739-
--> src/main.rs:2:17
740-
|
741-
2 | ..._: () = 42;
742-
| -- ^^ expected `()`, found integer
743-
| |
744-
| expected due to this
745-
746-
error: aborting due to previous error
747-
```
748-
749-
In contrast, without `-Z terminal-width`, the error would look as shown below:
750-
751-
```text
752-
error[E0308]: mismatched types
753-
--> src/main.rs:2:17
754-
|
755-
2 | let _: () = 42;
756-
| -- ^^ expected `()`, found integer
757-
| |
758-
| expected due to this
759-
760-
error: aborting due to previous error
761-
```
762-
763725
### per-package-target
764726
* Tracking Issue: [#9406](https://github.com/rust-lang/cargo/pull/9406)
765727
* Original Pull Request: [#9030](https://github.com/rust-lang/cargo/pull/9030)
@@ -1447,3 +1409,9 @@ See [workspace.package](workspaces.md#the-package-table),
14471409
[workspace.dependencies](workspaces.md#the-dependencies-table),
14481410
and [inheriting-a-dependency-from-a-workspace](specifying-dependencies.md#inheriting-a-dependency-from-a-workspace)
14491411
for more information.
1412+
1413+
### terminal-width
1414+
1415+
The `-Z terminal-width` option has been stabilized in the 1.68 release.
1416+
The terminal width is always passed to the compiler when running from a
1417+
terminal where Cargo can automatically detect the width.

tests/testsuite/build.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -6130,23 +6130,28 @@ fn target_directory_backup_exclusion() {
61306130
assert!(!&cachedir_tag.is_file());
61316131
}
61326132

6133-
#[cargo_test(>=1.64, reason = "--diagnostic-width is stabilized in 1.64")]
6133+
#[cargo_test]
61346134
fn simple_terminal_width() {
61356135
let p = project()
61366136
.file(
61376137
"src/lib.rs",
61386138
r#"
6139-
fn main() {
6139+
pub fn foo() {
61406140
let _: () = 42;
61416141
}
61426142
"#,
61436143
)
61446144
.build();
61456145

6146-
p.cargo("build -Zterminal-width=20")
6147-
.masquerade_as_nightly_cargo(&["terminal-width"])
6146+
p.cargo("build -v")
6147+
.env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "20")
61486148
.with_status(101)
6149-
.with_stderr_contains("3 | ..._: () = 42;")
6149+
.with_stderr_contains("[RUNNING] `rustc [..]--diagnostic-width=20[..]")
6150+
.run();
6151+
6152+
p.cargo("doc -v")
6153+
.env("__CARGO_TEST_TTY_WIDTH_DO_NOT_USE_THIS", "20")
6154+
.with_stderr_contains("[RUNNING] `rustdoc [..]--diagnostic-width=20[..]")
61506155
.run();
61516156
}
61526157

0 commit comments

Comments
 (0)