Skip to content

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 26, 2025

Summary

This PR adds support for publishing diagnostics from the ty language server.

It only adds support for it for text documents and not notebook documents because the server doesn't have full notebook support yet.

Closes: astral-sh/ty#79

Test Plan

Testing this out in Helix and Zed since those are the two editors that I know of that doesn't support pull diagnostics:

Helix

Screen.Recording.2025-05-26.at.10.36.21.mov

Zed

Screen.Recording.2025-05-26.at.10.30.22.mov

@dhruvmanila dhruvmanila added server Related to the LSP server ty Multi-file analysis & type inference labels May 26, 2025
Copy link
Contributor

github-actions bot commented May 26, 2025

mypy_primer results

No ecosystem changes detected ✅

@dhruvmanila dhruvmanila force-pushed the dhruv/publish-diagnostics branch from 4ed53ac to 86bde6d Compare May 26, 2025 05:17
@dhruvmanila dhruvmanila marked this pull request as ready for review May 26, 2025 05:17
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.

Thanks. This overall looks good.

I think there's an opportunity to reduce code duplication a bit and there are some code paths that I don't understand (see inline comments)

let lsp_diagnostics = diagnostics.as_slice().iter().map(|diagnostic| {
(
// TODO: Use the cell index instead using `source_kind`
usize::default(),
Copy link
Member

@MichaReiser MichaReiser May 26, 2025

Choose a reason for hiding this comment

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

Is usize::default correct here? What is it used for? I also don't understand the comment beauase it's unclear to me where we use source kind here

Copy link
Member Author

Choose a reason for hiding this comment

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

We would need to use source_kind (which is currently commented out) to get the NotebookIndex which will be used to get the cell index in which the diagnostics occur. This index will be used to get the URI for that cell to publish the diagnostics for.

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.

Oh, we also need to think about how this integrates with didWatchedFilesChange. I don't think it's necessary to call publish diagnostic if a single python file changed because that file isn't open. But we have to re-publish all diagnostics when the configuration changed

@MichaReiser MichaReiser self-requested a review May 26, 2025 07:12
@dhruvmanila dhruvmanila force-pushed the dhruv/publish-diagnostics branch from d6b28cf to e76575a Compare May 27, 2025 00:20
@dhruvmanila
Copy link
Member Author

Video testing that any changes to configuration files refreshes the diagnostics via publish mechanism in Zed:

Screen.Recording.2025-05-27.at.13.03.29.mov

@dhruvmanila dhruvmanila force-pushed the dhruv/publish-diagnostics branch from 3794ace to fc2d725 Compare May 28, 2025 06:18
Comment on lines +93 to 97

// SAFETY: Only paths that are part of the workspace are registered for file watching.
// So, virtual paths and paths that are outside of a workspace does not trigger this
// notification.
let db = session.project_db_for_path_mut(&*root).unwrap();
Copy link
Member Author

Choose a reason for hiding this comment

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

@MichaReiser can you verify this comment?

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 accurate. It may change in the future but we'll then need to change this line :)

Comment on lines +99 to +101
let result = db.apply_changes(changes, None);

project_changed |= result.project_changed();
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 this should be fine even after we add support for multiple workspaces.

@dhruvmanila dhruvmanila requested a review from MichaReiser May 28, 2025 06:22
Comment on lines +93 to 97

// SAFETY: Only paths that are part of the workspace are registered for file watching.
// So, virtual paths and paths that are outside of a workspace does not trigger this
// notification.
let db = session.project_db_for_path_mut(&*root).unwrap();
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 accurate. It may change in the future but we'll then need to change this line :)

@dhruvmanila dhruvmanila merged commit 48c425c into main May 28, 2025
33 of 34 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/publish-diagnostics branch May 28, 2025 07:45
dcreager added a commit that referenced this pull request May 28, 2025
* main:
  [ty] Support ephemeral uv virtual environments (#18335)
  Add a `ViolationMetadata::rule` method (#18234)
  Return `DiagnosticGuard` from `Checker::report_diagnostic` (#18232)
  [flake8_use_pathlib]: Replace os.symlink with Path.symlink_to (PTH211) (#18337)
  [ty] Support cancellation and retry in the server (#18273)
  [ty] Synthetic function-like callables (#18242)
  [ty] Support publishing diagnostics in the server (#18309)
  Add Autofix for ISC003 (#18256)
  [`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`) (#18334)
  [pycodestyle] Make `E712` suggestion not assume a context (#18328)
carljm added a commit to MatthewMckee4/ruff that referenced this pull request May 28, 2025
* main: (246 commits)
  [ty] Simplify signature types, use them in `CallableType` (astral-sh#18344)
  [ty] Support ephemeral uv virtual environments (astral-sh#18335)
  Add a `ViolationMetadata::rule` method (astral-sh#18234)
  Return `DiagnosticGuard` from `Checker::report_diagnostic` (astral-sh#18232)
  [flake8_use_pathlib]: Replace os.symlink with Path.symlink_to (PTH211) (astral-sh#18337)
  [ty] Support cancellation and retry in the server (astral-sh#18273)
  [ty] Synthetic function-like callables (astral-sh#18242)
  [ty] Support publishing diagnostics in the server (astral-sh#18309)
  Add Autofix for ISC003 (astral-sh#18256)
  [`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`) (astral-sh#18334)
  [pycodestyle] Make `E712` suggestion not assume a context (astral-sh#18328)
  put similar dunder-call tests next to each other (astral-sh#18343)
  [ty] Derive `PartialOrd, Ord` for `KnownInstanceType` (astral-sh#18340)
  [ty] Simplify `Type::try_bool()` (astral-sh#18342)
  [ty] Simplify `Type::normalized` slightly (astral-sh#18339)
  [ty] Move arviz off the list of selected primer projects (astral-sh#18336)
  [ty] Add --config-file CLI arg (astral-sh#18083)
  [ty] Tell the user why we inferred a certain Python version when reporting version-specific syntax errors (astral-sh#18295)
  [ty] Implement implicit inheritance from `Generic[]` for PEP-695 generic classes (astral-sh#18283)
  [ty] Add hint if async context manager is used in non-async with statement (astral-sh#18299)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Related to the LSP server ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support publish diagnostics
2 participants