Open History to see the last commit
1/**
2 * The single source of truth for packages the server CLI bundle must NOT inline.
3 *
4 * Two consumers derive from this list, and they must never disagree:
5 *
6 * - apps/server/vite.config.ts decides what stays external to the bundle.
7 * - scripts/build-desktop-artifact.ts selects the runtime dependency roots for
8 * the Windows server sidecar.
9 *
10 * A runtime package that is external but absent from the sidecar fails as soon
11 * as Node resolves it from the emitted bundle. Keeping both consumers on one
12 * list prevents packaging from drifting away from the bundle boundary.
13 *
14 * Entries are matched as prefixes (`id.startsWith(prefix)`), so they also cover
15 * a package's platform-specific siblings — `node-gyp-build` covers
16 * `node-gyp-build-optional-packages`, `@yuuang/` covers every `ffi-rs-*` binding.
17 */
18/**
19 * External because Node actually loads them from disk at runtime.
20 *
21 * Native addons (.node), the JS wrappers that dlopen them by real path, and —
22 * critically — the ordinary JS packages those wrappers require. An external
23 * package is loaded from the real filesystem, so its own `require` also
24 * resolves from the real filesystem; a dependency that was bundled away exists
25 * only inside the emitted bundle and is unreachable there. This closure is
26 * enforced by a test, not by inspection.
27 */
28export const CLI_RUNTIME_EXTERNAL_PREFIXES = [
29 // Cursor ships computed Webpack imports and platform helper packages.
30 "@cursor/sdk",
31 "node-pty",
32 "ffi-rs",
33 "@yuuang/",
34 "@ff-labs/",
35 "@napi-rs/keyring",
36 "@clerk/electron-passkeys",
37 "node-gyp-build",
38 "node-addon-api",
39 // ws's optional accelerators. Nothing in this repo declares them, so they are
40 // not in the staged production install and the packaged app does not ship
41 // them either way -- ws wraps the require in try/catch and falls back to its
42 // JS paths. They are listed because they were being inlined from the dev
43 // store: both carry binding.gyp and prebuilds and load through
44 // node-gyp-build, and a native loader inlined into a bundle chunk searches
45 // for prebuilds that cannot be beside it. Listing them keeps that from
46 // becoming real if either is ever declared as a dependency.
47 "bufferutil",
48 "utf-8-validate",
49] as const;
50
51// These are Cursor's disk-backed dependency closure. Match package boundaries
52// so "zod" does not also externalize unrelated packages such as zod-to-json-schema.
53const CURSOR_RUNTIME_DEPENDENCIES = [
54 "@bufbuild/protobuf",
55 "@connectrpc/connect",
56 "@connectrpc/connect-node",
57 "@connectrpc/connect-web",
58 "@statsig/js-client",
59 "@statsig/client-core",
60 "zod",
61 "undici",
62 "@fastify/busboy",
63] as const;
64
65export function isRuntimeExternalCliDependency(id: string): boolean {
66 return (
67 CLI_RUNTIME_EXTERNAL_PREFIXES.some((prefix) => id.startsWith(prefix)) ||
68 CURSOR_RUNTIME_DEPENDENCIES.some((name) => id === name || id.startsWith(`${name}/`))
69 );
70}
71
72/**
73 * True when `id` must stay out of the bundle.
74 *
75 * This has to be wired to the bundler's `neverBundle`, not just to
76 * `alwaysBundle`. `alwaysBundle` only forces packages IN — returning false from
77 * it means "no opinion", and the default then applies: a declared dependency
78 * stays external, but a transitive one gets bundled. That is how a native
79 * loader such as node-gyp-build ended up inlined while node-pty (a declared
80 * dependency) stayed external.
81 */
82export function isExternalCliDependency(id: string): boolean {
83 return isRuntimeExternalCliDependency(id);
84}
85
86/** True when the CLI bundle should inline `id` rather than leave it external. */
87export function shouldBundleCliDependency(id: string): boolean {
88 if (id.startsWith("node:")) return false;
89 return !isExternalCliDependency(id);
90}
91
92/** Select direct dependency roots whose runtime closure belongs in the sidecar. */
93export function selectCliRuntimeExternalDependencies(
94 dependencies: Readonly<Record<string, string>>,
95): Record<string, string> {
96 return Object.fromEntries(
97 Object.entries(dependencies).filter(([name]) => isRuntimeExternalCliDependency(name)),
98 );
99}
100
101/**
102 * Scan an emitted bundle chunk for runtime-external packages that were inlined.
103 *
104 * Configuring the bundler is not the same as checking what it produced. The
105 * `alwaysBundle` predicate only forces packages IN; returning false from it
106 * means "no opinion", so a transitive dependency still gets bundled by default.
107 * A native loader and its helper (node-gyp-build-optional-packages and
108 * detect-libc, when msgpackr-extract was still a dependency) were inlined that
109 * way while every list-based test passed, which is why this reads the artifact
110 * instead.
111 *
112 * `regionCount` is reported so the caller can tell "nothing was inlined" apart
113 * from "the marker format changed and this scan no longer sees anything".
114 *
115 * `inlinedPackages` is every package seen in a region, which lets the caller
116 * check the opposite direction too. Verifying only that externals are absent
117 * would still pass if the bundler reverted to leaving everything external: the
118 * scan would see source-file regions, report nothing inlined, and the packaged
119 * backends would then fail with ERR_MODULE_NOT_FOUND because those packages
120 * are not in the selected sidecar closure either.
121 */
122export function findInlinedExternalPackages(source: string): {
123 readonly regionCount: number;
124 readonly inlined: ReadonlyArray<string>;
125 readonly inlinedPackages: ReadonlyArray<string>;
126} {
127 // Rolldown marks each inlined module with a `//#region <path>` comment.
128 const regionPattern = /\/\/#region\s+(\S+)/g;
129 const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
130
131 let regionCount = 0;
132 const inlined = new Set<string>();
133 const inlinedPackages = new Set<string>();
134 for (const region of source.matchAll(regionPattern)) {
135 regionCount += 1;
136 const regionPath = region[1] ?? "";
137 for (const candidate of regionPath.matchAll(packagePattern)) {
138 const name = candidate[1];
139 if (name === undefined || name === ".pnpm") continue;
140 inlinedPackages.add(name);
141 if (isExternalCliDependency(name)) inlined.add(name);
142 }
143 }
144
145 return {
146 regionCount,
147 inlined: [...inlined].sort(),
148 inlinedPackages: [...inlinedPackages].sort(),
149 };
150}