Skip to content

Commit 6857989

Browse files
Fix "no value set for FileTextQuery(FileId(..))"
1 parent 42be522 commit 6857989

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn load(
7373
}
7474
vfs::loader::Message::Loaded { files } => {
7575
for (path, contents) in files {
76-
vfs.set_file_contents(path.into(), contents)
76+
vfs.set_file_contents(path.into(), contents);
7777
}
7878
}
7979
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl GlobalState {
255255
for (path, contents) in files {
256256
let path = VfsPath::from(path);
257257
if !self.mem_docs.contains_key(&path) {
258-
vfs.set_file_contents(path, contents)
258+
vfs.set_file_contents(path, contents);
259259
}
260260
}
261261
}
@@ -503,11 +503,21 @@ impl GlobalState {
503503
{
504504
log::error!("duplicate DidOpenTextDocument: {}", path)
505505
}
506-
this.vfs
506+
let changed = this
507+
.vfs
507508
.write()
508509
.0
509510
.set_file_contents(path, Some(params.text_document.text.into_bytes()));
510-
this.maybe_update_diagnostics();
511+
512+
// If the VFS contents are unchanged, update diagnostics, since `handle_event`
513+
// won't see any changes. This avoids missing diagnostics when opening a file.
514+
//
515+
// If the file *was* changed, `handle_event` will already recompute and send
516+
// diagnostics. We can't do it here, since the *current* file contents might be
517+
// unset in salsa, since the VFS change hasn't been applied to the database yet.
518+
if !changed {
519+
this.maybe_update_diagnostics();
520+
}
511521
}
512522
Ok(())
513523
})?

crates/vfs/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,19 @@ impl Vfs {
9999
(file_id, path)
100100
})
101101
}
102-
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) {
102+
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
103103
let file_id = self.alloc_file_id(path);
104104
let change_kind = match (&self.get(file_id), &contents) {
105-
(None, None) => return,
105+
(None, None) => return false,
106106
(None, Some(_)) => ChangeKind::Create,
107107
(Some(_), None) => ChangeKind::Delete,
108-
(Some(old), Some(new)) if old == new => return,
108+
(Some(old), Some(new)) if old == new => return false,
109109
(Some(_), Some(_)) => ChangeKind::Modify,
110110
};
111111

112112
*self.get_mut(file_id) = contents;
113-
self.changes.push(ChangedFile { file_id, change_kind })
113+
self.changes.push(ChangedFile { file_id, change_kind });
114+
true
114115
}
115116
pub fn has_changes(&self) -> bool {
116117
!self.changes.is_empty()

0 commit comments

Comments
 (0)