Skip to content

Comments

[ty] Add basic file watching to server#17912

Merged
MichaReiser merged 2 commits intomainfrom
micha/server-watch
May 7, 2025
Merged

[ty] Add basic file watching to server#17912
MichaReiser merged 2 commits intomainfrom
micha/server-watch

Conversation

@MichaReiser
Copy link
Member

Summary

This PR does the absolut minimum to add file watching support to ty's LSP.

The main limitation is that it only watches file in the project directory. It doesn't watch for
changes to:

  • The user configuration
  • search paths outside the project

It also has no special handling in case the user adds or removes a ty.toml or pyproject.toml which would change the project's root directory (other than what ty's apply_changes provides by default). The solution
here is a tighter integration (or at least code sharing) with ProjectWatcher. Ideally, the CLI and LSP would share all code and only differ by the file watching backend.

This gives us:

  • Updated diagnostics when changig files outside the main editor (e.g. git pull)
  • Updated inlays and diagnostics when changing the configuration

Test Plan

I edited a file outside of VS code and verified that diagnostics and inlay hints are correctly recomputed.

I verified that changing the python version correctly propagates to inlays.

@MichaReiser MichaReiser added server Related to the LSP server ty Multi-file analysis & type inference labels May 7, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2025

mypy_primer results

No ecosystem changes detected ✅

serde_json::to_value(DidChangeWatchedFilesRegistrationOptions {
watchers: vec![
FileSystemWatcher {
glob_pattern: lsp_types::GlobPattern::String("**/ty.toml".into()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly curious: does this mean that we're reacting to changes that match the **/ty.toml pattern. Or that we actively scan the whole project for this pattern when starting the server (which could be quite slow?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The VS code file watcher sends us notifications if any file inside the project matching ty.toml gets changed.

Comment on lines +199 to +208
FileSystemWatcher {
glob_pattern: lsp_types::GlobPattern::String(
"**/.gitignore".into(),
),
kind: None,
},
FileSystemWatcher {
glob_pattern: lsp_types::GlobPattern::String("**/.ignore".into()),
kind: None,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to be (more) exhaustive here, we would probably also have to add .git/info/exclude?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe. It's not something that we currently handle in apply_changes. (but maybe should?)

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'll ignore this for now. There's another issue around better handling for git ignored files

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you!

@AlexWaygood AlexWaygood removed their request for review May 7, 2025 14:18
}
};

by_db
Copy link
Member

Choose a reason for hiding this comment

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

Is this suppose to be ty_db ?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, nevermind, I missed the definition. I think it would be useful to include something before ??_by_db

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.

Looks good, thanks!

@MichaReiser MichaReiser changed the title Add basic file watching to server [ty] Add basic file watching to server May 7, 2025
@MichaReiser MichaReiser force-pushed the micha/server-watch branch from 1b606d0 to 5d17f87 Compare May 7, 2025 16:46
@MichaReiser MichaReiser merged commit 51386b3 into main May 7, 2025
35 checks passed
@MichaReiser MichaReiser deleted the micha/server-watch branch May 7, 2025 17:03
dcreager added a commit that referenced this pull request May 7, 2025
…lass

* origin/main:
  [`pylint`] add fix safety section (`PLC2801`) (#17825)
  Add instructions on how to upgrade to a newer Rust version (#17928)
  [parser] Flag single unparenthesized generator expr with trailing comma in arguments. (#17893)
  [ty] Ensure that `T` is disjoint from `~T` even when `T` is a TypeVar (#17922)
  [ty] Sort collected diagnostics before snapshotting them in mdtest (#17926)
  [ty] Add basic file watching to server (#17912)
  Make completions an opt-in LSP feature (#17921)
  Add link to `ty` issue tracker (#17924)
  [ty] Add support for `__all__` (#17856)
  [ty] fix assigning a typevar to a union with itself (#17910)
  [ty] Improve UX for `[duplicate-base]` diagnostics (#17914)
  Clean up some Ruff references in the ty server (#17920)
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.

3 participants