Skip to content

fix: Differentiate between vfs config load and file changed events #16319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fn load_crate_graph(
break;
}
}
vfs::loader::Message::Loaded { files } => {
vfs::loader::Message::Loaded { files } | vfs::loader::Message::Changed { files } => {
for (path, contents) in files {
vfs.set_file_contents(path.into(), contents);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ fn sysroot_to_crate_graph(
let public_deps = SysrootPublicDeps {
deps: sysroot
.public_deps()
.map(|(name, idx, prelude)| (name, sysroot_crates[&idx], prelude))
.filter_map(|(name, idx, prelude)| Some((name, *sysroot_crates.get(&idx)?, prelude)))
.collect::<Vec<_>>(),
};

Expand Down
16 changes: 8 additions & 8 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl GlobalState {
let _p = profile::span("GlobalState::process_changes");

let mut file_changes = FxHashMap::<_, (bool, ChangedFile)>::default();
let (change, modified_files, workspace_structure_change) = {
let (change, modified_rust_files, workspace_structure_change) = {
let mut change = Change::new();
let mut guard = self.vfs.write();
let changed_files = guard.0.take_changes();
Expand Down Expand Up @@ -254,8 +254,8 @@ impl GlobalState {
*change = Create(new);
*just_created = true;
}
// shouldn't occur, but collapse into `Modify`
(Modify(prev), _, Create(new)) => *prev = new,
// shouldn't occur, but keep the Create
(prev @ Modify(_), _, new @ Create(_)) => *prev = new,
}
}
Entry::Vacant(v) => {
Expand All @@ -276,7 +276,7 @@ impl GlobalState {
// A file was added or deleted
let mut has_structure_changes = false;
let mut bytes = vec![];
let mut modified_files = vec![];
let mut modified_rust_files = vec![];
for file in changed_files {
let vfs_path = &vfs.file_path(file.file_id);
if let Some(path) = vfs_path.as_path() {
Expand All @@ -288,8 +288,8 @@ impl GlobalState {
has_structure_changes = true;
workspace_structure_change =
Some((path, self.crate_graph_file_dependencies.contains(vfs_path)));
} else {
modified_files.push(file.file_id);
} else if path.extension() == Some("rs".as_ref()) {
modified_rust_files.push(file.file_id);
}
}

Expand Down Expand Up @@ -324,7 +324,7 @@ impl GlobalState {
let roots = self.source_root_config.partition(vfs);
change.set_roots(roots);
}
(change, modified_files, workspace_structure_change)
(change, modified_rust_files, workspace_structure_change)
};

self.analysis_host.apply_change(change);
Expand All @@ -339,7 +339,7 @@ impl GlobalState {
force_crate_graph_reload,
);
}
self.proc_macro_changed = modified_files.into_iter().any(|file_id| {
self.proc_macro_changed = modified_rust_files.into_iter().any(|file_id| {
let crates = raw_database.relevant_crates(file_id);
let crate_graph = raw_database.crate_graph();

Expand Down
6 changes: 4 additions & 2 deletions crates/rust-analyzer/src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ pub(crate) fn handle_did_change_text_document(
params.content_changes,
)
.into_bytes();
*data = new_contents.clone();
state.vfs.write().0.set_file_contents(path, Some(new_contents));
if *data != new_contents {
*data = new_contents.clone();
state.vfs.write().0.set_file_contents(path, Some(new_contents));
}
}
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,18 @@ impl GlobalState {
}

fn handle_vfs_msg(&mut self, message: vfs::loader::Message) {
let is_changed = matches!(message, vfs::loader::Message::Changed { .. });
match message {
vfs::loader::Message::Loaded { files } => {
vfs::loader::Message::Changed { files } | vfs::loader::Message::Loaded { files } => {
let vfs = &mut self.vfs.write().0;
for (path, contents) in files {
let path = VfsPath::from(path);
// if the file is in mem docs, it's managed by the client via notifications
// so only set it if its not in there
if !self.mem_docs.contains(&path) {
vfs.set_file_contents(path, contents);
if is_changed || vfs.file_id(&path).is_none() {
vfs.set_file_contents(path, contents);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/tests/slow-tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ fn main() {
#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
fn resolve_proc_macro() {
use expect_test::expect;
if skip_slow_tests() || true {
if skip_slow_tests() {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/vfs-notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl NotifyActor {
Some((path, contents))
})
.collect();
self.send(loader::Message::Loaded { files });
self.send(loader::Message::Changed { files });
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/vfs/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub enum Message {
Progress { n_total: usize, n_done: usize, config_version: u32 },
/// The handle loaded the following files' content.
Loaded { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> },
/// The handle loaded the following files' content.
Changed { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> },
}

/// Type that will receive [`Messages`](Message) from a [`Handle`].
Expand Down Expand Up @@ -199,6 +201,9 @@ impl fmt::Debug for Message {
Message::Loaded { files } => {
f.debug_struct("Loaded").field("n_files", &files.len()).finish()
}
Message::Changed { files } => {
f.debug_struct("Changed").field("n_files", &files.len()).finish()
}
Message::Progress { n_total, n_done, config_version } => f
.debug_struct("Progress")
.field("n_total", n_total)
Expand Down