Allow using system node (#18172)

Conrad Irwin and Mikayla created

Release Notes:

- (Potentially breaking change) Zed will now use the node installed on
your $PATH (if it is more recent than v18) instead of downloading its
own. You can disable the new behavior with `{"node":
{"disable_path_lookup": true}}` in your settings. We do not yet use
system/project-local node_modules.

---------

Co-authored-by: Mikayla <mikayla@zed.dev>

Change summary

Cargo.lock                                       |   4 
assets/settings/default.json                     |  15 
crates/collab/src/tests/test_server.rs           |   6 
crates/copilot/src/copilot.rs                    |  12 
crates/evals/src/eval.rs                         |   4 
crates/extension/src/extension_store.rs          |   4 
crates/extension/src/extension_store_test.rs     |   6 
crates/extension/src/wasm_host.rs                |   4 
crates/headless/src/headless.rs                  |   2 
crates/http_client/src/http_client.rs            |  29 
crates/language/src/language.rs                  |   1 
crates/languages/src/css.rs                      |  10 
crates/languages/src/json.rs                     |  10 
crates/languages/src/lib.rs                      |   6 
crates/languages/src/python.rs                   |  10 
crates/languages/src/tailwind.rs                 |  10 
crates/languages/src/typescript.rs               |  24 
crates/languages/src/vtsls.rs                    |  10 
crates/languages/src/yaml.rs                     |  10 
crates/markdown/examples/markdown.rs             |   4 
crates/markdown/examples/markdown_as_child.rs    |   4 
crates/node_runtime/Cargo.toml                   |   2 
crates/node_runtime/src/node_runtime.rs          | 646 +++++++++++------
crates/prettier/src/prettier.rs                  |   4 
crates/project/src/lsp_store.rs                  |  33 
crates/project/src/prettier_store.rs             |  18 
crates/project/src/project.rs                    |  12 
crates/project/src/project_settings.rs           |  15 
crates/remote_server/src/headless_project.rs     |   4 
crates/remote_server/src/remote_editing_tests.rs |   4 
crates/workspace/src/workspace.rs                |  10 
crates/zed/Cargo.toml                            |   2 
crates/zed/src/main.rs                           |  32 
crates/zed/src/zed.rs                            |   2 
34 files changed, 596 insertions(+), 373 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -7190,6 +7190,7 @@ dependencies = [
  "async-std",
  "async-tar",
  "async-trait",
+ "async-watch",
  "async_zip",
  "futures 0.3.30",
  "http_client",
@@ -7202,6 +7203,7 @@ dependencies = [
  "tempfile",
  "util",
  "walkdir",
+ "which 6.0.3",
  "windows 0.58.0",
 ]
 
@@ -14393,6 +14395,7 @@ dependencies = [
  "ashpd",
  "assets",
  "assistant",
+ "async-watch",
  "audio",
  "auto_update",
  "backtrace",
@@ -14466,6 +14469,7 @@ dependencies = [
  "session",
  "settings",
  "settings_ui",
+ "shellexpand 2.1.2",
  "simplelog",
  "smol",
  "snippet_provider",

assets/settings/default.json 🔗

@@ -771,6 +771,21 @@
       "pyrightconfig.json"
     ]
   },
+  /// By default use a recent system version of node, or install our own.
+  /// You can override this to use a version of node that is not in $PATH with:
+  /// {
+  ///   "node": {
+  ///     "node_path": "/path/to/node"
+  ///     "npm_path": "/path/to/npm" (defaults to node_path/../npm)
+  ///   }
+  /// }
+  /// or to ensure Zed always downloads and installs an isolated version of node:
+  /// {
+  ///   "node": {
+  ///     "disable_path_lookup": true
+  ///   }
+  /// NOTE: changing this setting currently requires restarting Zed.
+  "node": {},
   // The extensions that Zed should automatically install on startup.
   //
   // If you don't want any of these extensions, add this field to your settings

crates/collab/src/tests/test_server.rs 🔗

@@ -21,7 +21,7 @@ use git::GitHostingProviderRegistry;
 use gpui::{BackgroundExecutor, Context, Model, Task, TestAppContext, View, VisualTestContext};
 use http_client::FakeHttpClient;
 use language::LanguageRegistry;
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use notifications::NotificationStore;
 use parking_lot::Mutex;
 use project::{Project, WorktreeId};
@@ -278,7 +278,7 @@ impl TestServer {
             languages: language_registry,
             fs: fs.clone(),
             build_window_options: |_, _| Default::default(),
-            node_runtime: FakeNodeRuntime::new(),
+            node_runtime: NodeRuntime::unavailable(),
             session,
         });
 
@@ -408,7 +408,7 @@ impl TestServer {
             languages: language_registry,
             fs: fs.clone(),
             build_window_options: |_, _| Default::default(),
-            node_runtime: FakeNodeRuntime::new(),
+            node_runtime: NodeRuntime::unavailable(),
             session,
         });
 

crates/copilot/src/copilot.rs 🔗

