Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the current shell width to rustc #7315

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub use self::layout::is_bad_artifact_name;
use self::output_depinfo::output_depinfo;
use self::unit_dependencies::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::{Lto, PanicStrategy, Profile};
use crate::core::Feature;
Expand Down Expand Up @@ -714,6 +715,14 @@ fn add_error_format_and_color(
} else {
let mut color = true;
match cx.bcx.build_config.message_format {
MessageFormat::Human if nightly_features_allowed() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this changed it to unconditionally enable it on nightly. We haven't really done that before, but I don't really have an objection to it. We have problems with figuring out how to get people to test unstable flags, so unconditionally enabling it on nightly might be a good idea, since it is just a visual change. I'd like to hear from other cargo members, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late repsponse, but I think I'd prefer if this still needed to be opted-in to if that's ok, because otherwise if -Zterminal-width changes it'd break all nightly users by default until we can ship a fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

if let (Some(width), _) | (_, Some(width)) = (
cx.bcx.config.cli_unstable().terminal_width,
cx.bcx.config.shell().accurate_err_width(),
) {
cmd.arg(format!("-Zterminal-width={}", width));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this isn't included in the pipeline path? Does it not affect the rendered field? We plan on moving all interaction through the json path, so this may not work this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the rustc side ignores this codepath for the rendered field in json output, which is unfortunate for the integration here. We'll likely need some back and forth with the other repo to get both to be in the same page about this.

}
}
MessageFormat::Human => (),
MessageFormat::Json {
ansi,
Expand Down
12 changes: 12 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ pub struct CliUnstable {
pub build_std: Option<Vec<String>>,
pub timings: Option<Vec<String>>,
pub doctest_xcompile: bool,
pub terminal_width: Option<usize>,
}

impl CliUnstable {
Expand Down Expand Up @@ -376,6 +377,16 @@ impl CliUnstable {
}
}

fn parse_usize_opt(value: Option<&str>) -> CargoResult<Option<usize>> {
Ok(match value {
Some(value) => match value.parse::<usize>() {
Ok(value) => Some(value),
Err(e) => failure::bail!("expected a number, found: {}", e),
},
None => None,
})
}

match k {
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
"unstable-options" => self.unstable_options = true,
Expand All @@ -395,6 +406,7 @@ impl CliUnstable {
}
"timings" => self.timings = Some(parse_timings(v)),
"doctest-xcompile" => self.doctest_xcompile = true,
"terminal-width" => self.terminal_width = parse_usize_opt(v)?,
_ => failure::bail!("unknown `-Z` flag specified: {}", k),
}

Expand Down
20 changes: 20 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ impl Shell {
}
}

/// Returns the width of the terminal in spaces, if any. Always `None` in Windows.
pub fn accurate_err_width(&self) -> Option<usize> {
match self.err {
ShellOut::Stream { tty: true, .. } => imp::accurate_stderr_width(),
_ => None,
}
}

/// Returns `true` if stderr is a tty.
pub fn is_err_tty(&self) -> bool {
match self.err {
Expand Down Expand Up @@ -383,6 +391,10 @@ mod imp {

use super::Shell;

pub fn accurate_stderr_width() -> Option<usize> {
stderr_width()
}

pub fn stderr_width() -> Option<usize> {
unsafe {
let mut winsize: libc::winsize = mem::zeroed();
Expand Down Expand Up @@ -414,6 +426,10 @@ mod imp {
mod imp {
pub(super) use super::default_err_erase_line as err_erase_line;

pub fn accurate_stderr_width() -> Option<usize> {
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit unfortunate to unconditionally disable on Windows. Perhaps imp::stderr_width() could return an enum with the three states (notty, known, unknown?)? Then maybe it could have a method to convert it to Option<usize> and take care of converting unknown to 60.

}

pub fn stderr_width() -> Option<usize> {
None
}
Expand All @@ -431,6 +447,10 @@ mod imp {

pub(super) use super::default_err_erase_line as err_erase_line;

pub fn accurate_stderr_width() -> Option<usize> {
None
}

pub fn stderr_width() -> Option<usize> {
unsafe {
let stdout = GetStdHandle(STD_ERROR_HANDLE);
Expand Down
35 changes: 33 additions & 2 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use cargo::util::paths::dylib_path_envvar;
use cargo_test_support::paths::{root, CargoPathExt};
use cargo_test_support::registry::Package;
use cargo_test_support::{
basic_bin_manifest, basic_lib_manifest, basic_manifest, main_file, project, rustc_host,
sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, main_file, project,
rustc_host, sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
};
use std::env;
use std::fs::{self, File};
Expand Down Expand Up @@ -4723,3 +4723,34 @@ d
)
.run();
}

#[cargo_test]
fn simple_terminal_width() {
if !is_nightly() {
// --terminal-width is unstable
return;
}
let p = project()
.file(
"src/lib.rs",
"
fn a() {
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
let _: () = 42;
}
",
)
.build();

p.cargo("build -Zterminal-width=60")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr_contains("3 | ...t _: () = 42;")
.run();
}