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

Replace all ansi_term crates #2923

Merged
merged 19 commits into from
Jul 23, 2024
Merged

Replace all ansi_term crates #2923

merged 19 commits into from
Jul 23, 2024

Conversation

jasl
Copy link
Contributor

@jasl jasl commented Jan 13, 2024

Now Polkadot-SDK is ansi_term free

@jasl jasl requested a review from koute as a code owner January 13, 2024 15:34
@@ -244,10 +243,9 @@ mod time {
T: FormatTime,
{
if with_ansi {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
write!(writer, "\x1B[2m")?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The newer tracing-subscriber has removed with_ansi format support.
to keep it simple, I copy the dimmed style and replace its usage in write!

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't allow to get prefix and suffix.

timer.format_time(writer)?; will do write! so we can't wrap it with style here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I see what you mean now!

@jasl
Copy link
Contributor Author

jasl commented Jan 13, 2024

I see the newer tracing-subscriber uses nu-ansi-term, which is a fork of the retired ansi_term crate.
What do you think about using nu-ansi-term instead of console?

@bkchr
Copy link
Member

bkchr commented Jul 17, 2024

@jasl could you fix the merge conflicts?

@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

@jasl could you fix the merge conflicts?

I can, but I'm also thinking, what if we replace console with nu_ansi_term ?
because nu_ansi_term is 100% compatible with the obsoleted ansi_term, and a few dependencies use it, too.

So, if we switch to nu_ansi_term, we can simplify dependencies a bit, and there's no feature or performance difference.

What do you think?

@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

I just searched Cargo.toml, only tracing-subscriber uses nu-ansi-term, rebase this one seems OK

jasl added 4 commits July 18, 2024 02:06
# Conflicts:
#	Cargo.lock
#	substrate/client/informant/Cargo.toml
#	substrate/client/informant/src/display.rs
#	substrate/client/informant/src/lib.rs
#	substrate/client/tracing/Cargo.toml

Fix compile

Fix compile

Fix compile
# Conflicts:
#	substrate/client/tracing/Cargo.toml
color: impl Into<ColorOrStyle>,
data: impl ToString,
) -> impl Display {
fn print(&self, color: Color, attr: Option<Attribute>, data: impl Display) -> impl Display {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bkchr console API design is not good here. Do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

What is the problem here? I don't see it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In ansi_term, the function can just pass Colour::Red.bold() to get red and bold style, but console doesn't provide such fluent API, I separate color and attribute, and because there are usages do not have attribute, so I have to make it optional which feels not good than previous one

Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine. This is some internal api any way.

@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

Note: bridges/relay uses ansi_term, I will make another PR later

@bkchr
Copy link
Member

bkchr commented Jul 17, 2024

I can, but I'm also thinking, what if we replace console with nu_ansi_term ?

So, you don't want to do this? I'm fine with either way.

@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

I can, but I'm also thinking, what if we replace console with nu_ansi_term ?

So, you don't want to do this? I'm fine with either way.

I decide to move forward with this one.

@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

PR doc attached

substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
@@ -244,10 +243,9 @@ mod time {
T: FormatTime,
{
if with_ansi {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
write!(writer, "\x1B[2m")?;
Copy link
Member

Choose a reason for hiding this comment

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

@bkchr bkchr requested a review from a team July 17, 2024 19:31
@bkchr bkchr added the T0-node This PR/Issue is related to the topic “node”. label Jul 17, 2024
Co-authored-by: Bastian Köcher <git@kchr.de>
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

I just realized that we don't need all these to_string. Sorry 🙈

substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
@@ -244,10 +243,9 @@ mod time {
T: FormatTime,
{
if with_ansi {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
write!(writer, "\x1B[2m")?;
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I see what you mean now!

Co-authored-by: Bastian Köcher <git@kchr.de>
@jasl
Copy link
Contributor Author

jasl commented Jul 17, 2024

@bkchr Do I need to send PR for the bridge directly to the repo? I remember they have a standalone repo (?).

@bkchr
Copy link
Member

bkchr commented Jul 17, 2024

No the standalone repo doesn't exist anymore.

@bkchr bkchr enabled auto-merge July 19, 2024 09:38
Copy link
Contributor

@lexnv lexnv left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing!

@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: test-linux-stable 2/3
Logs: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/6749196

@jasl
Copy link
Contributor Author

jasl commented Jul 19, 2024

The CI fails seems unrelated

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 13 filtered out; finished in 72.00s
--- TRY 1 STDERR:        staging-node-cli::basic wasm_big_block_import_fails ---
2024-07-19T09:54:49.791584Z ERROR runtime::timestamp: `pallet_timestamp::UnixTime::now` is called at genesis, invalid value returned: 0    
thread 'wasm_big_block_import_fails' panicked at substrate/primitives/io/src/lib.rs:1567:36:
Failed to allocate memory: "Allocator ran out of space"

UPDATE:

--- TRY 6 STDERR:        sc-tracing logging::tests::control_characters_are_always_stripped_out_from_the_log_messages ---
thread 'logging::tests::control_characters_are_always_stripped_out_from_the_log_messages' panicked at substrate/client/tracing/src/logging/mod.rs:680:13:
assertion failed: stderr.contains("\x1B[31mERROR\x1B[0m")

It seems my PR changed the output style, let me dig it

auto-merge was automatically disabled July 19, 2024 14:41

Head branch was pushed to by a user without write access

@bkchr bkchr enabled auto-merge July 19, 2024 14:50
@jasl
Copy link
Contributor Author

jasl commented Jul 19, 2024

@bkchr Sorry to bother you again... when I checked the test fails, I saw console has a global ANSI style control

See https://github.com/console-rs/console/blob/master/src/utils.rs#L42-L49 and https://github.com/console-rs/console/blob/master/src/utils.rs#L625-L632

If we use the console style control, we don't need to pass enable_color and custom output format, we can remove all relates codes, do you suggest I append the change to the PR, or make a following PR?

I already prepared the code

BTW, the CI fails shows I need to get approve from
@bkontur @serban300 @acatangiu
Could you help review this PR?

auto-merge was automatically disabled July 20, 2024 05:35

Head branch was pushed to by a user without write access

@jasl
Copy link
Contributor Author

jasl commented Jul 20, 2024

@bkchr
Since I need to get approval from bridge devs,
I pushed a refactor here. We no longer need manual ANSI-style stripping, which can simplify the implementations

Sorry for you have to review one more time

image

@@ -652,36 +654,4 @@ mod tests {
assert!(stderr.contains(&line));
}
}

#[test]
fn control_characters_are_always_stripped_out_from_the_log_messages() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The case is impossible now, so the test is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bkchr If we only use console to style strings, when it renders ANSI strings, it will check the global colors_enabled() and colors_enabled_stderr(), if they're false, it will skip control codes.

So we don't need to strip ANSI control codes because they will not be rendered if colors diasbled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only restriction is we must style strings using console only, it doesn't take care of other sources of ANSI control codes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this case, console won't sanitize the RAW_LINE.
But if we only use console, it is impossible to get a string with ANSI control codes.

prdoc/pr_2923.prdoc Show resolved Hide resolved
substrate/client/informant/src/display.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/display.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/display.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/display.rs Outdated Show resolved Hide resolved
substrate/client/informant/src/lib.rs Outdated Show resolved Hide resolved
substrate/client/tracing/src/logging/event_format.rs Outdated Show resolved Hide resolved
substrate/client/tracing/src/logging/mod.rs Outdated Show resolved Hide resolved
substrate/client/tracing/src/logging/mod.rs Show resolved Hide resolved
}

// NOTE: When making any changes here make sure to also change this function in `sp-panic-handler`.
fn strip_control_codes(input: &str) -> std::borrow::Cow<str> {
Copy link
Member

Choose a reason for hiding this comment

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

We should not remove this code.

Copy link
Member

Choose a reason for hiding this comment

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

Or is console taking care of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to https://github.com/console-rs/console/blob/master/src/utils.rs#L625-L632
When console renders a styled string, it will check the global colors_enabled() and colors_enabled_stderr().
If false, it will skip rendering ANSI control codes.

So I think console taking care of this.

There is only one restriction we need to ensure we only use console for the ANSI styling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this change OK?

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'm not sure what you mean.
Do you mean the case that runtime panicked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Member

Choose a reason for hiding this comment

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

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 have a question:
Why need to call strip_control_codes(msg) in panic_hook() of sp-panic-handler?
The PanicInfo#payload() should be the argument of panic!. When will it contain ANSI control codes?

Copy link
Member

Choose a reason for hiding this comment

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

AFAIR it is just a protection against someone being able to trigger a panic and putting ANSI control codes into it. "It is possible", but very unlikely.

Co-authored-by: Bastian Köcher <git@kchr.de>
@jasl
Copy link
Contributor Author

jasl commented Jul 23, 2024

Kindly ping @bkontur @serban300 @acatangiu
Could you help to review my PR?

@jasl
Copy link
Contributor Author

jasl commented Jul 23, 2024

@bkchr required reviewers have been approved and CI green.

For the remaining questions, do you have a conclusion?

@bkchr bkchr added this pull request to the merge queue Jul 23, 2024
Merged via the queue into paritytech:master with commit bbb8668 Jul 23, 2024
160 of 163 checks passed
@bkchr
Copy link
Member

bkchr commented Jul 23, 2024

Ty @jasl for the PR and all the work.

@jasl jasl deleted the replace-ansi_term branch July 23, 2024 16:58
TarekkMA pushed a commit to moonbeam-foundation/polkadot-sdk that referenced this pull request Aug 2, 2024
Now Polkadot-SDK is ansi_term free

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T0-node This PR/Issue is related to the topic “node”.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants