OpenSwarm icon indicating copy to clipboard operation
OpenSwarm copied to clipboard

ESM bug: `require('node:path')` in `expandPath` crashes any `exec`/`run` with `--path <abs>`

Open shuklatushar226 opened this issue 7 months ago • 2 comments

Summary

@intrect/[email protected] crashes with ReferenceError: require is not defined whenever expandPath() (in dist/core/config.js) is invoked with resolveRelative=true. This breaks every openswarm exec / openswarm run invocation that passes --path <absolute-path>, because the CLI handler resolves the path via expandPath(path, true) before doing anything else.

Reproduction

npm install -g @intrect/openswarm
openswarm exec --local --pipeline --path /Users/me/some/repo "any prompt"

Stack trace

file:///usr/local/lib/node_modules/@intrect/openswarm/dist/core/config.js:289
        const { resolve } = require('node:path');
                            ^
ReferenceError: require is not defined
    at expandPath (file:///usr/local/lib/node_modules/@intrect/openswarm/dist/core/config.js:289:29)
    at executePrompt (file:///usr/local/lib/node_modules/@intrect/openswarm/dist/cli/promptHandler.js:132:25)
    at Command.<anonymous> (file:///usr/local/lib/node_modules/@intrect/openswarm/dist/cli.js:126:11)

Node.js v23.11.0

Root cause

The package is "type": "module" (ESM-only), and dist/core/config.js correctly imports join from node:path at the top. But inside expandPath, resolve is fetched lazily with CommonJS require, which is not defined in ESM modules:

import { join } from 'node:path';   // line 5 — ESM
// …
export function expandPath(path, resolveRelative = false) {
    if (path.startsWith('~/')) {
        return join(homedir(), path.slice(2));   // ← OK, never reaches the bug
    }
    if (resolveRelative) {
        const { resolve } = require('node:path'); // ← line 289, throws in ESM
        return resolve(path);
    }
    return path;
}

The ~/-prefixed branch dodges the bug because it returns before reaching the require call. As soon as a non-~/ path is passed with resolveRelative=true, the function throws.

Fix

Hoist resolve into the top-level node:path import and drop the lazy require:

-import { join } from 'node:path';
+import { join, resolve } from 'node:path';

 export function expandPath(path, resolveRelative = false) {
     if (path.startsWith('~/')) {
         return join(homedir(), path.slice(2));
     }
     if (resolveRelative) {
-        const { resolve } = require('node:path');
         return resolve(path);
     }
     return path;
 }

Verified locally: after this two-edit patch, openswarm exec --path <absolute> proceeds normally past path resolution. node --check passes; expandPath('/tmp/foo', true) returns /tmp/foo instead of throwing.

Workarounds (until fixed)

  1. Pass --path with a ~/-prefixed value (e.g. ~/Downloads/foo) — that branch of expandPath doesn't hit the bug.
  2. Manually patch the installed file as shown in the diff above. Note: any npm install -g @intrect/openswarm@<…> will overwrite the patched file.

Versions

  • @intrect/openswarm: 0.4.0
  • Node.js: v23.11.0
  • Platform: macOS Sequoia (Darwin 25.3.0, arm64)

Happy to send a PR with the one-line source fix if it'd help.

shuklatushar226 avatar May 03 '26 10:05 shuklatushar226

Thanks for the detailed root-cause writeup — repro, stack trace, and diff all spot-on. PR #53 applies your suggested fix.

While in there I found the exact same pattern in src/automation/runnerState.ts (const { mkdirSync } = require('node:fs')), which would have hit the same ReferenceError the first time the daily-pace dir didn't exist. Folded that into the same hotfix.

Will land in the next patch release.

unohee avatar May 07 '26 11:05 unohee

Released as 0.4.4 on npm: https://www.npmjs.com/package/@intrect/openswarm/v/0.4.4

npm install -g @intrect/[email protected]

Note: 0.4.3 was published to npm but its tarball was missing a protobufjs overrides entry that was supposed to mitigate CVE-2026-41242. 0.4.4 is a security re-publish that includes the override; everything else (including this fix) is identical to 0.4.3. Recommend upgrading directly to 0.4.4.

Thanks again for the clean root-cause writeup that made this a one-line fix.

unohee avatar May 07 '26 12:05 unohee