Skip to content

Commit 6d8c160

Browse files
alanzmeta-codesync[bot]
authored andcommitted
check if a file is in a project when it opens
Summary: ELP has an assumption that all the files belonging to a given project have some common root directory, and when using buck this is marked by an `.elp.toml` file. Now that we are using buck to generate some files as part of the build process, this assumption is no longer valid, as the generated files end up in the `buck out` directory, which is outside the normal project. Further, ELP can handle more than one project at a time. So when a new file is opened in the client, we check if it belongs to one of the existing projects, and if not we configure a new project for it. This leads to a situation where if we open one of the generated files, ELP will try to make a new project for it. This causes the ELP project-wide information to become invalid, as it is now spread over more than one. In particular this shows up as generated include files no longer being resolved. ## This diff This diff addresses the issue by making use of the fact that every file in a buck project is explicitly enumerated, precisely because there it no simple directory enclosure mechanism to determine project owner. So when a new file is opened, provided ELP is already in `Running` state and thus the internal database is valid, we check if the file is already known. If so we do not check for a possible new project. Reviewed By: robertoaloi Differential Revision: D85234832 fbshipit-source-id: 4858bbfa2d51d1e3f030f666f706a2ffb155e0f1
1 parent f417b8a commit 6d8c160

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

crates/elp/src/server.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -724,38 +724,45 @@ impl Server {
724724
this.ct_diagnostics_requested = true;
725725
this.native_diagnostics_requested = true;
726726
if let Ok(path) = convert::abs_path(&params.text_document.uri) {
727-
this.fetch_projects_if_needed(&path);
728-
let path = VfsPath::from(path);
727+
let vfs_path = VfsPath::from(path.clone());
729728
let already_exists = this
730729
.mem_docs
731730
.write()
732731
.insert(
733-
path.clone(),
732+
vfs_path.clone(),
734733
DocumentData::new(
735734
params.text_document.version,
736735
params.text_document.text.clone().into_bytes(),
737736
),
738737
)
739738
.is_err();
740739
if already_exists {
741-
tracing::error!("duplicate DidOpenTextDocument: {}", path);
742-
log::error!("duplicate DidOpenTextDocument: {path}");
740+
tracing::error!("duplicate DidOpenTextDocument: {}", &vfs_path);
741+
log::error!("duplicate DidOpenTextDocument: {vfs_path}");
743742
}
744743

745-
let mut vfs = this.vfs.write();
746-
let bytes = params.text_document.text.into_bytes();
747-
vfs.set_file_contents(path.clone(), Some(bytes.clone()));
748-
749-
// Until we bring over the full rust-analyzer
750-
// style change processing, make a list of files
751-
// that are freshly opened so diagnostics are
752-
// generated for them, despite no changes being
753-
// registered in vfs.
754-
let (file_id, _) = vfs.file_id(&path).unwrap();
755-
this.newly_opened_documents.push(ChangedFile {
756-
file_id,
757-
change: Change::Modify(bytes, params.text_document.version as u64),
758-
});
744+
let file_id = {
745+
let mut vfs = this.vfs.write();
746+
let bytes = params.text_document.text.into_bytes();
747+
vfs.set_file_contents(vfs_path.clone(), Some(bytes.clone()));
748+
749+
// Until we bring over the full rust-analyzer
750+
// style change processing, make a list of files
751+
// that are freshly opened so diagnostics are
752+
// generated for them, despite no changes being
753+
// registered in vfs.
754+
let (file_id, _) = vfs.file_id(&vfs_path).unwrap();
755+
this.newly_opened_documents.push(ChangedFile {
756+
file_id,
757+
change: Change::Modify(bytes, params.text_document.version as u64),
758+
});
759+
file_id
760+
};
761+
762+
if this.status != Status::Running || this.analysis_host.raw_database().file_app_data(file_id).is_none() {
763+
// We do not have a project associated with this file
764+
this.fetch_projects_if_needed(&path);
765+
}
759766
} else {
760767
log::error!(
761768
"DidOpenTextDocument: could not get vfs path for {}",

0 commit comments

Comments
 (0)