Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Mar 18, 2025

Summary

This PR updates check_path in the ruff_linter crate to return a Vec<Message> instead of a Vec<Diagnostic>. The main motivation for this is to make it easier to convert semantic syntax errors directly into Messages rather than Diagnostics in #16106. However, this also has the benefit of keeping the preview check on unsupported syntax errors in check_path, as suggested in #16429 (comment).

All of the interesting changes are in the first commit. The second commit just renames variables like diagnostics to messages, and the third commit is a tiny import fix.

I also updated the ExpandedMessage::location field name, which caused a few extra commits tidying up the playground code. I thought it was nicely symmetric with end_location, but I'm happy to revert that too.

Test Plan

Existing tests. I also tested the playground and server manually.

ntBre added 3 commits March 18, 2025 16:20
Summary
--

This PR updates `check_path` in the `ruff_linter` crate to return a
`Vec<Message>` instead of a `Vec<Diagnostic>`. The main motivation for this is
to make it easier to convert semantic syntax errors directly into `Message`s
rather than `Diagnostic`s in #16106. However, this also has the benefit of
keeping the preview check on unsupported syntax errors in `check_path`, as
suggested in https://github.com/astral-sh/ruff/pull/16429/files#r1974748024.

Test Plan
--

Existing tests. I also tested the playground and server manually.
I tried to preserve `diagnostic` where `Message::as_diagnostic_message` or
`Message::into_diagnostic_message` were called but otherwise convert everything
to `message`.
@ntBre ntBre added the internal An internal refactor or improvement label Mar 18, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 18, 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.

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.

This is a great improvement. I also think that it will make Andrew's diagnostic refactor easier because check_path. Thanks for working on this.

range: edit.range(),
fix: Some(Fix::safe_edit(edit)),
parent: None,
file: SourceFileBuilder::new("<filename>", "<code>").finish(),
Copy link
Member

Choose a reason for hiding this comment

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

While the source text doesn't matter here, I'd still prefer to pass it in because the test here otherwise start panicking (at best) or produce nonsensical results) if apply_fixes ever starts accessing file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you suggesting passing in a placeholder like "<filename>" but just requiring the caller to make that choice? It looks like we don't actually have a filename where this function is used, unlike message_from_diagnostic. We do have a Locator we could extract the source code argument from, though.

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 made this function take both a filename: &str and a source: &str to pass to the SourceFileBuilder and called the function throughout the tests module with "<filename>", locator.contents(), .... Happy to revisit it if that's not what you had in mind, though.

fixes: applied,
source_map,
}) = fix_file(&diagnostics, &locator, unsafe_fixes)
}) = fix_file(&messages, &locator, unsafe_fixes)
Copy link
Member

Choose a reason for hiding this comment

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

Did this PR just open up the possibility for the parser to have fixes :)

Comment on lines 320 to 322
let noqa_start = diagnostic.range.start();
diagnostic.file = source_file.clone();
diagnostic.noqa_offset = noqa_start;
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need the noqa offset mapping? Shouldn't diagnostic.file and diagnostic.noqa_offset already have the right file and offset?

Copy link
Contributor Author

@ntBre ntBre Mar 19, 2025

Choose a reason for hiding this comment

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

Ah I think you're right. Tests were failing because some of them truncate the input path to just the file name, but I think the cause ended up being somewhere else. We can actually just print all of the messages here, and the tests still pass, but I'll leave the filter on DiagnosticMessages for now.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

This looks great! Thanks for doing this.

@ntBre ntBre merged commit 22de00d into main Mar 19, 2025
22 checks passed
@ntBre ntBre deleted the brent/check-path-messages branch March 19, 2025 14:08
ntBre added a commit that referenced this pull request May 1, 2025
Summary
--

Deletes the unused `parent` field on two structs, which should make converting
these to the new `Diagnostic` type a little easier.

It looks like I accidentally added both of these in #16837 in an early commit
that I only partially reverted, so they have never actually been used.

Test Plan
--

Existing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants