fix(server): run the CLI on Node versions without import.meta.main #7141
main fromrefs/pull/7141/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubFixes #6465.
bin.ts and serviceLauncher.ts gate their entrypoints on import.meta.main. Node added that property in 22.18 and 24.2, but apps/server's engines range is ^22.16 || ^23.11 || >=24.10 — so a supported Node can load every module and exit 0 without running anything or printing a reason.
Measured here across real installs:
| Node | import.meta.main | today's guard |
|---|---|---|
| 22.17.0 | undefined | never runs, exit 0, no output |
| 23.11.0 | undefined | never runs, exit 0, no output |
| 22.18.0 | true | runs |
| 24.19.0 | true | runs |
^23.11 is affected too, not just the 22.16–22.17 window in the issue. Node 23 reached end-of-life before the backport, so every 23.11.x is in this state. That rules out narrowing the range as a fix — ^22.18 || ^23.11 || >=24.10 would still ship a broken branch — so this takes the other option in the issue and makes the guard version-agnostic.
The helper keeps import.meta.main whenever the runtime defines it, so on 22.18+/24.2+ nothing changes. It only falls back on versions where the property is missing, and those cannot start today, so the fallback has no working behavior to regress. The fallback also resolves through a symlink, which is how npm and npx install the CLI.
Why serviceLauncher.ts is in here. It has the identical guard and is the second shipped entrypoint. bootService.ts writes ExecStart=<node> <launcher> with Restart=always, and the unit test pins /usr/bin/node — the host runtime, not a pinned one. On an affected Node the launcher exits 0 silently and systemd restarts it forever. Same one-line guard, so it is here rather than in a follow-up, but happy to split it out if you would rather keep this to the reported path.
Verification, bundling the real source with esbuild and running it on each version: the current guard is silent on 22.17 and 23.11 for both entrypoints and the branch runs; output is identical on 22.18 and 24.19; the symlink shape behaves the same. entrypoint.test.ts covers the runtime-provided answer, the fallback, the symlink, the imported-not-entrypoint case that keeps bin.test.ts from launching the CLI, and a missing argv[1]. bin.test.ts 17/17 and bootService/serviceLauncher 15/15 still pass, tsgo --noEmit is clean.
One thing I did not touch: the issue also reports that on WSL a cold bin.mjs --help from app.asar.unpacked over 9p takes ~67s and blows the readiness window. That is a separate problem from this one and looks related to #5769.
Note
Medium Risk Touches the only shipped process entrypoints (CLI and systemd launcher); behavior is unchanged on Node 22.18+/24.2+ but the fallback path is new for affected engines.
Overview
On supported Node versions where import.meta.main is undefined (e.g. 22.16–22.17, 23.11), the CLI and service launcher no longer exit silently with code 0 because their if (import.meta.main) guards never ran.
This PR adds isEntrypoint, which still defers to import.meta.main when Node defines it, and otherwise treats the current module as the entrypoint by comparing import.meta.url to process.argv[1], including realpathSync so npm/npx symlink installs match. bin.ts and serviceLauncher.ts use that helper instead of a bare import.meta.main check; entrypoint.test.ts covers runtime main, path fallback, symlinks, non-entry imports, and missing argv[1].
Note
Fix CLI and service launcher entry guards for Node versions without import.meta.main#
Replaces import.meta.main checks in bin.ts and serviceLauncher.ts with a new isEntrypoint utility defined in entrypoint.ts.
isEntrypointreturnsruntimeMaindirectly when available, otherwise compares the entry path to the module URL, with arealpathfallback to handle symlinked executables.- Guards against missing or empty entry paths and catches filesystem errors from
realpath. - Behavioral Change: on Node versions where
import.meta.mainisundefined, the CLI and service launcher previously may not have run; they now correctly detect the entrypoint.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved ecd3c6d
This is a straightforward compatibility fix that adds a fallback for Node versions where
You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).import.meta.mainis undefined. The new helper function has clear logic and comprehensive test coverage, with no impact on runtime behavior when the native API is available.t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
8028711c-12d0-4349-b4d8-ce79f59bc20aYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share