perf(cli): use `Module.runMain` instead of `child_process`
What's the problem this PR addresses?
Depends on https://github.com/yarnpkg/berry/pull/5595.
When yarnPath points to a different binary from the current one, Yarn forwards the command to the specified binary using child_process.execFileSync, which adds unnecessary overhead.
How did you fix it?
Made it use Module.runMain instead, the same trick we use in corepack starting from https://github.com/nodejs/corepack/pull/97.
This speeds up every command when using Yarn from sources (or in other cases where corepack uses modern, and corepack and yarnPath point to different binaries).
yarn -v in such a situation:
- Before:
0.263s - After:
0.229s - Improvement:
13%
Checklist
- [X] I have read the Contributing Guide.
- [X] I have set the packages that need to be released for my changes to be effective.
- [X] I will check that all automated PR checks pass before the PR gets reviewed.
I haven't tested it but this might fix https://github.com/yarnpkg/berry/issues/3996.
:thinking: Most likely, indeed.
@arcanis Can you please take a look at the v1 PR (https://github.com/yarnpkg/yarn/pull/8793) so we can fully close that issue?