lsp: Fix wrong WorkspaceFolder when opening only file (#12129)

This fixes #11361 and #8764 by making sure we pass a directory as
`WorkspaceFolder` to the language server.

We already compute the `working_dir` correctly when
`self.root_path.is_file()`, but we didn't use it.

Release Notes:

- Fixed language servers (such as `gopls`) not starting up correctly
when opening a single file in Zed.
([#11361](https://github.com/zed-industries/zed/issues/11361) and
[#8764](https://github.com/zed-industries/zed/issues/8764)).
This commit is contained in:
Thorsten Ball 2024-05-22 16:32:06 +02:00 committed by GitHub
parent 49dffabab9
commit aa539fc347
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -79,6 +79,7 @@ pub struct LanguageServer {
io_tasks: Mutex<Option<(Task<Option<()>>, Task<Option<()>>)>>, io_tasks: Mutex<Option<(Task<Option<()>>, Task<Option<()>>)>>,
output_done_rx: Mutex<Option<barrier::Receiver>>, output_done_rx: Mutex<Option<barrier::Receiver>>,
root_path: PathBuf, root_path: PathBuf,
working_dir: PathBuf,
server: Arc<Mutex<Option<Child>>>, server: Arc<Mutex<Option<Child>>>,
} }
@ -288,6 +289,7 @@ impl LanguageServer {
stderr_capture, stderr_capture,
Some(server), Some(server),
root_path, root_path,
working_dir,
code_action_kinds, code_action_kinds,
cx, cx,
move |notification| { move |notification| {
@ -322,6 +324,7 @@ impl LanguageServer {
stderr_capture: Arc<Mutex<Option<String>>>, stderr_capture: Arc<Mutex<Option<String>>>,
server: Option<Child>, server: Option<Child>,
root_path: &Path, root_path: &Path,
working_dir: &Path,
code_action_kinds: Option<Vec<CodeActionKind>>, code_action_kinds: Option<Vec<CodeActionKind>>,
cx: AsyncAppContext, cx: AsyncAppContext,
on_unhandled_notification: F, on_unhandled_notification: F,
@ -393,6 +396,7 @@ impl LanguageServer {
io_tasks: Mutex::new(Some((input_task, output_task))), io_tasks: Mutex::new(Some((input_task, output_task))),
output_done_rx: Mutex::new(Some(output_done_rx)), output_done_rx: Mutex::new(Some(output_done_rx)),
root_path: root_path.to_path_buf(), root_path: root_path.to_path_buf(),
working_dir: working_dir.to_path_buf(),
server: Arc::new(Mutex::new(server)), server: Arc::new(Mutex::new(server)),
} }
} }
@ -566,7 +570,7 @@ impl LanguageServer {
options: Option<Value>, options: Option<Value>,
cx: &AppContext, cx: &AppContext,
) -> Task<Result<Arc<Self>>> { ) -> Task<Result<Arc<Self>>> {
let root_uri = Url::from_file_path(&self.root_path).unwrap(); let root_uri = Url::from_file_path(&self.working_dir).unwrap();
#[allow(deprecated)] #[allow(deprecated)]
let params = InitializeParams { let params = InitializeParams {
process_id: None, process_id: None,
@ -1171,6 +1175,7 @@ impl FakeLanguageServer {
Arc::new(Mutex::new(None)), Arc::new(Mutex::new(None)),
None, None,
Path::new("/"), Path::new("/"),
Path::new("/"),
None, None,
cx.clone(), cx.clone(),
|_| {}, |_| {},
@ -1187,6 +1192,7 @@ impl FakeLanguageServer {
Arc::new(Mutex::new(None)), Arc::new(Mutex::new(None)),
None, None,
Path::new("/"), Path::new("/"),
Path::new("/"),
None, None,
cx, cx,
move |msg| { move |msg| {