#2555·OpenCLI

[Bug]: npm install in a plugin directory silently replaces the host @jackwener/opencli link with a registry copy

Author: wstczywCreated Sep 29, 2026Updated Sep 29, 2026

Description

opencli plugin install links <plugin>/node_modules/@jackwener/opencli to the running host (linkHostOpencli, src/plugin.ts:1405, called only from finalizePluginRuntime, src/plugin.ts:588). Plugins declare @jackwener/opencli as a peer dependency — opencli plugin create scaffolds "peerDependencies": { "@jackwener/opencli": ">=<version>" } (src/plugin-scaffold.ts:72-74) — and npm installs peers automatically. So any later npm install or npm ci in the plugin directory, for example while adding a dependency to a plugin installed from a local path, replaces the link with a copy from the registry (or with whatever the plugin's lockfile pinned).

From then on the plugin's import … from '@jackwener/opencli/…' loads that copy instead of the host, and nothing says so: opencli plugin list looks the same, opencli doctor has no plugin checks, and the link only comes back on opencli plugin install / update.

Two effects we ran into:

  • Wrong exit code. A CliError thrown by the plugin still gets its error envelope (the duck typing from #2388, src/errors.ts:260-277), but resolveExitCode (src/commanderAdapter.ts:154-157) uses instanceof CliError, so the process exits 1 instead of the error's exitCode.
  • Version skew. Two of our plugins had ended up with copies 1.8.2 and 1.8.6 while the host was 1.8.8, so their @jackwener/opencli imports resolved against undici 6.x instead of the host's 7.29.0 from #2326.

Steps to Reproduce

A minimal plugin in plain JS, so no transpile step is involved:

bash
mkdir peer-demo && cd peer-demo
cat > package.json <<'EOF'
{
  "name": "opencli-plugin-peer-demo",
  "version": "0.1.0",
  "type": "module",
  "peerDependencies": { "@jackwener/opencli": ">=1.8.8" }
}
EOF
cat > opencli-plugin.json <<'EOF'
{ "name": "peer-demo", "version": "0.1.0", "opencli": ">=1.8.8" }
EOF
cat > where.js <<'EOF'
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
  site: 'peer-demo', name: 'where', description: 'realpath of the @jackwener/opencli this plugin imports',
  strategy: Strategy.PUBLIC, browser: false, access: 'read', columns: ['registry'],
  func: async () => [{ registry: fs.realpathSync(fileURLToPath(import.meta.resolve('@jackwener/opencli/registry'))) }],
});
EOF
cat > fail.js <<'EOF'
import { cli, Strategy } from '@jackwener/opencli/registry';
import { CliError } from '@jackwener/opencli/errors';
cli({
  site: 'peer-demo', name: 'fail', description: 'throws CliError with exitCode 2',
  strategy: Strategy.PUBLIC, browser: false, access: 'read', columns: ['x'],
  func: async () => { throw new CliError('PEER_DEMO', 'intentional error', 'demo hint', 2); },
});
EOF

opencli plugin install "$PWD"
opencli peer-demo where -f json          # resolves inside the host package
opencli peer-demo fail; echo "exit=$?"   # exit=2

npm install                              # any install in the plugin dir
opencli peer-demo where -f json          # now resolves to ./node_modules/@jackwener/opencli, a copy
opencli peer-demo fail; echo "exit=$?"   # exit=1, although the printed error still says exitCode: 2
opencli plugin list                      # unchanged, no warning

What I observed, with the host installed by npm install --prefix <tmp>/host @jackwener/[email protected], run as node <tmp>/host/node_modules/@jackwener/opencli/dist/src/main.js, with HOME/USERPROFILE pointed at an empty temp dir and the npm steps run with --ignore-scripts:

bash
after `opencli plugin install <dir>`
  node_modules/@jackwener/opencli: isSymbolicLink=true  realpath=<tmp>\host\node_modules\@jackwener\opencli  version=1.8.8
  opencli peer-demo where -> <tmp>\host\node_modules\@jackwener\opencli\dist\src\registry-api.js
  opencli peer-demo fail  -> exit 2
npm install (in the plugin dir) -> changed 1 package in 3s
after `npm install` in the plugin dir
  node_modules/@jackwener/opencli: isSymbolicLink=false realpath=<tmp>\peer-demo\node_modules\@jackwener\opencli  version=1.8.8
  opencli peer-demo where -> <tmp>\peer-demo\node_modules\@jackwener\opencli\dist\src\registry-api.js
  opencli peer-demo fail  -> exit 1   (stderr still prints "exitCode: 2")
opencli plugin list -> peer-demo @0.1.0 (fail, where) ← local:<tmp>\peer-demo

Expected Behavior

The plugin keeps running against the host that loads it, or opencli says that it does not. A few possible directions, your call:

  1. At discovery, compare realpath(<pluginDir>/node_modules/@jackwener/opencli) with the host root and re-link or warn when they differ, the same check linkHostOpencli makes at install time.
  2. Report the mismatch in opencli plugin list or opencli doctor, pointing at opencli plugin update <name>.
  3. Document it for plugin authors. As a stopgap, our plugins now run a postinstall that re-links node_modules/@jackwener/opencli to $(npm root -g)/@jackwener/opencli.

OpenCLI Version

1.8.8 (npm latest). src/plugin.ts, src/discovery.ts and src/doctor.ts are unchanged on main (24136945) since v1.8.8.

Node.js Version

Other: v26.7.0 (npm 12.1.0)

Operating System

Windows 11