-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
New feature panic color errors #125614
New feature panic color errors #125614
Conversation
Co-authored-by: Henrique Carrão <henrique.carrao@tecnico.ulisboa.pt>
Co-authored-by: Henrique Carrão <henrique.carrao@tecnico.ulisboa.pt>
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BoxyUwU (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Hi, thank you for your PR! There are a few improvements that need to be done to the implementation, but before addressing that I think it makes more sense to first figure out whether we want this in the first place. |
r? libs |
@@ -213,6 +213,8 @@ declare_features! ( | |||
(internal, negative_bounds, "1.71.0", None), | |||
/// Allows using `#[omit_gdb_pretty_printer_section]`. | |||
(internal, omit_gdb_pretty_printer_section, "1.5.0", None), | |||
/// Highlights the error message of pannic calls. | |||
(unstable, panic_color_errors,"CURRENT_RUSTC_VERSION", None), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only necessary for compiler features. Library features like this PR should not get an entry in the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so its only that part or there's more we should delete?
Actually, we found this guide somewhat confusing regarding library features. The link it provides for the library features guide leads only to stability attributes. As a result, we followed the guide for compiler features instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should really link to https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html
#[unstable(feature = "panic_color_errors", issue = "none")] | ||
pub fn highlight_errors(set_color: Option<bool>){ | ||
match set_color { | ||
Some(true) => {crate::env::set_var("RUST_COLOR_ERRORS", "1")} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does CI not fail on this formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong here?
@@ -213,6 +213,8 @@ declare_features! ( | |||
(internal, negative_bounds, "1.71.0", None), | |||
/// Allows using `#[omit_gdb_pretty_printer_section]`. | |||
(internal, omit_gdb_pretty_printer_section, "1.5.0", None), | |||
/// Highlights the error message of pannic calls. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Highlights the error message of pannic calls. | |
/// Highlights the error message of panic calls. |
library/std/src/panicking.rs
Outdated
@@ -233,6 +235,42 @@ where | |||
*hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info))); | |||
} | |||
|
|||
/// Adjusts the visual representation of panic messages by enabling or | |||
/// disabling color highlighting, or leaving it with a default behaviour. This | |||
/// function sets, unsets or remove an environment variable (RUST_COLOR_ERRORS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// function sets, unsets or remove an environment variable (RUST_COLOR_ERRORS) | |
/// function sets, unsets or removes an environment variable (RUST_COLOR_ERRORS) |
|
||
------------------------ | ||
|
||
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is highlight the errors when there's a terminal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is highlight the errors when there's a terminal | |
With this feature enabled, error messages generated during panics are highlighted for improved visibility and quick identification. By employing color highlighting, developers can easily distinguish error messages from other output, making debugging more efficient and intuitive. This is optional and can be toggled on or off using a dedicated function. The default behaviour is to highlight the errors when there's a terminal |
Another environment variable? Regardless, waiting on like, an ACP or something. @rustbot author |
We forgot to address it but there was an issue regarding this feature. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have reviewed this more closely and am recommending that T-libs-api reject this PR in its current state unless its internals are considerably redesigned.
Some(true) => {crate::env::set_var("RUST_COLOR_ERRORS", "1")} | ||
Some(false) => {crate::env::set_var("RUST_COLOR_ERRORS", "0")} | ||
_ => {crate::env::remove_var("RUST_COLOR_ERRORS")} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anomalous formatting.
Some(true) => {crate::env::set_var("RUST_COLOR_ERRORS", "1")} | |
Some(false) => {crate::env::set_var("RUST_COLOR_ERRORS", "0")} | |
_ => {crate::env::remove_var("RUST_COLOR_ERRORS")} | |
Some(true) => crate::env::set_var("RUST_COLOR_ERRORS", "1"), | |
Some(false) => crate::env::set_var("RUST_COLOR_ERRORS", "0"), | |
_ => crate::env::remove_var("RUST_COLOR_ERRORS"), |
@@ -233,6 +235,42 @@ where | |||
*hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info))); | |||
} | |||
|
|||
/// Adjusts the visual representation of panic messages by enabling or | |||
/// disabling color highlighting, or leaving it with a default behaviour. This | |||
/// function sets, unsets or removes an environment variable (RUST_COLOR_ERRORS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// function sets, unsets or removes an environment variable (RUST_COLOR_ERRORS) | |
/// function sets, unsets or removes an environment variable (`RUST_COLOR_ERRORS`) |
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
panic!("Oops sometging went wrong"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
panic!("Oops sometging went wrong"); | |
panic!("Oops something went wrong"); |
|
||
fn foo(x: u64) { | ||
if x == 0 { | ||
panic!("Oops sometging went wrong"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
panic!("Oops sometging went wrong"); | |
panic!("Oops something went wrong"); |
pub fn highlight_errors(set_color: Option<bool>){ | ||
match set_color { | ||
Some(true) => {crate::env::set_var("RUST_COLOR_ERRORS", "1")} | ||
Some(false) => {crate::env::set_var("RUST_COLOR_ERRORS", "0")} | ||
_ => {crate::env::remove_var("RUST_COLOR_ERRORS")} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both set_var
and remove_var
are unsafe fn
because they update the environment in an unsynchronized way, but this function is marked as safe. We cannot accept this into the standard library.
This requires an API design that does not depend on racily setting the environment variable.
@@ -259,7 +297,10 @@ fn default_hook(info: &PanicInfo<'_>) { | |||
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>"); | |||
|
|||
let write = |err: &mut dyn crate::io::Write| { | |||
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}"); | |||
// We should check if the feature is active and only then procede to format the message, but we dont know how to do it for now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// We should check if the feature is active and only then procede to format the message, but we dont know how to do it for now. | |
// We should check if the feature is active and only then proceed to format the message, but we dont know how to do it for now. |
match crate::env::var_os("RUST_COLOR_ERRORS") { | ||
Some(x) if x == "1" => format!("\x1b[31m{msg}\x1b[0m"), | ||
None => { | ||
if io::stderr().is_terminal() { | ||
format!("\x1b[31m{msg}\x1b[0m") | ||
} | ||
else { | ||
msg.to_string() | ||
} | ||
}, | ||
_ => msg.to_string() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not need to allocate to function. It is desirable if backtraces minimize the number of allocations they perform, even with std enabled. It should instead write to a formatter.
/// highlighting otherwise. | ||
#[unstable(feature = "panic_color_errors", issue = "none")] | ||
fn format_error_message (msg: &str) -> String { | ||
match crate::env::var_os("RUST_COLOR_ERRORS") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should cache this instead of checking it every time, as we do with most env vars.
☔ The latest upstream changes (presumably #127397) made this pull request unmergeable. Please resolve the merge conflicts. |
@Vudvud any updates on creating the ACP? thanks |
We have introduced a new feature in Rust that enhances the visibility of errors in backtraces by highlighting them in red. This improvement aims to make debugging easier by drawing immediate attention to errors. The color highlighting is enabled by default when the output is directed to a terminal. However, we also provide flexibility through an environment variable and a function to control this behavior programmatically.