Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented May 16, 2025

Summary

As the title says, this PR adds a SourceFile field to ruff_diagnostics::Diagnostic. After #18051, this will be the main piece of information present in Message and not in ruff_diagnostics::Diagnostic, so this PR will facilitate replacing ruff_diagnostics::Diagnostic with Message.

The essential code changes are very small:

  • Adding a source_file: SourceFile field to ruff_diagnostics::Diagnostic
  • Adding a source_file: SourceFile field to Checker

Other than that, the changes are just plumbing SourceFile arguments through functions to get them to ruff_diagnostics::Diagnostic::new calls.

Test Plan

Existing tests

@ntBre ntBre added internal An internal refactor or improvement diagnostics Related to reporting of diagnostics. labels May 16, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 16, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre force-pushed the brent/diagnostic-source-file branch from b78c67e to 26b2137 Compare May 16, 2025 21:55
@ntBre ntBre marked this pull request as ready for review May 16, 2025 22:12
@ntBre ntBre requested a review from AlexWaygood as a code owner May 16, 2025 22:12
@ntBre ntBre requested review from MichaReiser and removed request for AlexWaygood May 16, 2025 22:35
@MichaReiser
Copy link
Member

The PR summary is a bit confusing because it isn't clear to me when/if you're speaking about the ruff or linter Diagnostic type. Maybe rewrite it to DiagnosticOld and clarify at the beginning what Diagnostic points to.

(It may be too late for this but I liked what @BurntSushi did early on in his refactor: He renamed the old Diagnostic type to OldDiagnostic. This helped clarify a lot in his PRs)

settings,
source_type,
source_kind.as_ipy_notebook().map(Notebook::cell_offsets),
source_file.clone(),
Copy link
Member

Choose a reason for hiding this comment

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

Can't we pass the file by reference instead of cloning everywhere?

@ntBre
Copy link
Contributor Author

ntBre commented May 17, 2025

Oh sorry, this PR is all about the old ruff_diagnostics::Diagnostic. I guess I only typed that out once and then dropped the prefix. The new ruff_db::Diagnostics also have a file attached, so the old ruff_diagnostics::Diagnostic type is the only one of the three (including Message) without an associated file.

I think you might be right about it being a bit late. My plan after this and #18051 is to delete ruff_diagnostics::Diagnostic (OldDiagnostic) entirely. I had a branch stacked on #18051 doing that when I realized I needed the file, which seemed good to split off.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I feel bad saying this because this must have taken you a lot of time but I feel a bit concerned about introducing a new required argument for every lint rule.

Could we take inspiration from InferContext::report_lint and e.g. introduce a checker.report_violation(violation, range) that returns a DiagnosticGuard that automatically injects the file and pushes the diagnostic on drop?

I didn't review all call sites to get a good sense if this works in all cases but it would avoid passing file everywhere

@ntBre
Copy link
Contributor Author

ntBre commented May 17, 2025

I feel bad saying this because this must have taken you a lot of time but I feel a bit concerned about introducing a new required argument for every lint rule.

Could we take inspiration from InferContext::report_lint and e.g. introduce a checker.report_violation(violation, range) that returns a DiagnosticGuard that automatically injects the file and pushes the diagnostic on drop?

I didn't review all call sites to get a good sense if this works in all cases but it would avoid passing file everywhere

No worries, I was concerned about that too. I'll take a look at InferContext::report_lint!

It definitely won't work everywhere, at least as a method on Checker, because we don't have a Checker available in a surprising number of cases. However, we still can pass a Checker to some subset of those. That's actually what I did first before switching to pass only the SourceFile. So I think it will still be useful in the vast majority of cases.

@MichaReiser
Copy link
Member

It definitely won't work everywhere, at least as a method on Checker, because we don't have a Checker available in a surprising number of cases. However, we still can pass a Checker to some subset of those. That's actually what I did first before switching to pass only the SourceFile. So I think it will still be useful in the vast majority of cases.

I'm fine passing SourceFile in places where we don't have a Checker. I think some of those have a context parameter on which we can store it instead.

@ntBre ntBre mentioned this pull request May 19, 2025
@ntBre
Copy link
Contributor Author

ntBre commented May 19, 2025

I'm going to go ahead and close this one instead of trying to rebase onto #18051. I'll put up a second attempt with a new Checker method for constructing diagnostics after I study InferContext a bit.

@ntBre ntBre closed this May 19, 2025
@ntBre ntBre deleted the brent/diagnostic-source-file branch May 19, 2025 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics. internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants