diff --git a/crates/project/src/agent_server_store.rs b/crates/project/src/agent_server_store.rs index 8b10f8bb54a..a4c45070b4c 100644 --- a/crates/project/src/agent_server_store.rs +++ b/crates/project/src/agent_server_store.rs @@ -1388,21 +1388,36 @@ impl ExternalAgentServer for LocalRegistryNpxAgent { .join("registry") .join("npx") .join(sanitize_path_component(®istry_id)); + + // Every project and connection calls this, and concurrent `npm install`s in the same + // directory can leave packages half-extracted (e.g. without a package.json). + let install_lock = npx_install_lock(&install_dir); + let _install_guard = install_lock.lock().await; + fs.create_dir(&install_dir).await?; let (package_name, package_spec) = bounded_npm_package_spec(&package); - node_runtime - .run_npm_subcommand( - Some(&install_dir), - "install", - &[package_spec.as_str(), "--save-exact"], + let node_modules_dir = install_dir.join("node_modules"); + install_npx_package(&node_runtime, &install_dir, &package_spec).await?; + if let Some(incomplete_package_dir) = + find_incomplete_npm_package(fs.as_ref(), &node_modules_dir).await? + { + log::warn!( + "npm package at {incomplete_package_dir:?} is missing its package.json, reinstalling {package_name}" + ); + fs.remove_dir( + &node_modules_dir, + RemoveOptions { + recursive: true, + ignore_if_not_exists: true, + }, ) - .await?; - let executable = node_runtime::read_package_executable( - install_dir.join("node_modules"), - package_name, - ) - .await?; + .await + .with_context(|| format!("removing {node_modules_dir:?}"))?; + install_npx_package(&node_runtime, &install_dir, &package_spec).await?; + } + let executable = + node_runtime::read_package_executable(node_modules_dir, package_name).await?; let node_binary = node_runtime.binary_path().await?; env.extend(node_runtime::npm_command_env(&node_binary)); @@ -1433,6 +1448,81 @@ impl ExternalAgentServer for LocalRegistryNpxAgent { } } +static NPX_INSTALL_LOCKS: LazyLock< + parking_lot::Mutex>>>, +> = LazyLock::new(Default::default); + +fn npx_install_lock(install_dir: &Path) -> Arc> { + NPX_INSTALL_LOCKS + .lock() + .entry(install_dir.to_path_buf()) + .or_default() + .clone() +} + +async fn install_npx_package( + node_runtime: &NodeRuntime, + install_dir: &Path, + package_spec: &str, +) -> Result<()> { + node_runtime + .run_npm_subcommand( + Some(install_dir), + "install", + &[package_spec, "--save-exact"], + ) + .await?; + Ok(()) +} + +/// Returns the first top-level package directory in `node_modules` that has no package.json. +/// +/// Node resolves such a package to `index.js`, so the agent fails to start with +/// `ERR_MODULE_NOT_FOUND`. `npm install` doesn't repair this on its own when it trusts its +/// hidden lockfile (`node_modules/.package-lock.json`) over the package folders. +async fn find_incomplete_npm_package( + fs: &dyn Fs, + node_modules_dir: &Path, +) -> Result> { + let mut package_dirs = Vec::new(); + for entry in npm_package_dir_entries(fs, node_modules_dir).await? { + let is_scope = entry + .file_name() + .is_some_and(|name| name.to_string_lossy().starts_with('@')); + if is_scope { + package_dirs.extend(npm_package_dir_entries(fs, &entry).await?); + } else { + package_dirs.push(entry); + } + } + + for package_dir in package_dirs { + if !fs.is_file(&package_dir.join("package.json")).await { + return Ok(Some(package_dir)); + } + } + Ok(None) +} + +async fn npm_package_dir_entries(fs: &dyn Fs, dir: &Path) -> Result> { + let mut entries = fs + .read_dir(dir) + .await + .with_context(|| format!("reading {dir:?}"))?; + let mut package_dirs = Vec::new(); + while let Some(entry) = entries.next().await { + let entry = entry.with_context(|| format!("reading entry in {dir:?}"))?; + // npm keeps metadata and in-progress extractions in dot-prefixed entries. + let is_hidden = entry + .file_name() + .is_none_or(|name| name.to_string_lossy().starts_with('.')); + if !is_hidden && fs.is_dir(&entry).await { + package_dirs.push(entry); + } + } + Ok(package_dirs) +} + /// People are using min-release-age more frequently. Which means a fresh registry will likely have /// new package versions than the user can install. /// We set the version to now be a ceiling and not an exact pin instead. This allows npm to resolve @@ -2128,6 +2218,49 @@ mod tests { assert_eq!(std::fs::read(command.path).unwrap(), contents); } + #[gpui::test] + async fn test_find_incomplete_npm_package(cx: &mut TestAppContext) { + let fs = fs::FakeFs::new(cx.executor()); + let node_modules_dir = Path::new("/agent/node_modules"); + fs.insert_tree( + node_modules_dir, + serde_json::json!({ + ".bin": {}, + ".package-lock.json": "{}", + "zod": { "package.json": "{}" }, + "@agentclientprotocol": { + "claude-agent-acp": { "package.json": "{}" }, + ".claude-agent-acp-abc123": {}, + }, + "@anthropic-ai": { + "claude-agent-sdk": { "package.json": "{}", "sdk.mjs": "" }, + }, + }), + ) + .await; + + assert_eq!( + find_incomplete_npm_package(fs.as_ref(), node_modules_dir) + .await + .unwrap(), + None + ); + + fs.remove_file( + &node_modules_dir.join("@anthropic-ai/claude-agent-sdk/package.json"), + Default::default(), + ) + .await + .unwrap(); + + assert_eq!( + find_incomplete_npm_package(fs.as_ref(), node_modules_dir) + .await + .unwrap(), + Some(node_modules_dir.join("@anthropic-ai/claude-agent-sdk")) + ); + } + #[gpui::test] async fn test_remove_stale_versioned_archive_cache_dirs(cx: &mut TestAppContext) { let fs = fs::FakeFs::new(cx.executor());