Skip to content

Commit 1bac125

Browse files
author
tsouth89
committed
test(build): check the bundle is self-contained, not just free of externals
The bundle scan only asserted that external packages were absent. A build that externalized everything would pass it: source-file regions still exist, so the region count is non-zero and no external is inlined. That is the exact failure this change exists to prevent, because those packages are not covered by the unpack globs either and the WSL backend dies on ERR_MODULE_NOT_FOUND. The scan now reports every package it saw in a region, and the build asserts "effect" is among them. Every server module imports it, so it is inlined in any correctly bundled build -- 209 regions in the current one -- and its absence means the dependencies went external again. Verified against the emitted bundle: 788 regions, no external violations, effect inlined, 25 third-party packages inlined in total. Reported by macroscope.
1 parent 1e5a52a commit 1bac125

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎scripts/build-desktop-artifact.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ const desktopBuildInputArtifactNames = {
379379
"bundled-server-client": "bundled server client",
380380
} satisfies Record<DesktopBuildInputArtifact, string>;
381381

382+
/**
383+
* Imported by every server module, so it is inlined in any correctly bundled
384+
* build. Its absence means the bundle went back to externalizing its
385+
* dependencies, which the unpack globs do not cover.
386+
*/
387+
const BUNDLE_SELF_CONTAINED_SENTINEL = "effect";
388+
389+
export class ExternalizedBundleError extends Schema.TaggedErrorClass<ExternalizedBundleError>()(
390+
"ExternalizedBundleError",
391+
{ sentinel: Schema.String, inlinedPackageCount: Schema.Number },
392+
) {
393+
override get message(): string {
394+
return `The server bundle did not inline "${this.sentinel}" (${this.inlinedPackageCount} packages inlined). The bundle is meant to be self-contained apart from the native externals; if its dependencies are external again they will not be unpacked, and the WSL backend will fail with ERR_MODULE_NOT_FOUND. Check the deps.alwaysBundle wiring in apps/server/vite.config.ts.`;
395+
}
396+
}
397+
382398
export class InlinedExternalPackageError extends Schema.TaggedErrorClass<InlinedExternalPackageError>()(
383399
"InlinedExternalPackageError",
384400
{ packages: Schema.Array(Schema.String) },
@@ -1851,11 +1867,13 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
18511867
);
18521868
let totalRegions = 0;
18531869
const inlined = new Set<string>();
1870+
const inlinedPackages = new Set<string>();
18541871
for (const chunkName of chunkNames) {
18551872
const source = yield* fs.readFileString(path.join(distDirs.serverDist, chunkName));
18561873
const scan = findInlinedExternalPackages(source);
18571874
totalRegions += scan.regionCount;
18581875
for (const name of scan.inlined) inlined.add(name);
1876+
for (const name of scan.inlinedPackages) inlinedPackages.add(name);
18591877
}
18601878
if (inlined.size > 0) {
18611879
return yield* new InlinedExternalPackageError({
@@ -1869,6 +1887,19 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
18691887
packages: ["<no module regions found; the bundle scan needs updating>"],
18701888
});
18711889
}
1890+
// The check above is one-directional: it only proves nothing external got
1891+
// inlined. A regression to externalizing everything would also pass it,
1892+
// since source-file regions still exist -- and that is the failure this
1893+
// whole change exists to prevent, because those packages are not in the
1894+
// unpack globs and the WSL backend would die on ERR_MODULE_NOT_FOUND.
1895+
// `effect` is imported by every server module, so it is inlined in any
1896+
// correctly bundled build (209 regions at the time of writing).
1897+
if (!inlinedPackages.has(BUNDLE_SELF_CONTAINED_SENTINEL)) {
1898+
return yield* new ExternalizedBundleError({
1899+
sentinel: BUNDLE_SELF_CONTAINED_SENTINEL,
1900+
inlinedPackageCount: inlinedPackages.size,
1901+
});
1902+
}
18721903
}
18731904

18741905
if (!(yield* fs.exists(bundledClientEntry))) {

‎scripts/lib/cli-external-packages.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,27 @@ var x = 1;
235235

236236
// regionCount is what separates "clean" from "this scan went blind because the
237237
// marker format changed". A caller that ignores it gets a vacuous pass.
238+
// The scan has to answer both directions. Checking only that externals are
239+
// absent still passes on a bundle that externalized everything, which is the
240+
// failure this whole change prevents.
241+
it("reports the packages that were inlined, not just the violations", () => {
242+
const source =
243+
region("../../node_modules/.pnpm/[email protected]/node_modules/effect/dist/index.js") +
244+
region("../../node_modules/.pnpm/[email protected]/node_modules/yaml/dist/index.js") +
245+
region("../../src/server/main.ts");
246+
const result = findInlinedExternalPackages(source);
247+
248+
assert.deepStrictEqual(result.inlinedPackages, ["effect", "yaml"]);
249+
assert.deepStrictEqual(result.inlined, []);
250+
});
251+
252+
it("does not report the pnpm store directory as a package", () => {
253+
const result = findInlinedExternalPackages(
254+
region("../../node_modules/.pnpm/[email protected]/node_modules/effect/dist/index.js"),
255+
);
256+
assert.deepStrictEqual(result.inlinedPackages, ["effect"]);
257+
});
258+
238259
it("reports no regions when the marker format is absent", () => {
239260
const result = findInlinedExternalPackages("var x = 1; // node_modules/detect-libc/lib.js");
240261
assert.strictEqual(result.regionCount, 0);

‎scripts/lib/cli-external-packages.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,40 @@ export const CLI_EXTERNAL_PACKAGE_UNPACK_GLOBS = CLI_EXTERNAL_PACKAGE_PREFIXES.f
107107
*
108108
* `regionCount` is reported so the caller can tell "nothing was inlined" apart
109109
* from "the marker format changed and this scan no longer sees anything".
110+
*
111+
* `inlinedPackages` is every package seen in a region, which lets the caller
112+
* check the opposite direction too. Verifying only that externals are absent
113+
* would still pass if the bundler reverted to leaving everything external: the
114+
* scan would see source-file regions, report nothing inlined, and the packaged
115+
* WSL backend would then fail with ERR_MODULE_NOT_FOUND because those packages
116+
* are not in the unpack globs either.
110117
*/
111118
export function findInlinedExternalPackages(source: string): {
112119
readonly regionCount: number;
113120
readonly inlined: ReadonlyArray<string>;
121+
readonly inlinedPackages: ReadonlyArray<string>;
114122
} {
115123
// Rolldown marks each inlined module with a `//#region <path>` comment.
116124
const regionPattern = /\/\/#region\s+(\S+)/g;
117125
const packagePattern = /node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g;
118126

119127
let regionCount = 0;
120128
const inlined = new Set<string>();
129+
const inlinedPackages = new Set<string>();
121130
for (const region of source.matchAll(regionPattern)) {
122131
regionCount += 1;
123132
const regionPath = region[1] ?? "";
124133
for (const candidate of regionPath.matchAll(packagePattern)) {
125134
const name = candidate[1];
126-
if (name !== undefined && isExternalCliDependency(name)) inlined.add(name);
135+
if (name === undefined || name === ".pnpm") continue;
136+
inlinedPackages.add(name);
137+
if (isExternalCliDependency(name)) inlined.add(name);
127138
}
128139
}
129140

130-
return { regionCount, inlined: [...inlined].sort() };
141+
return {
142+
regionCount,
143+
inlined: [...inlined].sort(),
144+
inlinedPackages: [...inlinedPackages].sort(),
145+
};
131146
}

0 commit comments

Comments
 (0)