language: Spawn language servers on background threads (#44631)

Closes https://github.com/zed-industries/zed/issues/39056

Leverages a new `await_on_background` API that spawns the future on the
background but blocks the current task, allowing to borrow from the
surrounding scope.

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-12-11 17:23:27 +00:00
committed by GitHub
parent cda78c12ab
commit 5a6198cc39
8 changed files with 132 additions and 69 deletions
+20 -16
View File
@@ -314,7 +314,7 @@ pub struct AdapterServerCapabilities {
impl LanguageServer {
/// Starts a language server process.
pub fn new(
pub async fn new(
stderr_capture: Arc<Mutex<Option<String>>>,
server_id: LanguageServerId,
server_name: LanguageServerName,
@@ -331,26 +331,30 @@ impl LanguageServer {
};
let root_uri = Uri::from_file_path(&working_dir)
.map_err(|()| anyhow!("{working_dir:?} is not a valid URI"))?;
log::info!(
"starting language server process. binary path: {:?}, working directory: {:?}, args: {:?}",
"starting language server process. binary path: \
{:?}, working directory: {:?}, args: {:?}",
binary.path,
working_dir,
&binary.arguments
);
let mut command = util::command::new_smol_command(&binary.path);
command
.current_dir(working_dir)
.args(&binary.arguments)
.envs(binary.env.clone().unwrap_or_default())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
let mut server = command
.spawn()
.with_context(|| format!("failed to spawn command {command:?}",))?;
let mut server = cx
.background_executor()
.await_on_background(async {
let mut command = util::command::new_smol_command(&binary.path);
command
.current_dir(working_dir)
.args(&binary.arguments)
.envs(binary.env.clone().unwrap_or_default())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
command
.spawn()
.with_context(|| format!("failed to spawn command {command:?}",))
})
.await?;
let stdin = server.stdin.take().unwrap();
let stdout = server.stdout.take().unwrap();