diff --git a/Cargo.lock b/Cargo.lock index 2ec5aa4a7d4..7f969a50a2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9798,14 +9798,17 @@ dependencies = [ "globset", "gpui", "grammars", + "http_client", "indoc", "itertools 0.14.0", "json_schema_store", "language", "log", "lsp", + "managed_resources", "node_runtime", "parking_lot", + "paths", "pet", "pet-conda", "pet-core", @@ -9841,6 +9844,7 @@ dependencies = [ "unindent", "url", "util", + "watch", ] [[package]] diff --git a/crates/copilot/src/copilot.rs b/crates/copilot/src/copilot.rs index d157e7f8b3d..a35bb7e6f82 100644 --- a/crates/copilot/src/copilot.rs +++ b/crates/copilot/src/copilot.rs @@ -1350,6 +1350,7 @@ async fn get_copilot_lsp( .execute_command( node_runtime::LatestNpmPackageVersion { package: PACKAGE_NAME.into(), + requirement: None, }, bill.clone(), cx, diff --git a/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs b/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs index 8977d53d4c1..29126b3a71e 100644 --- a/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs +++ b/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs @@ -770,6 +770,7 @@ impl nodejs::Host for WasmState { self.execute_node_command( node_runtime::LatestNpmPackageVersion { package: package_name, + requirement: None, }, reason, ) diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index d7a5cff9c47..e9b2b9aeffb 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -737,8 +737,9 @@ pub trait LspInstaller { _version: &Self::BinaryVersion, _container_dir: &PathBuf, _delegate: &Arc, - ) -> impl Send + Future> + use { - async { None } + _cx: AsyncApp, + ) -> impl Future>> + use { + async { Ok(None) } } fn fetch_server_binary( @@ -746,12 +747,14 @@ pub trait LspInstaller { latest_version: Self::BinaryVersion, container_dir: PathBuf, _delegate: &Arc, - ) -> impl Send + Future> + use; + cx: AsyncApp, + ) -> impl Future> + use; fn cached_server_binary( &self, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> impl Future>; } @@ -797,10 +800,9 @@ where .fetch_latest_server_version(delegate, pre_release, cx) .await?; - if let Some(binary) = cx - .background_executor() - .spawn(self.check_if_version_installed(&latest_version, &container_dir, &delegate)) - .await + if let Some(binary) = self + .check_if_version_installed(&latest_version, &container_dir, &delegate, cx.clone()) + .await? { log::debug!("language server {:?} is already installed", name.0); delegate.update_status(name.clone(), BinaryStatus::None); @@ -808,9 +810,8 @@ where } else { log::debug!("downloading language server {:?}", name.0); delegate.update_status(name.clone(), BinaryStatus::Downloading); - let binary = cx - .background_executor() - .spawn(self.fetch_server_binary(latest_version, container_dir, delegate)) + let binary = self + .fetch_server_binary(latest_version, container_dir, delegate, cx.clone()) .await; delegate.update_status(name.clone(), BinaryStatus::None); @@ -874,7 +875,7 @@ where }; let last_downloaded_binary = self - .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref()) + .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref(), cx.clone()) .await .context( "did not find existing language server binary, falling back to downloading", @@ -891,7 +892,11 @@ where if let Err(error) = binary.as_ref() { if let Some(prev_downloaded_binary) = self - .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref()) + .cached_server_binary( + container_dir.to_path_buf(), + delegate.as_ref(), + cx.clone(), + ) .await { log::info!( @@ -1668,7 +1673,8 @@ impl LspInstaller for FakeLspAdapter { _: (), _: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { + _: AsyncApp, + ) -> impl Future> + use<> { async { unreachable!(); } @@ -1678,6 +1684,7 @@ impl LspInstaller for FakeLspAdapter { &self, _: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { unreachable!(); } diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml index 0040b235622..4d926482886 100644 --- a/crates/languages/Cargo.toml +++ b/crates/languages/Cargo.toml @@ -67,8 +67,11 @@ util.workspace = true [dev-dependencies] fs = { workspace = true, features = ["test-support"] } +http_client.workspace = true indoc.workspace = true language = { workspace = true, features = ["test-support"] } +managed_resources.workspace = true +paths.workspace = true pretty_assertions.workspace = true tempfile.workspace = true settings = { workspace = true, features = ["test-support"] } @@ -83,3 +86,4 @@ tree-sitter-rust.workspace = true tree-sitter-typescript.workspace = true tree-sitter = { workspace = true, features = ["wasm"] } unindent.workspace = true +watch.workspace = true diff --git a/crates/languages/src/bash.rs b/crates/languages/src/bash.rs index 103cacdc165..45a35024d60 100644 --- a/crates/languages/src/bash.rs +++ b/crates/languages/src/bash.rs @@ -43,7 +43,7 @@ impl BashLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, env: HashMap, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir @@ -71,9 +71,15 @@ impl LspInstaller for BashLspAdapter { &self, container_dir: std::path::PathBuf, delegate: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { let env = delegate.shell_env().await; - Self::get_cached_server_binary(container_dir, env, &self.node).await + Self::get_cached_server_binary( + container_dir, + env, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } async fn check_if_user_installed( @@ -97,8 +103,9 @@ impl LspInstaller for BashLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); let delegate = delegate.clone(); @@ -115,17 +122,17 @@ impl LspInstaller for BashLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { let env = delegate.shell_env().await; - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: Some(env), arguments: vec![server_path.into(), "start".into()], - }) + })) } } } @@ -134,9 +141,9 @@ impl LspInstaller for BashLspAdapter { &self, _: &Arc, _: bool, - _: &mut gpui::AsyncApp, + cx: &mut gpui::AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::PACKAGE_NAME) .await } @@ -146,8 +153,9 @@ impl LspInstaller for BashLspAdapter { _latest_version: Self::BinaryVersion, container_dir: std::path::PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let delegate = delegate.clone(); async move { diff --git a/crates/languages/src/c.rs b/crates/languages/src/c.rs index 13e3065bedb..bfc0ed9815c 100644 --- a/crates/languages/src/c.rs +++ b/crates/languages/src/c.rs @@ -73,10 +73,11 @@ impl LspInstaller for CLspAdapter { version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { ensure_arch_compatibility()?; let GitHubLspBinaryVersion { @@ -150,13 +151,14 @@ impl LspInstaller for CLspAdapter { .await?; Ok(binary) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { get_cached_server_binary(container_dir).await } diff --git a/crates/languages/src/css.rs b/crates/languages/src/css.rs index cd5862d19c3..ca05463fe46 100644 --- a/crates/languages/src/css.rs +++ b/crates/languages/src/css.rs @@ -40,9 +40,9 @@ impl LspInstaller for CssLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version("vscode-langservers-extracted") .await } @@ -70,8 +70,9 @@ impl LspInstaller for CssLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(SERVER_PATH); @@ -92,8 +93,9 @@ impl LspInstaller for CssLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -107,16 +109,16 @@ impl LspInstaller for CssLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: server_binary_arguments(&server_path), - }) + })) } } } @@ -125,8 +127,13 @@ impl LspInstaller for CssLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_server_binary(container_dir, &self.node).await + get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -180,7 +187,7 @@ impl LspAdapter for CssLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(SERVER_PATH); diff --git a/crates/languages/src/eslint.rs b/crates/languages/src/eslint.rs index c55d7135850..66978d5fc68 100644 --- a/crates/languages/src/eslint.rs +++ b/crates/languages/src/eslint.rs @@ -102,9 +102,10 @@ impl LspInstaller for EsLintLspAdapter { version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - let node = self.node.clone(); + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let destination_path = Self::build_destination_path(&container_dir); @@ -161,12 +162,14 @@ impl LspInstaller for EsLintLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let server_path = Self::build_destination_path(&container_dir).join(EsLintLspAdapter::SERVER_PATH); fs::metadata(&server_path).await.ok()?; Some(LanguageServerBinary { - path: self.node.binary_path().await.ok()?, + path: node.binary_path().await.ok()?, env: None, arguments: eslint_server_binary_arguments(&server_path), }) diff --git a/crates/languages/src/go.rs b/crates/languages/src/go.rs index 0c096a398ab..0414569bd2f 100644 --- a/crates/languages/src/go.rs +++ b/crates/languages/src/go.rs @@ -128,10 +128,11 @@ impl LspInstaller for GoLspAdapter { version: Option, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let go = delegate.which("go".as_ref()).await.unwrap_or("go".into()); let go_version_output = util::command::new_command(&go) .args(["version"]) @@ -195,13 +196,14 @@ impl LspInstaller for GoLspAdapter { arguments: server_binary_arguments(), env: None, }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { get_cached_server_binary(&container_dir).await } diff --git a/crates/languages/src/json.rs b/crates/languages/src/json.rs index 28ece19a0f4..89af153e7a2 100644 --- a/crates/languages/src/json.rs +++ b/crates/languages/src/json.rs @@ -157,9 +157,9 @@ impl LspInstaller for JsonLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::PACKAGE_NAME) .await } @@ -187,8 +187,9 @@ impl LspInstaller for JsonLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -202,16 +203,16 @@ impl LspInstaller for JsonLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: server_binary_arguments(&server_path), - }) + })) } } } @@ -221,8 +222,9 @@ impl LspInstaller for JsonLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(SERVER_PATH); @@ -242,8 +244,13 @@ impl LspInstaller for JsonLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_server_binary(container_dir, &self.node).await + get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -403,7 +410,7 @@ fn json_schema_proxy_settings(proxy: Option) -> Option { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(SERVER_PATH); @@ -584,10 +591,11 @@ impl LspInstaller for NodeVersionAdapter { latest_version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let version = &latest_version; let destination_path = container_dir.join(format!( "{}-{}{}", @@ -627,13 +635,14 @@ impl LspInstaller for NodeVersionAdapter { env: None, arguments: Default::default(), }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _delegate: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { get_cached_version_server_binary(container_dir).await } diff --git a/crates/languages/src/lib.rs b/crates/languages/src/lib.rs index 5e210008bd4..d4883ca3b37 100644 --- a/crates/languages/src/lib.rs +++ b/crates/languages/src/lib.rs @@ -23,6 +23,7 @@ mod css; mod eslint; mod go; mod json; +mod npm; mod package_json; mod python; mod rust; diff --git a/crates/languages/src/npm.rs b/crates/languages/src/npm.rs new file mode 100644 index 00000000000..dd4debd81ef --- /dev/null +++ b/crates/languages/src/npm.rs @@ -0,0 +1,327 @@ +use anyhow::Result; +use gpui::AsyncApp; +use language::LspAdapter; +use node_runtime::{ + ExecutionBillRoot, InstallNpmPackages, InstalledNpmPackageVersion, LatestNpmPackageVersion, + NodeBinaryPath, NodeRuntime, ResourceExecutionBill, RunNpmSubcommand, VersionStrategy, +}; +use semver::{Version, VersionReq}; +use std::{ + path::{Path, PathBuf}, + process::Output, +}; +use util::ResultExt; + +pub(crate) struct NpmInstaller { + node: NodeRuntime, + bill: ResourceExecutionBill, + cx: AsyncApp, +} + +impl NpmInstaller { + pub fn new(node: &NodeRuntime, adapter: &impl LspAdapter, cx: AsyncApp) -> Self { + Self { + node: node.clone(), + bill: ResourceExecutionBill { + root: ExecutionBillRoot::LanguageServer { + name: adapter.name().0, + }, + reasoning: vec!["Set up the language server".into()], + }, + cx, + } + } + + pub async fn binary_path(&self) -> Result { + self.node + .execute_command(NodeBinaryPath, self.bill.clone(), &mut self.cx.clone()) + .await? + } + + pub async fn npm_package_latest_version(&self, package: &str) -> Result { + self.npm_package_latest_version_with_requirement(package, None) + .await + } + + pub async fn npm_package_latest_version_with_requirement( + &self, + package: &str, + requirement: Option<&VersionReq>, + ) -> Result { + self.node + .execute_command( + LatestNpmPackageVersion { + package: package.into(), + requirement: requirement.cloned(), + }, + self.bill.clone(), + &mut self.cx.clone(), + ) + .await? + } + + pub async fn npm_install_latest_packages( + &self, + directory: &Path, + packages: &[&str], + ) -> Result<()> { + let packages = packages + .iter() + .map(|package| (*package, "latest")) + .collect::>(); + self.npm_install_packages(directory, &packages).await + } + + pub async fn npm_install_packages( + &self, + directory: &Path, + packages: &[(&str, &str)], + ) -> Result<()> { + self.node + .execute_command( + InstallNpmPackages { + directory: directory.into(), + packages: packages + .iter() + .map(|(name, version)| ((*name).into(), (*version).into())) + .collect(), + }, + self.bill.clone(), + &mut self.cx.clone(), + ) + .await? + } + + pub async fn run_npm_subcommand( + &self, + directory: Option<&Path>, + subcommand: &str, + args: &[&str], + ) -> Result { + self.node + .execute_command( + RunNpmSubcommand { + directory: directory.map(Path::to_path_buf), + subcommand: subcommand.into(), + args: args.iter().map(|argument| (*argument).into()).collect(), + }, + self.bill.clone(), + &mut self.cx.clone(), + ) + .await? + } + + pub async fn should_install_npm_package( + &self, + package: &str, + executable: &Path, + directory: &Path, + strategy: VersionStrategy<'_>, + ) -> Result { + let installed = self + .node + .execute_command( + InstalledNpmPackageVersion { + directory: directory.into(), + package: package.into(), + }, + self.bill.clone(), + &mut self.cx.clone(), + ) + .await?; + let Some(installed) = installed.log_err().flatten() else { + return Ok(true); + }; + if smol::fs::metadata(executable).await.is_err() { + return Ok(true); + } + Ok(match strategy { + VersionStrategy::Pin(version) => installed != *version, + VersionStrategy::Latest(version) => installed < *version, + }) + } +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use gpui::{AppContext, TestAppContext}; + use managed_resources::{ManagedResourceStore, ResourcePermission, ResourcePermissionRequest}; + use node_runtime::NodeBinaryOptions; + use std::{cell::Cell, fs, os::unix::fs::PermissionsExt, rc::Rc, sync::Arc}; + + #[gpui::test] + async fn npm_installer_preserves_permissions_and_version_rules(cx: &mut TestAppContext) { + // Node discovery resets a cache beneath the global data directory. + if std::env::var_os("ZED_LSP_NPM_TEST_CHILD").is_none() { + let output = + std::process::Command::new(std::env::current_exe().expect("test executable")) + .args([ + "--exact", + "npm::tests::npm_installer_preserves_permissions_and_version_rules", + "--nocapture", + ]) + .env("ZED_LSP_NPM_TEST_CHILD", "1") + .env("ITERATIONS", "1") + .output() + .expect("isolated test"); + assert!(output.status.success(), "{output:?}"); + return; + } + cx.executor().allow_parking(); + let directory = tempfile::tempdir().expect("runtime directory"); + paths::set_custom_data_dir(&directory.path().join("data").to_string_lossy()); + let node = directory.path().join("node"); + let npm = directory.path().join("npm"); + fs::write(&node, "#!/bin/sh\necho v22.0.0\n").expect("node fixture"); + fs::write(&npm, "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$0.args\"\n").expect("npm fixture"); + for binary in [&node, &npm] { + fs::set_permissions(binary, fs::Permissions::from_mode(0o755)).expect("executable"); + } + let package = directory.path().join("node_modules/example"); + fs::create_dir_all(&package).expect("package directory"); + let executable = package.join("server.js"); + fs::write(&executable, "").expect("server"); + fs::write(package.join("package.json"), r#"{"version":"3.1.4"}"#).expect("metadata"); + let allow = Rc::new(Cell::new(false)); + let store = cx.new(|_| { + let allow = allow.clone(); + ManagedResourceStore::new(Default::default(), move |request, _| { + let ResourcePermissionRequest::ExecuteCommand { bill, .. } = request else { + panic!("configured Node should not need management"); + }; + assert_eq!( + bill.root, + ExecutionBillRoot::LanguageServer { + name: "vscode-css-language-server".into() + } + ); + gpui::Task::ready(if allow.get() { + ResourcePermission::Allowed + } else { + ResourcePermission::Denied + }) + }) + }); + let runtime = NodeRuntime::new( + Arc::new(http_client::BlockedHttpClient), + None, + watch::channel(Some(NodeBinaryOptions { + use_paths: Some((node.clone(), npm.clone())), + ..Default::default() + })) + .1, + ) + .with_managed_resources(store); + let adapter = crate::css::CssLspAdapter::new(runtime.clone()); + let installer = NpmInstaller::new(&runtime, &adapter, cx.to_async()); + let older = Version::new(3, 0, 0); + assert!( + installer + .should_install_npm_package( + "example", + &executable, + directory.path(), + VersionStrategy::Latest(&older) + ) + .await + .is_err(), + "denial must not become a cache miss" + ); + assert!( + installer + .npm_install_packages(directory.path(), &[("example", "3.0.0")]) + .await + .is_err() + ); + assert!(!directory.path().join("npm.args").exists()); + allow.set(true); + assert_eq!( + installer.binary_path().await.expect("configured binary"), + node + ); + assert!( + !installer + .should_install_npm_package( + "example", + &executable, + directory.path(), + VersionStrategy::Latest(&older) + ) + .await + .expect("newer installed") + ); + assert!( + installer + .should_install_npm_package( + "example", + &executable, + directory.path(), + VersionStrategy::Pin(&older) + ) + .await + .expect("pin requires downgrade") + ); + assert!( + !installer + .should_install_npm_package( + "example", + &executable, + directory.path(), + VersionStrategy::Pin(&Version::new(3, 1, 4)) + ) + .await + .expect("matching pin") + ); + installer + .npm_install_packages(directory.path(), &[("example", "3.0.0")]) + .await + .expect("install"); + let arguments = + fs::read_to_string(directory.path().join("npm.args")).expect("npm arguments"); + assert!( + arguments + .lines() + .any(|argument| argument == "example@3.0.0") + ); + fs::write( + &npm, + r#"#!/bin/sh +case " $* " in + *" info "*) echo '{"dist-tags":{"latest":"7.0.0"},"versions":["6.2.0","7.0.0"]}' ;; + *) echo null ;; +esac +"#, + ) + .expect("registry fixture"); + assert_eq!( + installer + .npm_package_latest_version("typescript") + .await + .expect("latest version"), + Version::new(7, 0, 0) + ); + assert_eq!( + installer + .npm_package_latest_version_with_requirement( + "typescript", + Some(&VersionReq::parse("<7").expect("requirement")) + ) + .await + .expect("compatible version"), + Version::new(6, 2, 0) + ); + fs::write(package.join("package.json"), "invalid").expect("broken metadata"); + assert!( + installer + .should_install_npm_package( + "example", + &executable, + directory.path(), + VersionStrategy::Latest(&older) + ) + .await + .expect("bad metadata needs reinstall") + ); + } +} diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs index 0501b2c6d16..7af100ee852 100644 --- a/crates/languages/src/python.rs +++ b/crates/languages/src/python.rs @@ -512,10 +512,11 @@ impl LspInstaller for TyLspAdapter { latest_version: Self::BinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let GitHubLspBinaryVersion { name, url, @@ -599,13 +600,14 @@ impl LspInstaller for TyLspAdapter { env: None, arguments: vec!["server".into()], }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { maybe!(async { let mut last = None; @@ -652,7 +654,7 @@ impl PyrightLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { let server_path = container_dir.join(Self::SERVER_PATH); if server_path.exists() { @@ -777,9 +779,9 @@ impl LspInstaller for PyrightLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::SERVER_NAME.as_ref()) .await } @@ -788,7 +790,7 @@ impl LspInstaller for PyrightLspAdapter { &self, delegate: &Arc, _: Option, - _: &AsyncApp, + cx: &AsyncApp, ) -> Option { if let Some(pyright_bin) = delegate.which("pyright-langserver".as_ref()).await { let env = delegate.shell_env().await; @@ -798,11 +800,14 @@ impl LspInstaller for PyrightLspAdapter { arguments: vec!["--stdio".into()], }) } else { - let node = delegate.which("node".as_ref()).await?; let (node_modules_path, _) = delegate .npm_package_installed_version(Self::SERVER_NAME.as_ref()) .await .log_err()??; + let node = crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) + .binary_path() + .await + .log_err()?; let path = node_modules_path.join(Self::NODE_MODULE_RELATIVE_SERVER_PATH); @@ -820,9 +825,10 @@ impl LspInstaller for PyrightLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - let node = self.node.clone(); + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(Self::SERVER_PATH); @@ -843,9 +849,10 @@ impl LspInstaller for PyrightLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future>> + use<> { let delegate = delegate.clone(); - let node = self.node.clone(); + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -859,17 +866,17 @@ impl LspInstaller for PyrightLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { let env = delegate.shell_env().await; - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: Some(env), arguments: vec![server_path.into(), "--stdio".into()], - }) + })) } } } @@ -878,8 +885,13 @@ impl LspInstaller for PyrightLspAdapter { &self, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - let mut binary = Self::get_cached_server_binary(container_dir, &self.node).await?; + let mut binary = Self::get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await?; binary.env = Some(delegate.shell_env().await); Some(binary) } @@ -1998,10 +2010,11 @@ impl LspInstaller for PyLspAdapter { _: (), _: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let venv = Self::ensure_venv(delegate.as_ref()).await?; let pip_path = venv.join(BINARY_DIR).join("pip3"); ensure!( @@ -2036,13 +2049,14 @@ impl LspInstaller for PyLspAdapter { env: None, arguments: vec![], }) - } + }) } async fn cached_server_binary( &self, _: PathBuf, delegate: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { let venv = self.base_venv(delegate).await.ok()?; let pylsp = venv.join(BINARY_DIR).join("pylsp"); @@ -2071,7 +2085,7 @@ impl BasedPyrightLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { let server_path = container_dir.join(Self::SERVER_PATH); if server_path.exists() { @@ -2217,9 +2231,9 @@ impl LspInstaller for BasedPyrightLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::SERVER_NAME.as_ref()) .await } @@ -2228,7 +2242,7 @@ impl LspInstaller for BasedPyrightLspAdapter { &self, delegate: &Arc, _: Option, - _: &AsyncApp, + cx: &AsyncApp, ) -> Option { if let Some(path) = delegate.which(Self::BINARY_NAME.as_ref()).await { let env = delegate.shell_env().await; @@ -2238,12 +2252,14 @@ impl LspInstaller for BasedPyrightLspAdapter { arguments: vec!["--stdio".into()], }) } else { - // TODO shouldn't this be self.node.binary_path()? - let node = delegate.which("node".as_ref()).await?; let (node_modules_path, _) = delegate .npm_package_installed_version(Self::SERVER_NAME.as_ref()) .await .log_err()??; + let node = crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) + .binary_path() + .await + .log_err()?; let path = node_modules_path.join(Self::NODE_MODULE_RELATIVE_SERVER_PATH); @@ -2261,9 +2277,10 @@ impl LspInstaller for BasedPyrightLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - let node = self.node.clone(); + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(Self::SERVER_PATH); @@ -2284,9 +2301,10 @@ impl LspInstaller for BasedPyrightLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future>> + use<> { let delegate = delegate.clone(); - let node = self.node.clone(); + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -2300,17 +2318,17 @@ impl LspInstaller for BasedPyrightLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { let env = delegate.shell_env().await; - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: Some(env), arguments: vec![server_path.into(), "--stdio".into()], - }) + })) } } } @@ -2319,8 +2337,13 @@ impl LspInstaller for BasedPyrightLspAdapter { &self, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - let mut binary = Self::get_cached_server_binary(container_dir, &self.node).await?; + let mut binary = Self::get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await?; binary.env = Some(delegate.shell_env().await); Some(binary) } @@ -2603,10 +2626,11 @@ impl LspInstaller for RuffLspAdapter { latest_version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let GitHubLspBinaryVersion { name, url, @@ -2687,13 +2711,14 @@ impl LspInstaller for RuffLspAdapter { env: None, arguments: vec!["server".into()], }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { maybe!(async { let mut last = None; @@ -2903,10 +2928,11 @@ impl LspInstaller for PyreflyLspAdapter { latest_version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let GitHubLspBinaryVersion { name, url, @@ -2990,13 +3016,14 @@ impl LspInstaller for PyreflyLspAdapter { env: None, arguments: vec!["lsp".into()], }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { maybe!(async { let mut last = None; diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs index f7c1203eb3a..9e2b58796f5 100644 --- a/crates/languages/src/rust.rs +++ b/crates/languages/src/rust.rs @@ -782,10 +782,11 @@ impl LspInstaller for RustLspAdapter { version: GitHubLspBinaryVersion, container_dir: PathBuf, delegate: &Arc, - ) -> impl Send + Future> + use<> { + cx: AsyncApp, + ) -> impl Future> + use<> { let delegate = delegate.clone(); - async move { + cx.background_executor().spawn(async move { let GitHubLspBinaryVersion { name, url, @@ -864,13 +865,14 @@ impl LspInstaller for RustLspAdapter { env: None, arguments: Default::default(), }) - } + }) } async fn cached_server_binary( &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { get_cached_server_binary(container_dir).await } diff --git a/crates/languages/src/tailwind.rs b/crates/languages/src/tailwind.rs index 0af7ae68b2c..9bb5a68690c 100644 --- a/crates/languages/src/tailwind.rs +++ b/crates/languages/src/tailwind.rs @@ -47,9 +47,9 @@ impl LspInstaller for TailwindLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::PACKAGE_NAME) .await } @@ -75,8 +75,9 @@ impl LspInstaller for TailwindLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(SERVER_PATH); @@ -97,8 +98,9 @@ impl LspInstaller for TailwindLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -112,16 +114,16 @@ impl LspInstaller for TailwindLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: server_binary_arguments(&server_path), - }) + })) } } } @@ -130,8 +132,13 @@ impl LspInstaller for TailwindLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_server_binary(container_dir, &self.node).await + get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -214,7 +221,7 @@ impl LspAdapter for TailwindLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(SERVER_PATH); diff --git a/crates/languages/src/tailwindcss.rs b/crates/languages/src/tailwindcss.rs index ac5e12e07ff..e842c8c3c97 100644 --- a/crates/languages/src/tailwindcss.rs +++ b/crates/languages/src/tailwindcss.rs @@ -43,9 +43,9 @@ impl LspInstaller for TailwindCssLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::PACKAGE_NAME) .await } @@ -71,8 +71,9 @@ impl LspInstaller for TailwindCssLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(SERVER_PATH); @@ -93,8 +94,9 @@ impl LspInstaller for TailwindCssLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -108,16 +110,16 @@ impl LspInstaller for TailwindCssLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: server_binary_arguments(&server_path), - }) + })) } } } @@ -126,8 +128,13 @@ impl LspInstaller for TailwindCssLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_server_binary(container_dir, &self.node).await + get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -181,7 +188,7 @@ impl LspAdapter for TailwindCssLspAdapter { async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(SERVER_PATH); diff --git a/crates/languages/src/typescript.rs b/crates/languages/src/typescript.rs index 8e3d17ea916..93f0c838869 100644 --- a/crates/languages/src/typescript.rs +++ b/crates/languages/src/typescript.rs @@ -668,18 +668,17 @@ impl LspInstaller for TypeScriptLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx.clone()); Ok(TypeScriptVersions { - typescript_version: self - .node + typescript_version: node .npm_package_latest_version_with_requirement( Self::PACKAGE_NAME, Some(&TYPESCRIPT_VERSION_REQ), ) .await?, - server_version: self - .node + server_version: node .npm_package_latest_version(Self::SERVER_PACKAGE_NAME) .await?, }) @@ -690,8 +689,9 @@ impl LspInstaller for TypeScriptLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let typescript_version = version.typescript_version.clone(); let server_version = version.server_version.clone(); let container_dir = container_dir.clone(); @@ -707,9 +707,9 @@ impl LspInstaller for TypeScriptLspAdapter { &container_dir, VersionStrategy::Pin(&typescript_version), ) - .await + .await? { - return None; + return Ok(None); } if node @@ -719,16 +719,16 @@ impl LspInstaller for TypeScriptLspAdapter { &container_dir, VersionStrategy::Latest(&server_version), ) - .await + .await? { - return None; + return Ok(None); } - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: typescript_server_binary_arguments(&server_path), - }) + })) } } @@ -737,8 +737,9 @@ impl LspInstaller for TypeScriptLspAdapter { latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(Self::NEW_SERVER_PATH); @@ -765,8 +766,13 @@ impl LspInstaller for TypeScriptLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_ts_server_binary(container_dir, &self.node).await + get_cached_ts_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -883,7 +889,7 @@ impl LspAdapter for TypeScriptLspAdapter { async fn get_cached_ts_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let old_server_path = container_dir.join(TypeScriptLspAdapter::OLD_SERVER_PATH); diff --git a/crates/languages/src/vtsls.rs b/crates/languages/src/vtsls.rs index d0a7d67fe30..7b2309bd817 100644 --- a/crates/languages/src/vtsls.rs +++ b/crates/languages/src/vtsls.rs @@ -98,9 +98,9 @@ impl LspInstaller for VtslsLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version(Self::PACKAGE_NAME) .await } @@ -125,8 +125,9 @@ impl LspInstaller for VtslsLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(Self::SERVER_PATH); @@ -147,8 +148,9 @@ impl LspInstaller for VtslsLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let server_version = version.clone(); let container_dir = container_dir.clone(); @@ -162,16 +164,16 @@ impl LspInstaller for VtslsLspAdapter { &container_dir, VersionStrategy::Latest(&server_version), ) - .await + .await? { - return None; + return Ok(None); } - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: typescript_server_binary_arguments(&server_path), - }) + })) } } @@ -179,8 +181,13 @@ impl LspInstaller for VtslsLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_ts_server_binary(container_dir, &self.node).await + get_cached_ts_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -373,7 +380,7 @@ impl LspAdapter for VtslsLspAdapter { async fn get_cached_ts_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(VtslsLspAdapter::SERVER_PATH); diff --git a/crates/languages/src/yaml.rs b/crates/languages/src/yaml.rs index 41586a469fd..24a8b7f7553 100644 --- a/crates/languages/src/yaml.rs +++ b/crates/languages/src/yaml.rs @@ -43,9 +43,9 @@ impl LspInstaller for YamlLspAdapter { &self, _: &Arc, _: bool, - _: &mut AsyncApp, + cx: &mut AsyncApp, ) -> Result { - self.node + crate::npm::NpmInstaller::new(&self.node, self, cx.clone()) .npm_package_latest_version("yaml-language-server") .await } @@ -71,8 +71,9 @@ impl LspInstaller for YamlLspAdapter { _latest_version: Self::BinaryVersion, container_dir: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); async move { let server_path = container_dir.join(SERVER_PATH); @@ -93,8 +94,9 @@ impl LspInstaller for YamlLspAdapter { version: &Self::BinaryVersion, container_dir: &PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { - let node = self.node.clone(); + cx: AsyncApp, + ) -> impl Future>> + use<> { + let node = crate::npm::NpmInstaller::new(&self.node, self, cx); let version = version.clone(); let container_dir = container_dir.clone(); @@ -108,16 +110,16 @@ impl LspInstaller for YamlLspAdapter { &container_dir, VersionStrategy::Latest(&version), ) - .await; + .await?; if should_install_language_server { - None + Ok(None) } else { - Some(LanguageServerBinary { - path: node.binary_path().await.ok()?, + Ok(Some(LanguageServerBinary { + path: node.binary_path().await?, env: None, arguments: server_binary_arguments(&server_path), - }) + })) } } } @@ -126,8 +128,13 @@ impl LspInstaller for YamlLspAdapter { &self, container_dir: PathBuf, _: &dyn LspAdapterDelegate, + cx: AsyncApp, ) -> Option { - get_cached_server_binary(container_dir, &self.node).await + get_cached_server_binary( + container_dir, + &crate::npm::NpmInstaller::new(&self.node, self, cx), + ) + .await } } @@ -205,7 +212,7 @@ fn worktree_root(delegate: &Arc, settings: Option async fn get_cached_server_binary( container_dir: PathBuf, - node: &NodeRuntime, + node: &crate::npm::NpmInstaller, ) -> Option { maybe!(async { let server_path = container_dir.join(SERVER_PATH); diff --git a/crates/node_runtime/src/node_runtime.rs b/crates/node_runtime/src/node_runtime.rs index f9145316cfd..0b2addf1f3d 100644 --- a/crates/node_runtime/src/node_runtime.rs +++ b/crates/node_runtime/src/node_runtime.rs @@ -179,6 +179,7 @@ impl ManagedResourceCommand for NodeBinaryPath { pub struct LatestNpmPackageVersion { pub package: String, + pub requirement: Option, } impl ManagedResourceCommand for LatestNpmPackageVersion { @@ -198,14 +199,20 @@ impl ManagedResourceCommand for LatestNpmPackageVersion { } fn permission_description(&self, _: &Npm) -> String { - format!( + let description = format!( "Query the npm registry for the latest version of {}", self.package - ) + ); + match &self.requirement { + Some(requirement) => format!("{description} matching {requirement}"), + None => description, + } } async fn run(self, npm: &Npm, _: &gpui::BackgroundExecutor) -> Self::Output { - npm.runtime.npm_package_latest_version(&self.package).await + npm.runtime + .npm_package_latest_version_with_requirement(&self.package, self.requirement.as_ref()) + .await } } @@ -1982,7 +1989,8 @@ mod tests { ); assert_eq!( LatestNpmPackageVersion { - package: "typescript".into() + package: "typescript".into(), + requirement: None, } .permission(&settings), Some(ResourcePermission::Denied) diff --git a/crates/node_runtime/tests/system_node.rs b/crates/node_runtime/tests/system_node.rs index 5dcb1d7496d..a2b53d347a3 100644 --- a/crates/node_runtime/tests/system_node.rs +++ b/crates/node_runtime/tests/system_node.rs @@ -191,7 +191,8 @@ fn managed_npm_obtains_and_installs_packages() -> Result<()> { ); assert!( node_runtime::LatestNpmPackageVersion { - package: "test-package".into() + package: "test-package".into(), + requirement: None, } .run(&resource, &executor) .await diff --git a/crates/project/src/debugger/session.rs b/crates/project/src/debugger/session.rs index f84cf676ee4..2517423169f 100644 --- a/crates/project/src/debugger/session.rs +++ b/crates/project/src/debugger/session.rs @@ -3393,6 +3393,7 @@ async fn get_or_install_companion(node: NodeRuntime, cx: &mut AsyncApp) -> Resul .execute_command( node_runtime::LatestNpmPackageVersion { package: PACKAGE_NAME.into(), + requirement: None, }, bill.clone(), cx, diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index c990e56859e..006cfeae6bf 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -16963,6 +16963,7 @@ impl LspInstaller for SshLspAdapter { &self, _: PathBuf, _: &dyn LspAdapterDelegate, + _: AsyncApp, ) -> Option { None } @@ -16981,7 +16982,8 @@ impl LspInstaller for SshLspAdapter { _: (), _: PathBuf, _: &Arc, - ) -> impl Send + Future> + use<> { + _: AsyncApp, + ) -> impl Future> + use<> { async { anyhow::bail!("SshLspAdapter does not support fetch_server_binary") } } }