Repository navigation
fix(parse): read process.argv when parse() is called, not when the module is imported - #179
Merged
Merged
Conversation
…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
1 of 3 tasks
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #158
Root cause
src/runtime.tscapturedprocess.argvonce, when the module was imported (runtimeProcessArgs = process.argv), andparse()used that value when called without arguments.Runners such as vite-node load cac for their own CLI first and only afterwards replace
process.argvfor the script they run. With a single shared cac instance (same version resolved for the runner and the app) the app'scli.parse()sees the array from import time, which still contains the script path. That path ends up inargsand 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.
cac@6.7.14+vite-node@2.1.9:npx vite-node index.ts helloprints{"args":["index.ts","hello"],...}and noHello World!(the report in The behavior of the "parse" function when used without arguments is strange. #158).cac@7.0.0+vite-node@6(which depends on cac 7): same result.Hello World!and{"args":[],...}.Change
getRuntimeProcessArgs()returnsprocess.argvat call time (orundefinedwhere there is noprocess), andparse()uses it. TheruntimeInfohandling is untouched.Test
tests/index.test.tshas a plain unit test that replacesprocess.argvafter cac was imported and checks thatparse()reads the new value. It does not depend on vite-node. It fails onmain(rawArgsis the stale array) and passes with the change; the full suite passes (21 tests) andtsc --noEmitis clean.