project: Fix npm-based registry agents failing to launch after a broken install (#66235)

A user reported Claude failing to launch after upgrading Zed:

```
Error: Cannot find package '.../external_agents/registry/npx/claude-acp/node_modules/@anthropic-ai/claude-agent-sdk/index.js' imported from .../@agentclientprotocol/claude-agent-acp/dist/acp-agent.js
  code: 'ERR_MODULE_NOT_FOUND'
```

Node only falls back to `index.js` when a package directory has no
usable `package.json`, so the install on disk was incomplete. The
published package is fine.

## Cause

`LocalRegistryNpxAgent::get_command` runs `npm install` in the shared
per-agent install directory on every call (per project and per
connection), with no coordination. Running several installs at once in
the same `node_modules`, or interrupting one, can leave a package
directory without its `package.json`.

npm doesn't repair this state on its own. I reproduced it locally: with
`claude-agent-sdk/package.json` removed and npm's hidden lockfile
(`node_modules/.package-lock.json`) still looking up to date, `npm
install` reported success, the package stayed broken, and loading the
agent failed with the user's exact error.

## Fix

- Serialize installs per install directory with a shared async lock, so
concurrent launches of the same agent don't run overlapping `npm
install`s.
- After installing, check that every top-level and scoped package
directory in `node_modules` has a `package.json`. If one doesn't, delete
`node_modules` and reinstall once. I checked this manually against the
broken install above, and the agent loaded afterwards.

## Known limitation

The lock only applies within one Zed process. If the launch is cancelled
while `npm install` is running, npm keeps going after the lock is
released, so a new launch could overlap with it. In both cases the
self-repair check fixes any resulting breakage on the next launch.

Release Notes:

- Fixed registry agents installed via npm (such as Claude Agent) failing
to launch with `ERR_MODULE_NOT_FOUND` after an interrupted or concurrent
install.
This commit is contained in:
Ben Brandt authored and GitHub committed 2026-10-28 15:31:48 +00:00
1 parent 2daea4d488
commit 8767f1530c
1 file changed
+144 -11
+144 -11
View File
@@ -1388,21 +1388,36 @@ impl ExternalAgentServer for LocalRegistryNpxAgent {
.join("registry")
.join("npx")
.join(sanitize_path_component(&registry_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<HashMap<PathBuf, Arc<async_lock::Mutex<()>>>>,
> = LazyLock::new(Default::default);
fn npx_install_lock(install_dir: &Path) -> Arc<async_lock::Mutex<()>> {
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<Option<PathBuf>> {
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<Vec<PathBuf>> {
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());