diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs
index 0bd98340d06..74b9e436eb1 100644
--- a/src/bin/cargo/cli.rs
+++ b/src/bin/cargo/cli.rs
@@ -37,6 +37,7 @@ Available unstable (nightly-only) flags:
-Z timings -- Display concurrency information
-Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
-Z crate-versions -- Add crate versions to generated docs
+ -Z terminal-width -- Provide a terminal width to rustc for error truncation
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
);
diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 9399c50428c..91d444cce05 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -47,6 +47,7 @@ pub use self::lto::Lto;
use self::output_depinfo::output_depinfo;
use self::unit_graph::UnitDep;
pub use crate::core::compiler::unit::{Unit, UnitInterner};
+use crate::core::features::nightly_features_allowed;
use crate::core::manifest::TargetSourcePath;
use crate::core::profiles::{PanicStrategy, Profile, Strip};
use crate::core::{Edition, Feature, PackageId, Target};
@@ -709,6 +710,7 @@ fn add_error_format_and_color(
// to emit a message that cargo will intercept.
json.push_str(",artifacts");
}
+
match cx.bcx.build_config.message_format {
MessageFormat::Short | MessageFormat::Json { short: true, .. } => {
json.push_str(",diagnostic-short");
@@ -716,6 +718,26 @@ fn add_error_format_and_color(
_ => {}
}
cmd.arg(json);
+
+ if nightly_features_allowed() {
+ let config = cx.bcx.config;
+ match (
+ config.cli_unstable().terminal_width,
+ config.shell().err_width().diagnostic_terminal_width(),
+ ) {
+ // Terminal width explicitly provided - only useful for testing.
+ (Some(Some(width)), _) => {
+ cmd.arg(format!("-Zterminal-width={}", width));
+ }
+ // Terminal width was not explicitly provided but flag was provided - common case.
+ (Some(None), Some(width)) => {
+ cmd.arg(format!("-Zterminal-width={}", width));
+ }
+ // User didn't opt-in.
+ _ => (),
+ }
+ }
+
Ok(())
}
diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs
index 8ed33b2f615..47d5f9cd9e6 100644
--- a/src/cargo/core/features.rs
+++ b/src/cargo/core/features.rs
@@ -357,6 +357,7 @@ pub struct CliUnstable {
pub separate_nightlies: bool,
pub multitarget: bool,
pub rustdoc_map: bool,
+ pub terminal_width: Option