-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
refactor: Move diagnostic printing to Shell #13813
Conversation
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.
Overall looks good to me, especially for the prevention of double borrowing RefCell
!
However, I wonder if we need diagnostic with to be computed every time before Cargo prints messages. I guess the answer is no, we don't care people resize their terminal width during a build.
I'm a bit uncomfortable generating this in As for the terminal width changing, that is more likely to come up when it comes to So far, a |
73f7796
to
1601b6e
Compare
src/cargo/util/context/mod.rs
Outdated
@@ -412,6 +413,22 @@ impl GlobalContext { | |||
self.shell.borrow_mut() | |||
} | |||
|
|||
/// Prints the passed in [Message] to stderr | |||
pub fn renderer_diagnostic(&self, message: Message<'_>) -> std::io::Result<()> { |
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.
Any reason that this method is on GlobalContext
instead of Shell
?
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.
None that I can think of, I went ahead and moved it to Shell
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.
One of the problems with #12875 is that it put diagnostic tracking in Shell
. For me, I see Shell
as lower level than that. I feel like putting diagnotic rendering in Shell
could encourage that kind of thinking.
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 agree diagnostic tracking should not be in Shell
, but I am not as worried about encouraging that thinking with this change. This change moves to a function where things get rendered and then emitted, similar to:
Lines 387 to 393 in ba6fb40
pub fn print_json<T: serde::ser::Serialize>(&mut self, obj: &T) -> CargoResult<()> { | |
// Path may fail to serialize to JSON ... | |
let encoded = serde_json::to_string(&obj)?; | |
// ... but don't fail due to a closed pipe. | |
drop(writeln!(self.out(), "{}", encoded)); | |
Ok(()) | |
} |
The JSON gets "rendered" by serde_json::to_string
and then printed to stdout
.
The fact Renderer
gets created inside of emit_diagnostic
is an implementation detail.
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.
@epage do you consider it as a blocker? I am fine with either way.
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 agree diagnostic tracking should not be in Shell, but I am not as worried about encouraging that thinking with this change.
By putting it here, it implicitly states diagnostics live here and it is the starting point for any thought process on how to evolve this code.
do you consider it as a blocker? I am fine with either way.
I'm mixed. In terms of "this is all internal and easily changed", its not a blocker. It just sets us down the wrong direction for future changes unless someone thinks beyond the code in front of them which we don't always do.
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.
Maybe we could put some design principles in the module-level for shell, so at least this discussion won't just disappear in the air.
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.
The current naming is low level enough that I'm fine with moving this forward
1601b6e
to
986a3ee
Compare
986a3ee
to
7d45802
Compare
7d45802
to
6fb7a9b
Compare
src/cargo/core/shell.rs
Outdated
@@ -1,3 +1,4 @@ | |||
use annotate_snippets::{Message, Renderer}; |
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.
nit: we don't really have a style convention for imports, though I believe if this was added by auto-import via Rust Analyzer, it would be grouped together with anstream
and `anstyle.
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.
Nice catch! Normally, things get added correctly; I'm not sure why it didn't in this case.
I wish group_imports
and imports_granularity
were stable so we could use them.
6fb7a9b
to
5a22441
Compare
5a22441
to
5415b04
Compare
@bors r+ Thank you all :) |
☀️ Test successful - checks-actions |
Update cargo 15 commits in b60a1555155111e962018007a6d0ef85207db463..6087566b3fa73bfda29702632493e938b12d19e5 2024-04-26 16:37:29 +0000 to 2024-04-30 20:45:20 +0000 - fix(cargo-fix): dont fix into standard library (rust-lang/cargo#13792) - refactor: Move diagnostic printing to Shell (rust-lang/cargo#13813) - Populate git information when building Cargo from Rust's source tarball (rust-lang/cargo#13832) - docs: fix several typos found by `typos-cli` (rust-lang/cargo#13831) - fix(alias): Aliases without subcommands should not panic (rust-lang/cargo#13819) - fix(toml): Improve granularity of traces (rust-lang/cargo#13830) - fix(toml): Warn, rather than fail publish, if a target is excluded (rust-lang/cargo#13713) - test(cargo-lints): Add a test to ensure cap-lints works (rust-lang/cargo#13829) - fix(toml)!: Remove support for inheriting badges (rust-lang/cargo#13788) - chore(ci): Don't check `cargo` against beta channel (rust-lang/cargo#13827) - Fix target entry in .gitignore (rust-lang/cargo#13817) - Bump to 0.81.0; update changelog (rust-lang/cargo#13823) - Add failing test: artifact_dep_target_specified (rust-lang/cargo#13816) - fix(cargo-lints): Don't always inherit workspace lints (rust-lang/cargo#13812) - Update SleepTraker returns_in_order unit test (rust-lang/cargo#13811) r? ghost
As I have been working on cargo's diagnostic system, I have disliked copying around the code for a new
Renderer
, and then emitting the diagnostic:Moving to a method on
Shell
helps mitigate the risk of updating duplicate code and missing one. It should also make it nearly impossible to get into scenarios wheregctx.shell()
is borrowed twice, leading to panics. This problem was one I ran into early on as I tried to write messages tostderr
like so: