Skip to content

Commit dfe1450

Browse files
authored
[ty] Avoid cancelling requests for unrelated file changes (astral-sh#29604)
1 parent 3bf4988 commit dfe1450

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎crates/ty_project/src/db.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ mod tests {
957957
use ruff_db::system::{DbWithWritableSystem as _, SystemPathBuf, TestSystem};
958958
use ruff_db::testing::assert_function_query_was_not_run_by_name;
959959
use ruff_python_trivia::textwrap::dedent;
960+
use salsa::plumbing::current_revision;
960961
use ty_module_resolver::list_modules;
961962
use ty_python_semantic::Db as _;
962963

@@ -996,6 +997,29 @@ mod tests {
996997
Ok(())
997998
}
998999

1000+
#[test]
1001+
fn unrelated_file_change_does_not_advance_revision() -> anyhow::Result<()> {
1002+
let system = TestSystem::default();
1003+
let fs = system.memory_file_system().clone();
1004+
fs.write_files_all([
1005+
("/project/main.py", ""),
1006+
("/project/unrelated.log", "before"),
1007+
])?;
1008+
let metadata = ProjectMetadata::new("test", SystemPathBuf::from("/project"));
1009+
let mut db = ProjectDatabase::fallible(metadata, system)?;
1010+
db.project().files(&db);
1011+
let revision = current_revision(&db);
1012+
1013+
fs.write_file_all("/project/unrelated.log", "after")?;
1014+
db.apply_changes(&[ChangeEvent::file_content_changed(SystemPathBuf::from(
1015+
"/project/unrelated.log",
1016+
))]);
1017+
1018+
assert_eq!(current_revision(&db), revision);
1019+
1020+
Ok(())
1021+
}
1022+
9991023
#[test]
10001024
fn changed_script_metadata_updates_import_resolution() -> anyhow::Result<()> {
10011025
let system = TestSystem::default();

‎crates/ty_project/src/files.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl IndexedMut<'_> {
267267

268268
pub(super) fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
269269
self.inner_mut().diagnostics = diagnostics;
270+
self.did_change = true;
270271
}
271272

272273
fn inner_mut(&mut self) -> &mut IndexedInner {

‎crates/ty_project/src/lib.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,13 @@ impl Project {
766766
///
767767
/// This is a no-op if the project files haven't been indexed yet.
768768
fn replace_index_diagnostics(self, db: &mut dyn Db, diagnostics: Vec<Diagnostic>) {
769+
// Check before acquiring a mutable index, which cancels pending queries.
770+
match self.file_set(db).get() {
771+
Index::Lazy(_) => return,
772+
Index::Indexed(index) if index.diagnostics() == diagnostics => return,
773+
Index::Indexed(_) => {}
774+
}
775+
769776
let Some(mut index) = IndexedFiles::indexed_mut(db, self) else {
770777
return;
771778
};

0 commit comments

Comments
 (0)