Skip to content

fix(parse): read process.argv when parse() is called, not when the module is imported - #179

Merged
sxzz merged 4 commits into
cacjs:mainfrom
Bryandero98:fix/158-read-argv-at-parse-time
Oct 11, 2026
Merged

sxzz merged 4 commits into
cacjs:mainfrom
Bryandero98:fix/158-read-argv-at-parse-time

Conversation

@Bryandero98

Copy link
Copy Markdown
Contributor

Fixes #158

Root cause

src/runtime.ts captured process.argv once, when the module was imported (runtimeProcessArgs = process.argv), and parse() used that value when called without arguments.

Runners such as vite-node load cac for their own CLI first and only afterwards replace process.argv for the script they run. With a single shared cac instance (same version resolved for the runner and the app) the app's cli.parse() sees the array from import time, which still contains the script path. That path ends up in args and the matched command never runs, which is the behaviour in the issue.

Reproduction

Not tied to a particular version of cac or vite-node: it needs the runner and the app to share one cac instance.

Change

getRuntimeProcessArgs() returns process.argv at call time (or undefined where there is no process), and parse() uses it. The runtimeInfo handling is untouched.

Test

tests/index.test.ts has a plain unit test that replaces process.argv after cac was imported and checks that parse() reads the new value. It does not depend on vite-node. It fails on main (rawArgs is the stale array) and passes with the change; the full suite passes (21 tests) and tsc --noEmit is clean.

…dule is imported

Runners such as vite-node load cac for their own CLI and only then rewrite
process.argv for the script. Because cac kept the array it saw at import time,
a later parse() with no arguments used the stale one and treated the script
file as a positional argument, so the matched command never ran.

Fixes cacjs#158
@sxzz
sxzz merged commit c0ccb94 into cacjs:main Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The behavior of the "parse" function when used without arguments is strange.

2 participants