@@ -57,7 +57,7 @@ pub fn init(
     new_server_id: LanguageServerId,
     fs: Arc<dyn Fs>,
     http: Arc<dyn HttpClient>,
-    node_runtime: Arc<dyn NodeRuntime>,
+    node_runtime: NodeRuntime,
     cx: &mut AppContext,
 ) {
     copilot_chat::init(fs, http.clone(), cx);
@@ -302,7 +302,7 @@ pub struct Completion {
 
 pub struct Copilot {
     http: Arc<dyn HttpClient>,
-    node_runtime: Arc<dyn NodeRuntime>,
+    node_runtime: NodeRuntime,
     server: CopilotServer,
     buffers: HashSet<WeakModel<Buffer>>,
     server_id: LanguageServerId,
@@ -334,7 +334,7 @@ impl Copilot {
     fn start(
         new_server_id: LanguageServerId,
         http: Arc<dyn HttpClient>,
-        node_runtime: Arc<dyn NodeRuntime>,
+        node_runtime: NodeRuntime,
         cx: &mut ModelContext<Self>,
     ) -> Self {
         let mut this = Self {
@@ -392,7 +392,7 @@ impl Copilot {
     #[cfg(any(test, feature = "test-support"))]
     pub fn fake(cx: &mut gpui::TestAppContext) -> (Model<Self>, lsp::FakeLanguageServer) {
         use lsp::FakeLanguageServer;
-        use node_runtime::FakeNodeRuntime;
+        use node_runtime::NodeRuntime;
 
         let (server, fake_server) = FakeLanguageServer::new(
             LanguageServerId(0),
@@ -406,7 +406,7 @@ impl Copilot {
             cx.to_async(),
         );
         let http = http_client::FakeHttpClient::create(|_| async { unreachable!() });
-        let node_runtime = FakeNodeRuntime::new();
+        let node_runtime = NodeRuntime::unavailable();
         let this = cx.new_model(|cx| Self {
             server_id: LanguageServerId(0),
             http: http.clone(),
@@ -425,7 +425,7 @@ impl Copilot {
     async fn start_language_server(
         new_server_id: LanguageServerId,
         http: Arc<dyn HttpClient>,
-        node_runtime: Arc<dyn NodeRuntime>,
+        node_runtime: NodeRuntime,
         this: WeakModel<Self>,
         mut cx: AsyncAppContext,
     ) {

crates/evals/src/eval.rs 🔗

@@ -9,7 +9,7 @@ use git::GitHostingProviderRegistry;
 use gpui::{AsyncAppContext, BackgroundExecutor, Context, Model};
 use http_client::{HttpClient, Method};
 use language::LanguageRegistry;
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use open_ai::OpenAiEmbeddingModel;
 use project::Project;
 use semantic_index::{
@@ -292,7 +292,7 @@ async fn run_evaluation(
     let user_store = cx
         .new_model(|cx| UserStore::new(client.clone(), cx))
         .unwrap();
-    let node_runtime = Arc::new(FakeNodeRuntime {});
+    let node_runtime = NodeRuntime::unavailable();
 
     let evaluations = fs::read(&evaluations_path).expect("failed to read evaluations.json");
     let evaluations: Vec<EvaluationProject> = serde_json::from_slice(&evaluations).unwrap();

crates/extension/src/extension_store.rs 🔗

@@ -177,7 +177,7 @@ actions!(zed, [ReloadExtensions]);
 pub fn init(
     fs: Arc<dyn Fs>,
     client: Arc<Client>,
-    node_runtime: Arc<dyn NodeRuntime>,
+    node_runtime: NodeRuntime,
     language_registry: Arc<LanguageRegistry>,
     theme_registry: Arc<ThemeRegistry>,
     cx: &mut AppContext,
@@ -228,7 +228,7 @@ impl ExtensionStore {
         http_client: Arc<HttpClientWithUrl>,
         builder_client: Arc<dyn HttpClient>,
         telemetry: Option<Arc<Telemetry>>,
-        node_runtime: Arc<dyn NodeRuntime>,
+        node_runtime: NodeRuntime,
         language_registry: Arc<LanguageRegistry>,
         theme_registry: Arc<ThemeRegistry>,
         slash_command_registry: Arc<SlashCommandRegistry>,

crates/extension/src/extension_store_test.rs 🔗

@@ -15,7 +15,7 @@ use http_client::{FakeHttpClient, Response};
 use indexed_docs::IndexedDocsRegistry;
 use isahc_http_client::IsahcHttpClient;
 use language::{LanguageMatcher, LanguageRegistry, LanguageServerBinaryStatus, LanguageServerName};
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use parking_lot::Mutex;
 use project::{Project, DEFAULT_COMPLETION_CONTEXT};
 use release_channel::AppVersion;
@@ -264,7 +264,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
     let slash_command_registry = SlashCommandRegistry::new();
     let indexed_docs_registry = Arc::new(IndexedDocsRegistry::new(cx.executor()));
     let snippet_registry = Arc::new(SnippetRegistry::new());
-    let node_runtime = FakeNodeRuntime::new();
+    let node_runtime = NodeRuntime::unavailable();
 
     let store = cx.new_model(|cx| {
         ExtensionStore::new(
@@ -490,7 +490,7 @@ async fn test_extension_store_with_test_extension(cx: &mut TestAppContext) {
     let slash_command_registry = SlashCommandRegistry::new();
     let indexed_docs_registry = Arc::new(IndexedDocsRegistry::new(cx.executor()));
     let snippet_registry = Arc::new(SnippetRegistry::new());
-    let node_runtime = FakeNodeRuntime::new();
+    let node_runtime = NodeRuntime::unavailable();
 
     let mut status_updates = language_registry.language_server_binary_statuses();
 

crates/extension/src/wasm_host.rs 🔗

@@ -33,7 +33,7 @@ pub(crate) struct WasmHost {
     engine: Engine,
     release_channel: ReleaseChannel,
     http_client: Arc<dyn HttpClient>,
-    node_runtime: Arc<dyn NodeRuntime>,
+    node_runtime: NodeRuntime,
     pub(crate) language_registry: Arc<LanguageRegistry>,
     fs: Arc<dyn Fs>,
     pub(crate) work_dir: PathBuf,
@@ -80,7 +80,7 @@ impl WasmHost {
     pub fn new(
         fs: Arc<dyn Fs>,
         http_client: Arc<dyn HttpClient>,
-        node_runtime: Arc<dyn NodeRuntime>,
+        node_runtime: NodeRuntime,
         language_registry: Arc<LanguageRegistry>,
         work_dir: PathBuf,
         cx: &mut AppContext,

crates/headless/src/headless.rs 🔗

@@ -25,7 +25,7 @@ pub struct DevServer {
 }
 
 pub struct AppState {
-    pub node_runtime: Arc<dyn NodeRuntime>,
+    pub node_runtime: NodeRuntime,
     pub user_store: Model<UserStore>,
     pub languages: Arc<LanguageRegistry>,
     pub fs: Arc<dyn Fs>,

crates/http_client/src/http_client.rs 🔗

@@ -264,6 +264,35 @@ pub fn read_proxy_from_env() -> Option<Uri> {
     None
 }
 
+pub struct BlockedHttpClient;
+
+impl HttpClient for BlockedHttpClient {
+    fn send(
+        &self,
+        _req: Request<AsyncBody>,
+    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
+        Box::pin(async {
+            Err(std::io::Error::new(
+                std::io::ErrorKind::PermissionDenied,
+                "BlockedHttpClient disallowed request",
+            )
+            .into())
+        })
+    }
+
+    fn proxy(&self) -> Option<&Uri> {
+        None
+    }
+
+    fn send_with_redirect_policy(
+        &self,
+        req: Request<AsyncBody>,
+        _: bool,
+    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
+        self.send(req)
+    }
+}
+
 #[cfg(feature = "test-support")]
 type FakeHttpHandler = Box<
     dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>>

crates/language/src/language.rs 🔗

@@ -564,6 +564,7 @@ async fn try_fetch_server_binary<L: LspAdapter + 'static + Send + Sync + ?Sized>
     let name = adapter.name();
     log::info!("fetching latest version of language server {:?}", name.0);
     delegate.update_status(name.clone(), LanguageServerBinaryStatus::CheckingForUpdate);
+
     let latest_version = adapter
         .fetch_latest_server_version(delegate.as_ref())
         .await?;

crates/languages/src/css.rs 🔗

@@ -22,11 +22,11 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct CssLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl CssLspAdapter {
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         CssLspAdapter { node }
     }
 }
@@ -81,14 +81,14 @@ impl LspAdapter for CssLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn initialization_options(
@@ -103,7 +103,7 @@ impl LspAdapter for CssLspAdapter {
 
 async fn get_cached_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let mut last_version_dir = None;

crates/languages/src/json.rs 🔗

@@ -59,13 +59,13 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct JsonLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
     languages: Arc<LanguageRegistry>,
     workspace_config: OnceLock<Value>,
 }
 
 impl JsonLspAdapter {
-    pub fn new(node: Arc<dyn NodeRuntime>, languages: Arc<LanguageRegistry>) -> Self {
+    pub fn new(node: NodeRuntime, languages: Arc<LanguageRegistry>) -> Self {
         Self {
             node,
             languages,
@@ -183,14 +183,14 @@ impl LspAdapter for JsonLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn initialization_options(
@@ -226,7 +226,7 @@ impl LspAdapter for JsonLspAdapter {
 
 async fn get_cached_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let mut last_version_dir = None;

crates/languages/src/lib.rs 🔗

@@ -30,11 +30,7 @@ mod yaml;
 #[exclude = "*.rs"]
 struct LanguageDir;
 
-pub fn init(
-    languages: Arc<LanguageRegistry>,
-    node_runtime: Arc<dyn NodeRuntime>,
-    cx: &mut AppContext,
-) {
+pub fn init(languages: Arc<LanguageRegistry>, node_runtime: NodeRuntime, cx: &mut AppContext) {
     languages.register_native_grammars([
         ("bash", tree_sitter_bash::LANGUAGE),
         ("c", tree_sitter_c::LANGUAGE),

crates/languages/src/python.rs 🔗

@@ -26,13 +26,13 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct PythonLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl PythonLspAdapter {
     const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("pyright");
 
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         PythonLspAdapter { node }
     }
 }
@@ -94,14 +94,14 @@ impl LspAdapter for PythonLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn process_completions(&self, items: &mut [lsp::CompletionItem]) {
@@ -198,7 +198,7 @@ impl LspAdapter for PythonLspAdapter {
 
 async fn get_cached_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     let server_path = container_dir.join(SERVER_PATH);
     if server_path.exists() {

crates/languages/src/tailwind.rs 🔗

@@ -28,14 +28,14 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct TailwindLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl TailwindLspAdapter {
     const SERVER_NAME: LanguageServerName =
         LanguageServerName::new_static("tailwindcss-language-server");
 
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         TailwindLspAdapter { node }
     }
 }
@@ -122,14 +122,14 @@ impl LspAdapter for TailwindLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn initialization_options(
@@ -198,7 +198,7 @@ impl LspAdapter for TailwindLspAdapter {
 
 async fn get_cached_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let mut last_version_dir = None;

crates/languages/src/typescript.rs 🔗

@@ -65,7 +65,7 @@ fn eslint_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct TypeScriptLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl TypeScriptLspAdapter {
@@ -73,7 +73,7 @@ impl TypeScriptLspAdapter {
     const NEW_SERVER_PATH: &'static str = "node_modules/typescript-language-server/lib/cli.mjs";
     const SERVER_NAME: LanguageServerName =
         LanguageServerName::new_static("typescript-language-server");
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         TypeScriptLspAdapter { node }
     }
     async fn tsdk_path(adapter: &Arc<dyn LspAdapterDelegate>) -> &'static str {
@@ -161,14 +161,14 @@ impl LspAdapter for TypeScriptLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_ts_server_binary(container_dir, &*self.node).await
+        get_cached_ts_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_ts_server_binary(container_dir, &*self.node).await
+        get_cached_ts_server_binary(container_dir, &self.node).await
     }
 
     fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
@@ -264,7 +264,7 @@ impl LspAdapter for TypeScriptLspAdapter {
 
 async fn get_cached_ts_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let old_server_path = container_dir.join(TypeScriptLspAdapter::OLD_SERVER_PATH);
@@ -293,7 +293,7 @@ async fn get_cached_ts_server_binary(
 }
 
 pub struct EsLintLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl EsLintLspAdapter {
@@ -310,7 +310,7 @@ impl EsLintLspAdapter {
     const FLAT_CONFIG_FILE_NAMES: &'static [&'static str] =
         &["eslint.config.js", "eslint.config.mjs", "eslint.config.cjs"];
 
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         EsLintLspAdapter { node }
     }
 }
@@ -476,11 +476,11 @@ impl LspAdapter for EsLintLspAdapter {
             }
 
             self.node
-                .run_npm_subcommand(Some(&repo_root), "install", &[])
+                .run_npm_subcommand(&repo_root, "install", &[])
                 .await?;
 
             self.node
-                .run_npm_subcommand(Some(&repo_root), "run-script", &["compile"])
+                .run_npm_subcommand(&repo_root, "run-script", &["compile"])
                 .await?;
         }
 
@@ -496,20 +496,20 @@ impl LspAdapter for EsLintLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_eslint_server_binary(container_dir, &*self.node).await
+        get_cached_eslint_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_eslint_server_binary(container_dir, &*self.node).await
+        get_cached_eslint_server_binary(container_dir, &self.node).await
     }
 }
 
 async fn get_cached_eslint_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         // This is unfortunate but we don't know what the version is to build a path directly

crates/languages/src/vtsls.rs 🔗

@@ -20,13 +20,13 @@ fn typescript_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct VtslsLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl VtslsLspAdapter {
     const SERVER_PATH: &'static str = "node_modules/@vtsls/language-server/bin/vtsls.js";
 
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         VtslsLspAdapter { node }
     }
     async fn tsdk_path(adapter: &Arc<dyn LspAdapterDelegate>) -> &'static str {
@@ -154,14 +154,14 @@ impl LspAdapter for VtslsLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_ts_server_binary(container_dir, &*self.node).await
+        get_cached_ts_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_ts_server_binary(container_dir, &*self.node).await
+        get_cached_ts_server_binary(container_dir, &self.node).await
     }
 
     fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
@@ -298,7 +298,7 @@ impl LspAdapter for VtslsLspAdapter {
 
 async fn get_cached_ts_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let server_path = container_dir.join(VtslsLspAdapter::SERVER_PATH);

crates/languages/src/yaml.rs 🔗

@@ -26,12 +26,12 @@ fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
 }
 
 pub struct YamlLspAdapter {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 }
 
 impl YamlLspAdapter {
     const SERVER_NAME: LanguageServerName = LanguageServerName::new_static("yaml-language-server");
-    pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
+    pub fn new(node: NodeRuntime) -> Self {
         YamlLspAdapter { node }
     }
 }
@@ -117,14 +117,14 @@ impl LspAdapter for YamlLspAdapter {
         container_dir: PathBuf,
         _: &dyn LspAdapterDelegate,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn installation_test_binary(
         &self,
         container_dir: PathBuf,
     ) -> Option<LanguageServerBinary> {
-        get_cached_server_binary(container_dir, &*self.node).await
+        get_cached_server_binary(container_dir, &self.node).await
     }
 
     async fn workspace_configuration(
@@ -157,7 +157,7 @@ impl LspAdapter for YamlLspAdapter {
 
 async fn get_cached_server_binary(
     container_dir: PathBuf,
-    node: &dyn NodeRuntime,
+    node: &NodeRuntime,
 ) -> Option<LanguageServerBinary> {
     maybe!(async {
         let mut last_version_dir = None;

crates/markdown/examples/markdown.rs 🔗

@@ -2,7 +2,7 @@ use assets::Assets;
 use gpui::{prelude::*, rgb, App, KeyBinding, StyleRefinement, View, WindowOptions};
 use language::{language_settings::AllLanguageSettings, LanguageRegistry};
 use markdown::{Markdown, MarkdownStyle};
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use settings::SettingsStore;
 use std::sync::Arc;
 use theme::LoadThemes;
@@ -102,7 +102,7 @@ pub fn main() {
         });
         cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
 
-        let node_runtime = FakeNodeRuntime::new();
+        let node_runtime = NodeRuntime::unavailable();
         theme::init(LoadThemes::JustBase, cx);
 
         let language_registry = LanguageRegistry::new(cx.background_executor().clone());

crates/markdown/examples/markdown_as_child.rs 🔗

@@ -2,7 +2,7 @@ use assets::Assets;
 use gpui::*;
 use language::{language_settings::AllLanguageSettings, LanguageRegistry};
 use markdown::{Markdown, MarkdownStyle};
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use settings::SettingsStore;
 use std::sync::Arc;
 use theme::LoadThemes;
@@ -28,7 +28,7 @@ pub fn main() {
         });
         cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
 
-        let node_runtime = FakeNodeRuntime::new();
+        let node_runtime = NodeRuntime::unavailable();
         let language_registry = Arc::new(LanguageRegistry::new(cx.background_executor().clone()));
         languages::init(language_registry.clone(), node_runtime, cx);
         theme::init(LoadThemes::JustBase, cx);

crates/node_runtime/Cargo.toml 🔗

@@ -18,6 +18,7 @@ test-support = ["tempfile"]
 [dependencies]
 anyhow.workspace = true
 async-compression.workspace = true
+async-watch.workspace = true
 async-tar.workspace = true
 async-trait.workspace = true
 async_zip.workspace = true
@@ -32,6 +33,7 @@ smol.workspace = true
 tempfile = { workspace = true, optional = true }
 util.workspace = true
 walkdir = "2.5.0"
+which.workspace = true
 
 [target.'cfg(windows)'.dependencies]
 async-std = { version = "1.12.0", features = ["unstable"] }

crates/node_runtime/src/node_runtime.rs 🔗

@@ -5,7 +5,7 @@ pub use archive::extract_zip;
 use async_compression::futures::bufread::GzipDecoder;
 use async_tar::Archive;
 use futures::AsyncReadExt;
-use http_client::HttpClient;
+use http_client::{HttpClient, Uri};
 use semver::Version;
 use serde::Deserialize;
 use smol::io::BufReader;
@@ -23,60 +23,166 @@ use util::ResultExt;
 #[cfg(windows)]
 use smol::process::windows::CommandExt;
 
-const VERSION: &str = "v22.5.1";
+#[derive(Clone, Debug, Default, Eq, PartialEq)]
+pub struct NodeBinaryOptions {
+    pub allow_path_lookup: bool,
+    pub allow_binary_download: bool,
+    pub use_paths: Option<(PathBuf, PathBuf)>,
+}
 
-#[cfg(not(windows))]
-const NODE_PATH: &str = "bin/node";
-#[cfg(windows)]
-const NODE_PATH: &str = "node.exe";
+#[derive(Clone)]
+pub struct NodeRuntime(Arc<Mutex<NodeRuntimeState>>);
 
-#[cfg(not(windows))]
-const NPM_PATH: &str = "bin/npm";
-#[cfg(windows)]
-const NPM_PATH: &str = "node_modules/npm/bin/npm-cli.js";
-
-enum ArchiveType {
-    TarGz,
-    Zip,
+struct NodeRuntimeState {
+    http: Arc<dyn HttpClient>,
+    instance: Option<Box<dyn NodeRuntimeTrait>>,
+    last_options: Option<NodeBinaryOptions>,
+    options: async_watch::Receiver<Option<NodeBinaryOptions>>,
 }
 
-#[derive(Debug, Deserialize)]
-#[serde(rename_all = "kebab-case")]
-pub struct NpmInfo {
-    #[serde(default)]
-    dist_tags: NpmInfoDistTags,
-    versions: Vec<String>,
-}
+impl NodeRuntime {
+    pub fn new(
+        http: Arc<dyn HttpClient>,
+        options: async_watch::Receiver<Option<NodeBinaryOptions>>,
+    ) -> Self {
+        NodeRuntime(Arc::new(Mutex::new(NodeRuntimeState {
+            http,
+            instance: None,
+            last_options: None,
+            options,
+        })))
+    }
 
-#[derive(Debug, Deserialize, Default)]
-pub struct NpmInfoDistTags {
-    latest: Option<String>,
-}
+    pub fn unavailable() -> Self {
+        NodeRuntime(Arc::new(Mutex::new(NodeRuntimeState {
+            http: Arc::new(http_client::BlockedHttpClient),
+            instance: None,
+            last_options: None,
+            options: async_watch::channel(Some(NodeBinaryOptions::default())).1,
+        })))
+    }
 
-#[async_trait::async_trait]
-pub trait NodeRuntime: Send + Sync {
-    async fn binary_path(&self) -> Result<PathBuf>;
-    async fn node_environment_path(&self) -> Result<OsString>;
+    async fn instance(&self) -> Result<Box<dyn NodeRuntimeTrait>> {
+        let mut state = self.0.lock().await;
 
-    async fn run_npm_subcommand(
+        while state.options.borrow().is_none() {
+            state.options.changed().await?;
+        }
+        let options = state.options.borrow().clone().unwrap();
+        if state.last_options.as_ref() != Some(&options) {
+            state.instance.take();
+        }
+        if let Some(instance) = state.instance.as_ref() {
+            return Ok(instance.boxed_clone());
+        }
+
+        if let Some((node, npm)) = options.use_paths.as_ref() {
+            let instance = SystemNodeRuntime::new(node.clone(), npm.clone()).await?;
+            state.instance = Some(instance.boxed_clone());
+            return Ok(instance);
+        }
+
+        if options.allow_path_lookup {
+            if let Some(instance) = SystemNodeRuntime::detect().await {
+                state.instance = Some(instance.boxed_clone());
+                return Ok(instance);
+            }
+        }
+
+        let instance = if options.allow_binary_download {
+            ManagedNodeRuntime::install_if_needed(&state.http).await?
+        } else {
+            Box::new(UnavailableNodeRuntime)
+        };
+
+        state.instance = Some(instance.boxed_clone());
+        return Ok(instance);
+    }
+
+    pub async fn binary_path(&self) -> Result<PathBuf> {
+        self.instance().await?.binary_path()
+    }
+
+    pub async fn run_npm_subcommand(
         &self,
-        directory: Option<&Path>,
+        directory: &Path,
         subcommand: &str,
         args: &[&str],
-    ) -> Result<Output>;
-
-    async fn npm_package_latest_version(&self, name: &str) -> Result<String>;
-
-    async fn npm_install_packages(&self, directory: &Path, packages: &[(&str, &str)])
-        -> Result<()>;
+    ) -> Result<Output> {
+        let http = self.0.lock().await.http.clone();
+        self.instance()
+            .await?
+            .run_npm_subcommand(Some(directory), http.proxy(), subcommand, args)
+            .await
+    }
 
-    async fn npm_package_installed_version(
+    pub async fn npm_package_installed_version(
         &self,
         local_package_directory: &Path,
         name: &str,
-    ) -> Result<Option<String>>;
+    ) -> Result<Option<String>> {
+        self.instance()
+            .await?
+            .npm_package_installed_version(local_package_directory, name)
+            .await
+    }
 
-    async fn should_install_npm_package(
+    pub async fn npm_package_latest_version(&self, name: &str) -> Result<String> {
+        let http = self.0.lock().await.http.clone();
+        let output = self
+            .instance()
+            .await?
+            .run_npm_subcommand(
+                None,
+                http.proxy(),
+                "info",
+                &[
+                    name,
+                    "--json",
+                    "--fetch-retry-mintimeout",
+                    "2000",
+                    "--fetch-retry-maxtimeout",
+                    "5000",
+                    "--fetch-timeout",
+                    "5000",
+                ],
+            )
+            .await?;
+
+        let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
+        info.dist_tags
+            .latest
+            .or_else(|| info.versions.pop())
+            .ok_or_else(|| anyhow!("no version found for npm package {}", name))
+    }
+
+    pub async fn npm_install_packages(
+        &self,
+        directory: &Path,
+        packages: &[(&str, &str)],
+    ) -> Result<()> {
+        let packages: Vec<_> = packages
+            .iter()
+            .map(|(name, version)| format!("{name}@{version}"))
+            .collect();
+
+        let mut arguments: Vec<_> = packages.iter().map(|p| p.as_str()).collect();
+        arguments.extend_from_slice(&[
+            "--save-exact",
+            "--fetch-retry-mintimeout",
+            "2000",
+            "--fetch-retry-maxtimeout",
+            "5000",
+            "--fetch-timeout",
+            "5000",
+        ]);
+
+        self.run_npm_subcommand(directory, "install", &arguments)
+            .await?;
+        Ok(())
+    }
+
+    pub async fn should_install_npm_package(
         &self,
         package_name: &str,
         local_executable_path: &Path,
@@ -110,21 +216,78 @@ pub trait NodeRuntime: Send + Sync {
     }
 }
 
-pub struct RealNodeRuntime {
-    http: Arc<dyn HttpClient>,
-    installation_lock: Mutex<()>,
+enum ArchiveType {
+    TarGz,
+    Zip,
 }
 
-impl RealNodeRuntime {
-    pub fn new(http: Arc<dyn HttpClient>) -> Arc<dyn NodeRuntime> {
-        Arc::new(RealNodeRuntime {
-            http,
-            installation_lock: Mutex::new(()),
-        })
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "kebab-case")]
+pub struct NpmInfo {
+    #[serde(default)]
+    dist_tags: NpmInfoDistTags,
+    versions: Vec<String>,
+}
+
+#[derive(Debug, Deserialize, Default)]
+pub struct NpmInfoDistTags {
+    latest: Option<String>,
+}
+
+#[async_trait::async_trait]
+trait NodeRuntimeTrait: Send + Sync {
+    fn boxed_clone(&self) -> Box<dyn NodeRuntimeTrait>;
+    fn binary_path(&self) -> Result<PathBuf>;
+
+    async fn run_npm_subcommand(
+        &self,
+        directory: Option<&Path>,
+        proxy: Option<&Uri>,
+        subcommand: &str,
+        args: &[&str],
+    ) -> Result<Output>;
+
+    async fn npm_package_installed_version(
+        &self,
+        local_package_directory: &Path,
+        name: &str,
+    ) -> Result<Option<String>>;
+}
+
+#[derive(Clone)]
+struct ManagedNodeRuntime {
+    installation_path: PathBuf,
+}
+
+impl ManagedNodeRuntime {
+    const VERSION: &str = "v22.5.1";
+
+    #[cfg(not(windows))]
+    const NODE_PATH: &str = "bin/node";
+    #[cfg(windows)]
+    const NODE_PATH: &str = "node.exe";
+
+    #[cfg(not(windows))]
+    const NPM_PATH: &str = "bin/npm";
+    #[cfg(windows)]
+    const NPM_PATH: &str = "node_modules/npm/bin/npm-cli.js";
+
+    async fn node_environment_path(&self) -> Result<OsString> {
+        let node_binary = self.installation_path.join(Self::NODE_PATH);
+        let mut env_path = vec![node_binary
+            .parent()
+            .expect("invalid node binary path")
+            .to_path_buf()];
+
+        if let Some(existing_path) = std::env::var_os("PATH") {
+            let mut paths = std::env::split_paths(&existing_path).collect::<Vec<_>>();
+            env_path.append(&mut paths);
+        }
+
+        std::env::join_paths(env_path).context("failed to create PATH env variable")
     }
 
-    async fn install_if_needed(&self) -> Result<PathBuf> {
-        let _lock = self.installation_lock.lock().await;
+    async fn install_if_needed(http: &Arc<dyn HttpClient>) -> Result<Box<dyn NodeRuntimeTrait>> {
         log::info!("Node runtime install_if_needed");
 
         let os = match consts::OS {
@@ -140,11 +303,12 @@ impl RealNodeRuntime {
             other => bail!("Running on unsupported architecture: {other}"),
         };
 
-        let folder_name = format!("node-{VERSION}-{os}-{arch}");
+        let version = Self::VERSION;
+        let folder_name = format!("node-{version}-{os}-{arch}");
         let node_containing_dir = paths::support_dir().join("node");
         let node_dir = node_containing_dir.join(folder_name);
-        let node_binary = node_dir.join(NODE_PATH);
-        let npm_file = node_dir.join(NPM_PATH);
+        let node_binary = node_dir.join(Self::NODE_PATH);
+        let npm_file = node_dir.join(Self::NPM_PATH);
 
         let mut command = Command::new(&node_binary);
 
@@ -177,16 +341,16 @@ impl RealNodeRuntime {
                 other => bail!("Running on unsupported os: {other}"),
             };
 
+            let version = Self::VERSION;
             let file_name = format!(
-                "node-{VERSION}-{os}-{arch}.{extension}",
+                "node-{version}-{os}-{arch}.{extension}",
                 extension = match archive_type {
                     ArchiveType::TarGz => "tar.gz",
                     ArchiveType::Zip => "zip",
                 }
             );
-            let url = format!("https://nodejs.org/dist/{VERSION}/{file_name}");
-            let mut response = self
-                .http
+            let url = format!("https://nodejs.org/dist/{version}/{file_name}");
+            let mut response = http
                 .get(&url, Default::default(), true)
                 .await
                 .context("error downloading Node binary tarball")?;
@@ -207,43 +371,32 @@ impl RealNodeRuntime {
         _ = fs::write(node_dir.join("blank_user_npmrc"), []).await;
         _ = fs::write(node_dir.join("blank_global_npmrc"), []).await;
 
-        anyhow::Ok(node_dir)
+        anyhow::Ok(Box::new(ManagedNodeRuntime {
+            installation_path: node_dir,
+        }))
     }
 }
 
 #[async_trait::async_trait]
-impl NodeRuntime for RealNodeRuntime {
-    async fn binary_path(&self) -> Result<PathBuf> {
-        let installation_path = self.install_if_needed().await?;
-        Ok(installation_path.join(NODE_PATH))
+impl NodeRuntimeTrait for ManagedNodeRuntime {
+    fn boxed_clone(&self) -> Box<dyn NodeRuntimeTrait> {
+        Box::new(self.clone())
     }
 
-    async fn node_environment_path(&self) -> Result<OsString> {
-        let installation_path = self.install_if_needed().await?;
-        let node_binary = installation_path.join(NODE_PATH);
-        let mut env_path = vec![node_binary
-            .parent()
-            .expect("invalid node binary path")
-            .to_path_buf()];
-
-        if let Some(existing_path) = std::env::var_os("PATH") {
-            let mut paths = std::env::split_paths(&existing_path).collect::<Vec<_>>();
-            env_path.append(&mut paths);
-        }
-
-        Ok(std::env::join_paths(env_path).context("failed to create PATH env variable")?)
+    fn binary_path(&self) -> Result<PathBuf> {
+        Ok(self.installation_path.join(Self::NODE_PATH))
     }
 
     async fn run_npm_subcommand(
         &self,
         directory: Option<&Path>,
+        proxy: Option<&Uri>,
         subcommand: &str,
         args: &[&str],
     ) -> Result<Output> {
         let attempt = || async move {
-            let installation_path = self.install_if_needed().await?;
-            let node_binary = installation_path.join(NODE_PATH);
-            let npm_file = installation_path.join(NPM_PATH);
+            let node_binary = self.installation_path.join(Self::NODE_PATH);
+            let npm_file = self.installation_path.join(Self::NPM_PATH);
             let env_path = self.node_environment_path().await?;
 
             if smol::fs::metadata(&node_binary).await.is_err() {
@@ -258,54 +411,17 @@ impl NodeRuntime for RealNodeRuntime {
             command.env_clear();
             command.env("PATH", env_path);
             command.arg(npm_file).arg(subcommand);
-            command.args(["--cache".into(), installation_path.join("cache")]);
+            command.args(["--cache".into(), self.installation_path.join("cache")]);
             command.args([
                 "--userconfig".into(),
-                installation_path.join("blank_user_npmrc"),
+                self.installation_path.join("blank_user_npmrc"),
             ]);
             command.args([
                 "--globalconfig".into(),
-                installation_path.join("blank_global_npmrc"),
+                self.installation_path.join("blank_global_npmrc"),
             ]);
             command.args(args);
-
-            if let Some(directory) = directory {
-                command.current_dir(directory);
-                command.args(["--prefix".into(), directory.to_path_buf()]);
-            }
-
-            if let Some(proxy) = self.http.proxy() {
-                // Map proxy settings from `http://localhost:10809` to `http://127.0.0.1:10809`
-                // NodeRuntime without environment information can not parse `localhost`
-                // correctly.
-                // TODO: map to `[::1]` if we are using ipv6
-                let proxy = proxy
-                    .to_string()
-                    .to_ascii_lowercase()
-                    .replace("localhost", "127.0.0.1");
-
-                command.args(["--proxy", &proxy]);
-            }
-
-            #[cfg(windows)]
-            {
-                // SYSTEMROOT is a critical environment variables for Windows.
-                if let Some(val) = std::env::var("SYSTEMROOT")
-                    .context("Missing environment variable: SYSTEMROOT!")
-                    .log_err()
-                {
-                    command.env("SYSTEMROOT", val);
-                }
-                // Without ComSpec, the post-install will always fail.
-                if let Some(val) = std::env::var("ComSpec")
-                    .context("Missing environment variable: ComSpec!")
-                    .log_err()
-                {
-                    command.env("ComSpec", val);
-                }
-                command.creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0);
-            }
-
+            configure_npm_command(&mut command, directory, proxy);
             command.output().await.map_err(|e| anyhow!("{e}"))
         };
 
@@ -332,182 +448,228 @@ impl NodeRuntime for RealNodeRuntime {
 
         output.map_err(|e| anyhow!("{e}"))
     }
-
-    async fn npm_package_latest_version(&self, name: &str) -> Result<String> {
-        let output = self
-            .run_npm_subcommand(
-                None,
-                "info",
-                &[
-                    name,
-                    "--json",
-                    "--fetch-retry-mintimeout",
-                    "2000",
-                    "--fetch-retry-maxtimeout",
-                    "5000",
-                    "--fetch-timeout",
-                    "5000",
-                ],
-            )
-            .await?;
-
-        let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
-        info.dist_tags
-            .latest
-            .or_else(|| info.versions.pop())
-            .ok_or_else(|| anyhow!("no version found for npm package {}", name))
-    }
-
     async fn npm_package_installed_version(
         &self,
         local_package_directory: &Path,
         name: &str,
     ) -> Result<Option<String>> {
-        let mut package_json_path = local_package_directory.to_owned();
-        package_json_path.extend(["node_modules", name, "package.json"]);
-
-        let mut file = match fs::File::open(package_json_path).await {
-            Ok(file) => file,
-            Err(err) => {
-                if err.kind() == io::ErrorKind::NotFound {
-                    return Ok(None);
-                }
+        read_package_installed_version(local_package_directory.join("node_modules"), name).await
+    }
+}
 
-                Err(err)?
-            }
-        };
+#[derive(Clone)]
+pub struct SystemNodeRuntime {
+    node: PathBuf,
+    npm: PathBuf,
+    global_node_modules: PathBuf,
+    scratch_dir: PathBuf,
+}
 
-        #[derive(Deserialize)]
-        struct PackageJson {
-            version: String,
+impl SystemNodeRuntime {
+    const MIN_VERSION: semver::Version = Version::new(18, 0, 0);
+    async fn new(node: PathBuf, npm: PathBuf) -> Result<Box<dyn NodeRuntimeTrait>> {
+        let output = Command::new(&node)
+            .arg("--version")
+            .output()
+            .await
+            .with_context(|| format!("running node from {:?}", node))?;
+        if !output.status.success() {
+            anyhow::bail!(
+                "failed to run node --version. stdout: {}, stderr: {}",
+                String::from_utf8_lossy(&output.stdout),
+                String::from_utf8_lossy(&output.stderr),
+            );
+        }
+        let version_str = String::from_utf8_lossy(&output.stdout);
+        let version = semver::Version::parse(version_str.trim().trim_start_matches('v'))?;
+        if version < Self::MIN_VERSION {
+            anyhow::bail!(
+                "node at {} is too old. want: {}, got: {}",
+                node.to_string_lossy(),
+                Self::MIN_VERSION,
+                version
+            )
         }
 
-        let mut contents = String::new();
-        file.read_to_string(&mut contents).await?;
-        let package_json: PackageJson = serde_json::from_str(&contents)?;
-        Ok(Some(package_json.version))
-    }
-
-    async fn npm_install_packages(
-        &self,
-        directory: &Path,
-        packages: &[(&str, &str)],
-    ) -> Result<()> {
-        let packages: Vec<_> = packages
-            .iter()
-            .map(|(name, version)| format!("{name}@{version}"))
-            .collect();
+        let scratch_dir = paths::support_dir().join("node");
+        fs::create_dir(&scratch_dir).await.ok();
+        fs::create_dir(scratch_dir.join("cache")).await.ok();
+        fs::write(scratch_dir.join("blank_user_npmrc"), [])
+            .await
+            .ok();
+        fs::write(scratch_dir.join("blank_global_npmrc"), [])
+            .await
+            .ok();
 
-        let mut arguments: Vec<_> = packages.iter().map(|p| p.as_str()).collect();
-        arguments.extend_from_slice(&[
-            "--save-exact",
-            "--fetch-retry-mintimeout",
-            "2000",
-            "--fetch-retry-maxtimeout",
-            "5000",
-            "--fetch-timeout",
-            "5000",
-        ]);
+        let mut this = Self {
+            node,
+            npm,
+            global_node_modules: PathBuf::default(),
+            scratch_dir,
+        };
+        let output = this.run_npm_subcommand(None, None, "root", &["-g"]).await?;
+        this.global_node_modules =
+            PathBuf::from(String::from_utf8_lossy(&output.stdout).to_string());
 
-        self.run_npm_subcommand(Some(directory), "install", &arguments)
-            .await?;
-        Ok(())
+        Ok(Box::new(this))
     }
-}
-
-pub struct FakeNodeRuntime;
 
-impl FakeNodeRuntime {
-    pub fn new() -> Arc<dyn NodeRuntime> {
-        Arc::new(Self)
+    async fn detect() -> Option<Box<dyn NodeRuntimeTrait>> {
+        let node = which::which("node").ok()?;
+        let npm = which::which("npm").ok()?;
+        Self::new(node, npm).await.log_err()
     }
 }
 
 #[async_trait::async_trait]
-impl NodeRuntime for FakeNodeRuntime {
-    async fn binary_path(&self) -> anyhow::Result<PathBuf> {
-        unreachable!()
+impl NodeRuntimeTrait for SystemNodeRuntime {
+    fn boxed_clone(&self) -> Box<dyn NodeRuntimeTrait> {
+        Box::new(self.clone())
     }
 
-    async fn node_environment_path(&self) -> anyhow::Result<OsString> {
-        unreachable!()
+    fn binary_path(&self) -> Result<PathBuf> {
+        Ok(self.node.clone())
     }
 
     async fn run_npm_subcommand(
         &self,
-        _: Option<&Path>,
+        directory: Option<&Path>,
+        proxy: Option<&Uri>,
         subcommand: &str,
         args: &[&str],
     ) -> anyhow::Result<Output> {
-        unreachable!("Should not run npm subcommand '{subcommand}' with args {args:?}")
-    }
+        let mut command = Command::new(self.node.clone());
+        command
+            .env_clear()
+            .env("PATH", std::env::var_os("PATH").unwrap_or_default())
+            .arg(self.npm.clone())
+            .arg(subcommand)
+            .args(["--cache".into(), self.scratch_dir.join("cache")])
+            .args([
+                "--userconfig".into(),
+                self.scratch_dir.join("blank_user_npmrc"),
+            ])
+            .args([
+                "--globalconfig".into(),
+                self.scratch_dir.join("blank_global_npmrc"),
+            ])
+            .args(args);
+        configure_npm_command(&mut command, directory, proxy);
+        let output = command.output().await?;
+        if !output.status.success() {
+            return Err(anyhow!(
+                "failed to execute npm {subcommand} subcommand:\nstdout: {:?}\nstderr: {:?}",
+                String::from_utf8_lossy(&output.stdout),
+                String::from_utf8_lossy(&output.stderr)
+            ));
+        }
 
-    async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> {
-        unreachable!("Should not query npm package '{name}' for latest version")
+        Ok(output)
     }
 
     async fn npm_package_installed_version(
         &self,
-        _local_package_directory: &Path,
+        local_package_directory: &Path,
         name: &str,
     ) -> Result<Option<String>> {
-        unreachable!("Should not query npm package '{name}' for installed version")
-    }
-
-    async fn npm_install_packages(
-        &self,
-        _: &Path,
-        packages: &[(&str, &str)],
-    ) -> anyhow::Result<()> {
-        unreachable!("Should not install packages {packages:?}")
+        read_package_installed_version(local_package_directory.join("node_modules"), name).await
+        // todo: allow returning a globally installed version (requires callers not to hard-code the path)
     }
 }
 
-// TODO: Remove this when headless binary can  run node
-pub struct DummyNodeRuntime;
+async fn read_package_installed_version(
+    node_module_directory: PathBuf,
+    name: &str,
+) -> Result<Option<String>> {
+    let package_json_path = node_module_directory.join(name).join("package.json");
+
+    let mut file = match fs::File::open(package_json_path).await {
+        Ok(file) => file,
+        Err(err) => {
+            if err.kind() == io::ErrorKind::NotFound {
+                return Ok(None);
+            }
+
+            Err(err)?
+        }
+    };
 
-impl DummyNodeRuntime {
-    pub fn new() -> Arc<dyn NodeRuntime> {
-        Arc::new(Self)
+    #[derive(Deserialize)]
+    struct PackageJson {
+        version: String,
     }
+
+    let mut contents = String::new();
+    file.read_to_string(&mut contents).await?;
+    let package_json: PackageJson = serde_json::from_str(&contents)?;
+    Ok(Some(package_json.version))
 }
 
+pub struct UnavailableNodeRuntime;
+
 #[async_trait::async_trait]
-impl NodeRuntime for DummyNodeRuntime {
-    async fn binary_path(&self) -> anyhow::Result<PathBuf> {
-        anyhow::bail!("Dummy Node Runtime")
+impl NodeRuntimeTrait for UnavailableNodeRuntime {
+    fn boxed_clone(&self) -> Box<dyn NodeRuntimeTrait> {
+        Box::new(UnavailableNodeRuntime)
     }
-
-    async fn node_environment_path(&self) -> anyhow::Result<OsString> {
-        anyhow::bail!("Dummy node runtime")
+    fn binary_path(&self) -> Result<PathBuf> {
+        bail!("binary_path: no node runtime available")
     }
 
     async fn run_npm_subcommand(
         &self,
         _: Option<&Path>,
-        _subcommand: &str,
-        _args: &[&str],
+        _: Option<&Uri>,
+        _: &str,
+        _: &[&str],
     ) -> anyhow::Result<Output> {
-        anyhow::bail!("Dummy node runtime")
-    }
-
-    async fn npm_package_latest_version(&self, _name: &str) -> anyhow::Result<String> {
-        anyhow::bail!("Dummy node runtime")
+        bail!("run_npm_subcommand: no node runtime available")
     }
 
     async fn npm_package_installed_version(
         &self,
         _local_package_directory: &Path,
-        _name: &str,
+        _: &str,
     ) -> Result<Option<String>> {
-        anyhow::bail!("Dummy node runtime")
+        bail!("npm_package_installed_version: no node runtime available")
     }
+}
 
-    async fn npm_install_packages(
-        &self,
-        _: &Path,
-        _packages: &[(&str, &str)],
-    ) -> anyhow::Result<()> {
-        anyhow::bail!("Dummy node runtime")
+fn configure_npm_command(command: &mut Command, directory: Option<&Path>, proxy: Option<&Uri>) {
+    if let Some(directory) = directory {
+        command.current_dir(directory);
+        command.args(["--prefix".into(), directory.to_path_buf()]);
+    }
+
+    if let Some(proxy) = proxy {
+        // Map proxy settings from `http://localhost:10809` to `http://127.0.0.1:10809`
+        // NodeRuntime without environment information can not parse `localhost`
+        // correctly.
+        // TODO: map to `[::1]` if we are using ipv6
+        let proxy = proxy
+            .to_string()
+            .to_ascii_lowercase()
+            .replace("localhost", "127.0.0.1");
+
+        command.args(["--proxy", &proxy]);
+    }
+
+    #[cfg(windows)]
+    {
+        // SYSTEMROOT is a critical environment variables for Windows.
+        if let Some(val) = std::env::var("SYSTEMROOT")
+            .context("Missing environment variable: SYSTEMROOT!")
+            .log_err()
+        {
+            command.env("SYSTEMROOT", val);
+        }
+        // Without ComSpec, the post-install will always fail.
+        if let Some(val) = std::env::var("ComSpec")
+            .context("Missing environment variable: ComSpec!")
+            .log_err()
+        {
+            command.env("ComSpec", val);
+        }
+        command.creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0);
     }
 }

crates/prettier/src/prettier.rs 🔗

@@ -138,7 +138,7 @@ impl Prettier {
     pub async fn start(
         _: LanguageServerId,
         prettier_dir: PathBuf,
-        _: Arc<dyn NodeRuntime>,
+        _: NodeRuntime,
         _: AsyncAppContext,
     ) -> anyhow::Result<Self> {
         Ok(Self::Test(TestPrettier {
@@ -151,7 +151,7 @@ impl Prettier {
     pub async fn start(
         server_id: LanguageServerId,
         prettier_dir: PathBuf,
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         cx: AsyncAppContext,
     ) -> anyhow::Result<Self> {
         use lsp::LanguageServerBinary;

crates/project/src/lsp_store.rs 🔗

@@ -17,7 +17,7 @@ use async_trait::async_trait;
 use client::{proto, TypedEnvelope};
 use collections::{btree_map, BTreeMap, HashMap, HashSet};
 use futures::{
-    future::{join_all, BoxFuture, Shared},
+    future::{join_all, Shared},
     select,
     stream::FuturesUnordered,
     AsyncWriteExt, Future, FutureExt, StreamExt,
@@ -27,7 +27,7 @@ use gpui::{
     AppContext, AsyncAppContext, Context, Entity, EventEmitter, Model, ModelContext, PromptLevel,
     Task, WeakModel,
 };
-use http_client::{AsyncBody, HttpClient, Request, Response, Uri};
+use http_client::{BlockedHttpClient, HttpClient};
 use language::{
     language_settings::{
         all_language_settings, language_settings, AllLanguageSettings, FormatOnSave, Formatter,
@@ -7979,35 +7979,6 @@ impl LspAdapterDelegate for LocalLspAdapterDelegate {
     }
 }
 
-struct BlockedHttpClient;
-
-impl HttpClient for BlockedHttpClient {
-    fn send(
-        &self,
-        _req: Request<AsyncBody>,
-    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
-        Box::pin(async {
-            Err(std::io::Error::new(
-                std::io::ErrorKind::PermissionDenied,
-                "ssh host blocked http connection",
-            )
-            .into())
-        })
-    }
-
-    fn proxy(&self) -> Option<&Uri> {
-        None
-    }
-
-    fn send_with_redirect_policy(
-        &self,
-        req: Request<AsyncBody>,
-        _: bool,
-    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
-        self.send(req)
-    }
-}
-
 struct SshLspAdapterDelegate {
     lsp_store: WeakModel<LspStore>,
     worktree: worktree::Snapshot,

crates/project/src/prettier_store.rs 🔗

@@ -30,7 +30,7 @@ use crate::{
 };
 
 pub struct PrettierStore {
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
     fs: Arc<dyn Fs>,
     languages: Arc<LanguageRegistry>,
     worktree_store: Model<WorktreeStore>,
@@ -52,7 +52,7 @@ impl EventEmitter<PrettierStoreEvent> for PrettierStore {}
 
 impl PrettierStore {
     pub fn new(
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         fs: Arc<dyn Fs>,
         languages: Arc<LanguageRegistry>,
         worktree_store: Model<WorktreeStore>,
@@ -212,7 +212,7 @@ impl PrettierStore {
     }
 
     fn start_prettier(
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         prettier_dir: PathBuf,
         worktree_id: Option<WorktreeId>,
         cx: &mut ModelContext<Self>,
@@ -241,7 +241,7 @@ impl PrettierStore {
     }
 
     fn start_default_prettier(
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         worktree_id: Option<WorktreeId>,
         cx: &mut ModelContext<PrettierStore>,
     ) -> Task<anyhow::Result<PrettierTask>> {
@@ -749,7 +749,7 @@ impl DefaultPrettier {
 
     pub fn prettier_task(
         &mut self,
-        node: &Arc<dyn NodeRuntime>,
+        node: &NodeRuntime,
         worktree_id: Option<WorktreeId>,
         cx: &mut ModelContext<PrettierStore>,
     ) -> Option<Task<anyhow::Result<PrettierTask>>> {
@@ -767,7 +767,7 @@ impl DefaultPrettier {
 impl PrettierInstance {
     pub fn prettier_task(
         &mut self,
-        node: &Arc<dyn NodeRuntime>,
+        node: &NodeRuntime,
         prettier_dir: Option<&Path>,
         worktree_id: Option<WorktreeId>,
         cx: &mut ModelContext<PrettierStore>,
@@ -786,7 +786,7 @@ impl PrettierInstance {
             None => match prettier_dir {
                 Some(prettier_dir) => {
                     let new_task = PrettierStore::start_prettier(
-                        Arc::clone(node),
+                        node.clone(),
                         prettier_dir.to_path_buf(),
                         worktree_id,
                         cx,
@@ -797,7 +797,7 @@ impl PrettierInstance {
                 }
                 None => {
                     self.attempt += 1;
-                    let node = Arc::clone(node);
+                    let node = node.clone();
                     cx.spawn(|prettier_store, mut cx| async move {
                         prettier_store
                             .update(&mut cx, |_, cx| {
@@ -818,7 +818,7 @@ impl PrettierInstance {
 async fn install_prettier_packages(
     fs: &dyn Fs,
     plugins_to_install: HashSet<Arc<str>>,
-    node: Arc<dyn NodeRuntime>,
+    node: NodeRuntime,
 ) -> anyhow::Result<()> {
     let packages_to_versions = future::try_join_all(
         plugins_to_install

crates/project/src/project.rs 🔗

@@ -153,7 +153,7 @@ pub struct Project {
     git_diff_debouncer: DebouncedDelay<Self>,
     remotely_created_models: Arc<Mutex<RemotelyCreatedModels>>,
     terminals: Terminals,
-    node: Option<Arc<dyn NodeRuntime>>,
+    node: Option<NodeRuntime>,
     tasks: Model<Inventory>,
     hosted_project_id: Option<ProjectId>,
     dev_server_project_id: Option<client::DevServerProjectId>,
@@ -579,7 +579,7 @@ impl Project {
 
     pub fn local(
         client: Arc<Client>,
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         user_store: Model<UserStore>,
         languages: Arc<LanguageRegistry>,
         fs: Arc<dyn Fs>,
@@ -675,7 +675,7 @@ impl Project {
     pub fn ssh(
         ssh: Arc<SshSession>,
         client: Arc<Client>,
-        node: Arc<dyn NodeRuntime>,
+        node: NodeRuntime,
         user_store: Model<UserStore>,
         languages: Arc<LanguageRegistry>,
         fs: Arc<dyn Fs>,
@@ -1064,7 +1064,7 @@ impl Project {
             .update(|cx| {
                 Project::local(
                     client,
-                    node_runtime::FakeNodeRuntime::new(),
+                    node_runtime::NodeRuntime::unavailable(),
                     user_store,
                     Arc::new(languages),
                     fs,
@@ -1104,7 +1104,7 @@ impl Project {
         let project = cx.update(|cx| {
             Project::local(
                 client,
-                node_runtime::FakeNodeRuntime::new(),
+                node_runtime::NodeRuntime::unavailable(),
                 user_store,
                 Arc::new(languages),
                 fs,
@@ -1157,7 +1157,7 @@ impl Project {
         self.user_store.clone()
     }
 
-    pub fn node_runtime(&self) -> Option<&Arc<dyn NodeRuntime>> {
+    pub fn node_runtime(&self) -> Option<&NodeRuntime> {
         self.node.as_ref()
     }
 

crates/project/src/project_settings.rs 🔗

@@ -34,6 +34,10 @@ pub struct ProjectSettings {
     #[serde(default)]
     pub git: GitSettings,
 
+    /// Configuration for Node-related features
+    #[serde(default)]
+    pub node: NodeBinarySettings,
+
     /// Configuration for how direnv configuration should be loaded
     #[serde(default)]
     pub load_direnv: DirenvSettings,
@@ -43,6 +47,17 @@ pub struct ProjectSettings {
     pub session: SessionSettings,
 }
 
+#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
+pub struct NodeBinarySettings {
+    /// The path to the node binary
+    pub path: Option<String>,
+    ///  The path to the npm binary Zed should use (defaults to .path/../npm)
+    pub npm_path: Option<String>,
+    /// If disabled, zed will download its own copy of node.
+    #[serde(default)]
+    pub disable_path_lookup: Option<bool>,
+}
+
 #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
 #[serde(rename_all = "snake_case")]
 pub enum DirenvSettings {

crates/remote_server/src/headless_project.rs 🔗

@@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
 use fs::Fs;
 use gpui::{AppContext, AsyncAppContext, Context, Model, ModelContext};
 use language::{proto::serialize_operation, Buffer, BufferEvent, LanguageRegistry};
-use node_runtime::DummyNodeRuntime;
+use node_runtime::NodeRuntime;
 use project::{
     buffer_store::{BufferStore, BufferStoreEvent},
     project_settings::SettingsObserver,
@@ -57,7 +57,7 @@ impl HeadlessProject {
         });
         let prettier_store = cx.new_model(|cx| {
             PrettierStore::new(
-                DummyNodeRuntime::new(),
+                NodeRuntime::unavailable(),
                 fs.clone(),
                 languages.clone(),
                 worktree_store.clone(),

crates/remote_server/src/remote_editing_tests.rs 🔗

@@ -9,7 +9,7 @@ use language::{
     Buffer, FakeLspAdapter, LanguageConfig, LanguageMatcher, LanguageRegistry, LanguageServerName,
 };
 use lsp::{CompletionContext, CompletionResponse, CompletionTriggerKind};
-use node_runtime::FakeNodeRuntime;
+use node_runtime::NodeRuntime;
 use project::{
     search::{SearchQuery, SearchResult},
     Project,
@@ -502,7 +502,7 @@ fn build_project(ssh: Arc<SshSession>, cx: &mut TestAppContext) -> Model<Project
         )
     });
 
-    let node = FakeNodeRuntime::new();
+    let node = NodeRuntime::unavailable();
     let user_store = cx.new_model(|cx| UserStore::new(client.clone(), cx));
     let languages = Arc::new(LanguageRegistry::test(cx.executor()));
     let fs = FakeFs::new(cx.executor());

crates/workspace/src/workspace.rs 🔗

@@ -556,7 +556,7 @@ pub struct AppState {
     pub workspace_store: Model<WorkspaceStore>,
     pub fs: Arc<dyn fs::Fs>,
     pub build_window_options: fn(Option<Uuid>, &mut AppContext) -> WindowOptions,
-    pub node_runtime: Arc<dyn NodeRuntime>,
+    pub node_runtime: NodeRuntime,
     pub session: Model<AppSession>,
 }
 
@@ -590,7 +590,7 @@ impl AppState {
 
     #[cfg(any(test, feature = "test-support"))]
     pub fn test(cx: &mut AppContext) -> Arc<Self> {
-        use node_runtime::FakeNodeRuntime;
+        use node_runtime::NodeRuntime;
         use session::Session;
         use settings::SettingsStore;
         use ui::Context as _;
@@ -619,7 +619,7 @@ impl AppState {
             languages,
             user_store,
             workspace_store,
-            node_runtime: FakeNodeRuntime::new(),
+            node_runtime: NodeRuntime::unavailable(),
             build_window_options: |_, _| Default::default(),
             session,
         })
@@ -4418,7 +4418,7 @@ impl Workspace {
 
     #[cfg(any(test, feature = "test-support"))]
     pub fn test_new(project: Model<Project>, cx: &mut ViewContext<Self>) -> Self {
-        use node_runtime::FakeNodeRuntime;
+        use node_runtime::NodeRuntime;
         use session::Session;
 
         let client = project.read(cx).client();
@@ -4434,7 +4434,7 @@ impl Workspace {
             user_store,
             fs: project.read(cx).fs().clone(),
             build_window_options: |_, _| Default::default(),
-            node_runtime: FakeNodeRuntime::new(),
+            node_runtime: NodeRuntime::unavailable(),
             session,
         });
         let workspace = Self::new(Default::default(), project, app_state, cx);

crates/zed/Cargo.toml 🔗

@@ -19,6 +19,7 @@ activity_indicator.workspace = true
 anyhow.workspace = true
 assets.workspace = true
 assistant.workspace = true
+async-watch.workspace = true
 audio.workspace = true
 auto_update.workspace = true
 backtrace = "0.3"
@@ -92,6 +93,7 @@ serde_json.workspace = true
 session.workspace = true
 settings.workspace = true
 settings_ui.workspace = true
+shellexpand.workspace = true
 simplelog.workspace = true
 smol.workspace = true
 snippet_provider.workspace = true

crates/zed/src/main.rs 🔗

@@ -29,8 +29,9 @@ use language::LanguageRegistry;
 use log::LevelFilter;
 
 use assets::Assets;
-use node_runtime::RealNodeRuntime;
+use node_runtime::{NodeBinaryOptions, NodeRuntime};
 use parking_lot::Mutex;
+use project::project_settings::ProjectSettings;
 use recent_projects::open_ssh_project;
 use release_channel::{AppCommitSha, AppVersion};
 use session::{AppSession, Session};
@@ -43,7 +44,7 @@ use std::{
     env,
     fs::OpenOptions,
     io::{IsTerminal, Write},
-    path::Path,
+    path::{Path, PathBuf},
     process,
     sync::Arc,
 };
@@ -477,7 +478,32 @@ fn main() {
         let mut languages = LanguageRegistry::new(cx.background_executor().clone());
         languages.set_language_server_download_dir(paths::languages_dir().clone());
         let languages = Arc::new(languages);
-        let node_runtime = RealNodeRuntime::new(client.http_client());
+        let (tx, rx) = async_watch::channel(None);
+        cx.observe_global::<SettingsStore>(move |cx| {
+            let settings = &ProjectSettings::get_global(cx).node;
+            let options = NodeBinaryOptions {
+                allow_path_lookup: !settings.disable_path_lookup.unwrap_or_default(),
+                // TODO: Expose this setting
+                allow_binary_download: true,
+                use_paths: settings.path.as_ref().map(|node_path| {
+                    let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
+                    let npm_path = settings
+                        .npm_path
+                        .as_ref()
+                        .map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
+                    (
+                        node_path.clone(),
+                        npm_path.unwrap_or_else(|| {
+                            let base_path = PathBuf::new();
+                            node_path.parent().unwrap_or(&base_path).join("npm")
+                        }),
+                    )
+                }),
+            };
+            tx.send(Some(options)).log_err();
+        })
+        .detach();
+        let node_runtime = NodeRuntime::new(client.http_client(), rx);
 
         language::init(cx);
         languages::init(languages.clone(), node_runtime.clone(), cx);

crates/zed/src/zed.rs 🔗

@@ -3365,7 +3365,7 @@ mod tests {
         cx.set_global(settings);
         let languages = LanguageRegistry::test(cx.executor());
         let languages = Arc::new(languages);
-        let node_runtime = node_runtime::FakeNodeRuntime::new();
+        let node_runtime = node_runtime::NodeRuntime::unavailable();
         cx.update(|cx| {
             languages::init(languages.clone(), node_runtime, cx);
         });