Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(plugins): discover npm installations for GUI imports

xjx035fdeea8eb0
diff --git a/apps/desktop/electron/main/npm-executable.ts b/apps/desktop/electron/main/npm-executable.ts
index 80b051e69..4c89ed5f9 100644
--- a/apps/desktop/electron/main/npm-executable.ts
+++ b/apps/desktop/electron/main/npm-executable.ts
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
import { accessSync, constants, mkdtempSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, dirname, extname, isAbsolute, join } from "node:path";
+import { mergePathParts, wellKnownUserBinDirs } from "./user-login-path";
/** Injectable so installer tests never run npm. */
export type DependencyCommandRunner = (
@@ -142,7 +143,7 @@ function requireFile(path: string, executable: boolean): void {
accessSync(path, executable && process.platform !== "win32" ? constants.X_OK : constants.R_OK);
}
-/** No shell probing: only the inherited PATH and the explicitly selected directory. */
+/** No shell probing: inherited PATH, known user bins, or an explicit selection. */
export async function prepareNpmExecutable(
npmPath?: string,
timeoutMs = NPM_VALIDATION_TIMEOUT_MS,
@@ -153,6 +154,11 @@ export async function prepareNpmExecutable(
try {
const tool: NpmExecutable = { command: npmPath ?? "npm", args: [], env: {} };
let node = process.platform === "win32" ? "node.exe" : "node";
+ if (npmPath === undefined && process.platform !== "win32") {
+ // GUI launches miss Homebrew/user bins. Keep inherited commands first and
+ // extend only this install's environment; never execute shell startup files.
+ tool.env.PATH = mergePathParts(process.env.PATH, wellKnownUserBinDirs().join(delimiter));
+ }
if (npmPath !== undefined) {
requireFile(npmPath, true); // stat follows symlinks; keep the selected directory.
const directory = dirname(npmPath);
diff --git a/apps/desktop/test/npm-executable.test.mjs b/apps/desktop/test/npm-executable.test.mjs
index f2c0b5fe8..08e554dc4 100644
--- a/apps/desktop/test/npm-executable.test.mjs
+++ b/apps/desktop/test/npm-executable.test.mjs
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import { register, syncBuiltinESMExports } from "node:module";
import childProcess from "node:child_process";
+import fs from "node:fs";
import { EventEmitter } from "node:events";
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
@@ -57,7 +58,13 @@ if (args[0] === 'install') fs.writeFileSync(path.join(process.cwd(), 'package-lo
`);
chmodSync(cli, 0o755);
writeFileSync(join(plugin, "package.json"), JSON.stringify({ dependencies: { example: "^1" } }));
- setEnv(t, { PATH: emptyPath });
+ // Keep executable discovery inside the fixture on every developer/CI host.
+ const realExists = fs.existsSync;
+ fs.existsSync = (path) => ["/opt/homebrew/bin", "/opt/homebrew/sbin", "/usr/local/bin"].includes(String(path))
+ ? false : realExists(path);
+ syncBuiltinESMExports();
+ t.after(() => { fs.existsSync = realExists; syncBuiltinESMExports(); });
+ setEnv(t, { PATH: emptyPath, HOME: join(root, "home") });
return { root, bin, plugin, emptyPath, log, node, npm, cli };
}
@@ -104,6 +111,24 @@ test("selected npm uses its directory PATH for both stages, without inheriting s
}
});
+test("GUI imports discover Node and npm in user bins without running a login shell", { skip: windows }, async (t) => {
+ const f = fixture(t);
+ const userBin = join(f.root, "home", ".local", "bin");
+ mkdirSync(userBin, { recursive: true });
+ symlinkSync(f.node, join(userBin, "node"));
+ symlinkSync(f.npm, join(userBin, "npm"));
+ const result = await installExtensionDependencies(f.plugin);
+ assert.deepEqual(result, { state: "installed" });
+ const recorded = calls(f);
+ assert.deepEqual(recorded.map((call) => call.args[0]), ["--version", "install", "ci"]);
+ for (const call of recorded) {
+ assert.equal(call.env.PATH, `${f.emptyPath}${delimiter}${userBin}`);
+ assert.equal(call.env.NODE_AUTH_TOKEN, undefined);
+ assert.equal(call.env.NODE_OPTIONS, undefined);
+ }
+ assert.equal(process.env.PATH, f.emptyPath, "discovery does not mutate the app environment");
+});
+
test("symlinked npm keeps the selected bin directory, not the real npm-cli directory", { skip: windows }, async (t) => {
const f = fixture(t);
const target = join(f.root, "npm-cli.js");
diff --git a/docs/adr/0244-imported-extension-dependency-boundary.md b/docs/adr/0244-imported-extension-dependency-boundary.md
index d11179135..bc79aae31 100644
--- a/docs/adr/0244-imported-extension-dependency-boundary.md
+++ b/docs/adr/0244-imported-extension-dependency-boundary.md
@@ -43,8 +43,11 @@ does not ship a standalone Node/npm executable.
`<dataDir>/npm-path.json` and revalidated on future imports. Stale choices
prompt again; save failure warns without preventing the current install.
Validation uses bounded npm/Node version checks and the install child alone
- receives the selected directory in `PATH`. No shell startup probing, global
- environment mutation, or credential inheritance is introduced. Recovery
+ receives the selected directory in `PATH`. Unconfigured macOS/Linux imports
+ append existing well-known user binary directories after inherited `PATH`
+ to recover GUI launches without changing configured executable precedence.
+ No shell startup probing, global environment mutation, or credential
+ inheritance is introduced. Recovery
retries the existing generated directory and registers it exactly once.
## Consequences
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 62e544ba0..5aadef689 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -16360,3 +16360,13 @@ renderer's durable transcript reads. No real model or provider is contacted.
- Installed Electron, real account/paid API and cross-version rollback are
separate release qualification. No MCP, Codemode or virtual-router migration
is included. See `docs/project/pi-0991-adoption.md` for candidate evidence.
+
+### Imported-extension GUI executable discovery regression (#1173)
+
+Start dependency installation with a GUI-like PATH that cannot resolve Node/npm
+and a trusted fixture installation in `~/.local/bin`. Validation and both npm
+stages must use that installation, preserve the application PATH, and avoid shell
+startup probing. Explicit selections keep their bin directory first. Missing
+installations still return `npm-unavailable` and retain native picker recovery.
+The executable integration fixture uses real isolated child processes and checks
+registry-only installation and credential isolation.
diff --git a/docs/spec/07-plugins/16-trusted-extensions.md b/docs/spec/07-plugins/16-trusted-extensions.md
index 2b1ab9b14..010776a56 100644
--- a/docs/spec/07-plugins/16-trusted-extensions.md
+++ b/docs/spec/07-plugins/16-trusted-extensions.md
@@ -144,9 +144,13 @@ reuse it after validation; a stale saved path returns to the recovery prompt.
If persistence fails, a native warning explains that the choice could not be
saved but the current import can still use the validated executable.
-The selected executable's directory is added only to the install child's `PATH`
-so npm can find `node`; no shell startup probing or global environment mutation
-is permitted. Version checks and installation retain a minimal environment with
+When no npm executable is configured on macOS/Linux, validation and installation
+append existing well-known user binary directories (including Homebrew and
+`~/.local/bin`) after the inherited `PATH`. This lets GUI imports find an existing
+Node.js/npm installation without executing shell startup files. A configured
+executable keeps its selected directory first and does not use these fallbacks.
+Only the install child's environment changes; the application `PATH` is unchanged.
+No shell startup probing or global environment mutation is permitted. Version checks and installation retain a minimal environment with
no inherited credentials. The registry-only proxy, isolated npm configuration,
bounded two-step install, disabled git resolution, and disabled lifecycle scripts
remain unchanged. Configured Windows `.cmd`/`.bat` launchers use the adjacent
diff --git a/scripts/e2e-plugin-import-deps.mjs b/scripts/e2e-plugin-import-deps.mjs
index f18659dca..d6b0054c5 100644
--- a/scripts/e2e-plugin-import-deps.mjs
+++ b/scripts/e2e-plugin-import-deps.mjs
@@ -14,7 +14,7 @@
* lifecycle scripts are allowed to run. Fixture B never accesses the network.
*/
import { createRequire, register } from "node:module";
-import { existsSync, mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { existsSync, mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, isAbsolute, join, resolve } from "node:path";
@@ -114,7 +114,24 @@ try {
});
let runner;
+ const originalPath = process.env.PATH;
+ const originalHome = process.env.HOME;
+ const guiPath = join(tempRoot, "gui-empty-bin");
try {
+ if (process.platform !== "win32") {
+ const npm = (originalPath ?? "").split(delimiter)
+ .filter(isAbsolute).map((directory) => join(directory, "npm"))
+ .find((path) => existsSync(path));
+ if (!npm) throw new Error("The E2E requires npm on the original PATH");
+ const home = join(tempRoot, "gui-home");
+ const bin = join(home, ".local", "bin");
+ mkdirSync(bin, { recursive: true });
+ mkdirSync(guiPath);
+ symlinkSync(process.execPath, join(bin, "node"));
+ symlinkSync(npm, join(bin, "npm"));
+ process.env.HOME = home;
+ process.env.PATH = guiPath;
+ }
const generated = generateImportedExtensionPlugin(sourceRoot, importRoot);
const copiedPackage = JSON.parse(readFileSync(join(generated.path, "package.json"), "utf8"));
const workspacesStripped = !Object.hasOwn(copiedPackage, "workspaces");
@@ -150,7 +167,8 @@ try {
registryOnly &&
nodeModulesCreated &&
lifecycleSuppressed &&
- loaded;
+ loaded &&
+ (process.platform === "win32" || process.env.PATH === guiPath);
record(
"E2E-PLUGIN-import-extension-installs-dependencies",
ok,
@@ -169,6 +187,10 @@ try {
);
} finally {
await runner?.dispose();
+ if (originalPath === undefined) delete process.env.PATH;
+ else process.env.PATH = originalPath;
+ if (originalHome === undefined) delete process.env.HOME;
+ else process.env.HOME = originalHome;
}
}
@@ -187,6 +209,9 @@ try {
const dataDir = join(tempRoot, "picker-data");
const emptyPath = join(tempRoot, "empty-bin");
mkdirSync(emptyPath);
+ // A stale configured path must still prompt instead of silently changing
+ // the user's chosen installation through automatic GUI lookup.
+ writeNpmPath(dataDir, join(emptyPath, name));
writeExtensionSource(sourceRoot, {
name: "picker-extension", version: "1.0.0",
pi: { extensions: ["index.mjs"] }, dependencies: { "is-number": "7.0.0" },