Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Better handling for panics during linting#20252

Closed
amyreese wants to merge 3 commits intogh/amyreese/2/basefrom
gh/amyreese/2/head
Closed

Better handling for panics during linting#20252
amyreese wants to merge 3 commits intogh/amyreese/2/basefrom
gh/amyreese/2/head

Conversation

@amyreese
Copy link
Member

@amyreese amyreese commented Sep 4, 2025

Stack from ghstack (oldest at bottom):


  • Convert panics to diagnostics with id Panic, severity Fatal, and
    the error as the diagnostic message, annotated with a Span with
    empty code block and no range.
  • Updates the post-linting message diagnostic handling to track the
    maximum severity seen, and then prints the "report a bug in ruff"
    message only if the max severity was Fatal

This depends on the sorting changes since it creates diagnostics with no
range specified.

[ghstack-poisoned]
amyreese added a commit that referenced this pull request Sep 4, 2025
- Convert panics to diagnostics with id `Panic`, severity `Fatal`, and
  the error as the diagnostic message, annotated with a `Span` with
  empty code block and no range.
- Updates the post-linting message diagnostic handling to track the
  maximum severity seen, and then prints the "report a bug in ruff"
  message only if the max severity was `Fatal`

This depends on the sorting changes since it creates diagnostics with no
range specified.


ghstack-source-id: cf7173c
Pull-Request: #20252
[ghstack-poisoned]
amyreese added a commit that referenced this pull request Sep 4, 2025
- Convert panics to diagnostics with id `Panic`, severity `Fatal`, and
  the error as the diagnostic message, annotated with a `Span` with
  empty code block and no range.
- Updates the post-linting message diagnostic handling to track the
  maximum severity seen, and then prints the "report a bug in ruff"
  message only if the max severity was `Fatal`

This depends on the sorting changes since it creates diagnostics with no
range specified.

ghstack-source-id: 77a0547
Pull-Request: #20252
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! I had a few suggestions, mostly based on the related code in ty. I'd also be interested in adding a panicking test rule so that we can see this in action. You can see some examples of those here:

/// Fake rules for testing Ruff's behavior
///
/// All of these rules should be assigned to the RUF9XX codes.

Comment on lines 202 to 207
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should follow ty again here, or maybe even factor out a common panic Diagnostic constructor:

let mut diagnostic = Diagnostic::new(DiagnosticId::Panic, Severity::Fatal, message);
diagnostic.sub(SubDiagnostic::new(
SubDiagnosticSeverity::Info,
"This indicates a bug in ty.",
));
let report_message = "If you could open an issue at https://github.com/astral-sh/ty/issues/new?title=%5Bpanic%5D, we'd be very appreciative!";
diagnostic.sub(SubDiagnostic::new(
SubDiagnosticSeverity::Info,
report_message,
));

Some of the code for constructing the main message might even be helpful for us too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the benefit of putting this outside the diagnostic is that we get the clear error message at the bottom after the diagnostic messages, cluing the user into the existence of a panic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right, I was still thinking that we'd sort by ascending severity, like in RenderingSortKey, so that the panics would be at the end, but that makes sense if we don't want to sort.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh that's not the main comparison in RenderingSortKey anyway, hmm. It looks like ty has a relatively short logging/tracing message but still most of this in the diagnostic:

ruff/crates/ty/src/lib.rs

Lines 365 to 369 in df142de

if max_severity.is_fatal() {
tracing::warn!(
"A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details."
);
}

@amyreese
Copy link
Member Author

amyreese commented Sep 4, 2025

Thanks! I had a few suggestions, mostly based on the related code in ty. I'd also be interested in adding a panicking test rule so that we can see this in action.

I've got a commit in progress to do that, but I need to figure out how to get that panic to only happen on the right inputs or whatever, because otherwise it breaks/ruins all the existing text rule fixtures/snapshots. But also the current test rule doesn't go through the same code path, so it ends up creating a ruff crashed panic instead.

[ghstack-poisoned]
amyreese added a commit that referenced this pull request Sep 4, 2025
- Convert panics to diagnostics with id `Panic`, severity `Fatal`, and
  the error as the diagnostic message, annotated with a `Span` with
  empty code block and no range.
- Updates the post-linting message diagnostic handling to track the
  maximum severity seen, and then prints the "report a bug in ruff"
  message only if the max severity was `Fatal`

This depends on the sorting changes since it creates diagnostics with no
range specified.

ghstack-source-id: 8cda710
Pull-Request: #20252
@amyreese amyreese closed this Sep 5, 2025
@amyreese amyreese deleted the gh/amyreese/2/head branch November 25, 2025 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments