Compare commits

...

3 Commits

Author SHA1 Message Date
Diego Rodrigues de Sa e Souza
094a1aa1c0 fix(ci): stop the Codecov uploader from scanning the coverage shards (main) (#19332) 2026-11-08 22:33:19 -03:00
Diego Rodrigues de Sa e Souza
7eb1b69b51 fix(ci): give the Sonar JS/TS analyzer enough heap (#19280)
Cherry-pick of #19179 to main, so PRs based on main scan with the same heap as the release branch.
2026-11-07 17:13:02 -03:00
Diego Rodrigues de Sa e Souza
0461722b05 fix(electron): make the shared standalone bundle portable and fast to restore (#19283)
Port of #19239 (release/v3.8.54) to main, so the v3.8.53 macOS arm64
installer can be built through the shared standalone job. The per-leg
Next build on the arm64 runner hit the 6 h job limit on 2026-11-06.

Keeps traced symlinks verbatim in the colocated optionals, normalizes
Windows separators in the manifest and refuses relative escapes under
standalone/, and replaces the quadratic tarball block reader with a
bounded chunk queue. Scripts and tests only; the changelog fragment
stays on release/v3.8.54.
2026-11-07 16:41:01 -03:00
7 changed files with 507 additions and 78 deletions

View File

@@ -1326,6 +1326,11 @@ jobs:
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
with:
files: coverage/lcov.info
# `files` does not stop the uploader from also searching the workspace: on the
# v3.8.53 release push (run 42180292091) it found the 22207 per-shard JSON files
# under coverage-shards/, hit the 5-minute ceiling and never uploaded anything,
# while continue-on-error kept the step green. lcov.info is the only report.
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
- name: Upload coverage artifacts

View File

@@ -47,7 +47,15 @@
* fail-open, so this never throws into the install.
*/
import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync } from "node:fs";
import {
cpSync,
existsSync,
lstatSync,
mkdirSync,
readFileSync,
realpathSync,
rmSync,
} from "node:fs";
import { createRequire } from "node:module";
import { dirname, join, sep } from "node:path";
@@ -113,9 +121,7 @@ export function computeDependencyClosure(nodeModulesDir, seeds = SEED_PACKAGES)
function isPackageIntact(targetNodeModulesDir, name) {
if (!existsSync(join(targetNodeModulesDir, name))) return false;
try {
const probe = createRequire(
join(targetNodeModulesDir, "__colocate_probe__.js")
);
const probe = createRequire(join(targetNodeModulesDir, "__colocate_probe__.js"));
const resolved = probe.resolve(name);
// A resolution that walked past the target into an ancestor tree does not
// prove the target copy is usable.
@@ -127,6 +133,26 @@ function isPackageIntact(targetNodeModulesDir, name) {
}
}
/**
* `cpSync` filter for the no-clobber merge. With `verbatimSymlinks`, cpSync
* throws EEXIST when a link already sits at the destination, which would stop
* the merge halfway through a partially traced package. A link has no payload
* to preserve, so the traced one gives way to the source package's.
*
* @param {string} source
* @param {string} destination
* @returns {true}
*/
function replaceTracedSymlink(source, destination) {
if (
lstatSync(source).isSymbolicLink() &&
lstatSync(destination, { throwIfNoEntry: false })?.isSymbolicLink()
) {
rmSync(destination);
}
return true;
}
/**
* Co-locate the SLM optional dependency closure from `<rootDir>/node_modules`
* into a standalone bundle's `node_modules`.
@@ -174,10 +200,7 @@ export function colocateLlmlinguaOptionals({
// Check the complete closure rather than only the entry package, and judge
// presence by entrypoint integrity — a partially traced directory (see
// isPackageIntact) must still receive its missing files.
if (
closure.length > 0 &&
closure.every((name) => isPackageIntact(targetNm, name))
) {
if (closure.length > 0 && closure.every((name) => isPackageIntact(targetNm, name))) {
return { skipped: true, reason: "already co-located" };
}
@@ -192,10 +215,16 @@ export function colocateLlmlinguaOptionals({
// force:false merges into a partially traced directory: files the trace
// already materialized are kept, missing ones (the package payload) are
// filled in from the root tree.
// verbatimSymlinks: without it cpSync rewrites a package's relative links
// (global-agent's node_modules/.bin/semver) to absolute paths into this
// machine's root node_modules, which dangle in any shipped bundle and
// make the Electron web-build refuse to pack it.
cpSync(join(rootNm, name), dest, {
recursive: true,
force: false,
errorOnExist: false,
verbatimSymlinks: true,
filter: replaceTracedSymlink,
});
copied++;
} catch (err) {

View File

@@ -32,42 +32,56 @@ async function sha256File(filePath) {
}
/**
* Rewrite a symlink target so it is anchored to the tree being packed/verified
* instead of the machine that happened to create it (issue #11979).
* Reduce a symlink target to the portable form the manifest records, or refuse
* it when it leaves the tree (issue #11979).
*
* `npm`'s bin-links usually produce a target already relative to the
* symlink's own directory (e.g. `../semver/bin/semver.js`), which survives an
* archive/restore round trip unchanged on every OS. Some `npm ci` legs
* (observed on the ubuntu web-build runner) instead emit an ABSOLUTE target
* tied to that machine's checkout path. An absolute target is inherently
* non-portable: POSIX restores it as a dangling symlink once the packing
* machine's path is gone, and Windows' `CreateSymbolicLink` rewrites a
* leading `/` into a drive-relative path on read-back, so a byte-for-byte
* comparison against the recorded value fails outright.
* The portable form is relative to the symlink's own directory with `/`
* separators -- what `npm` writes for its bin links (e.g.
* `../semver/bin/semver.js`). It survives an archive/restore round trip on
* every OS, with one exception handled here: Node stores `\\` in a Windows
* symlink, so a restored link reads back with the other separator.
*
* A relative target has no such ambiguity, so an absolute target is resolved
* against `rootDir` and re-expressed relative to the symlink's own directory
* -- the same portable shape `npm install` already produces natively.
* An ABSOLUTE target is inherently non-portable: POSIX restores it as a
* dangling symlink once the packing machine's path is gone, and Windows'
* `CreateSymbolicLink` rewrites a leading `/` into a drive-relative path on
* read-back. One that points inside `rootDir` is re-expressed in the relative
* form. One that points elsewhere is refused: only the step that wrote the link
* knows where it should point, so the refusal never tries to find a same-named
* file in the tree.
*
* A relative target that climbs out of the tree is refused under `standalone/`,
* the part that ships and must be self-contained. Elsewhere it is legitimate:
* Turbopack's hashed externals in `<distDir>/node_modules` are relative links
* to the checkout's own `node_modules`, and they resolve on every desktop leg
* because each leg has the same checkout layout.
*
* @param {string} rootDir absolute path to the tree root
* @param {string} entryRelPath the symlink's own path, relative to rootDir (posix-separated)
* @param {string} rawTarget the raw string from fs.readlinkSync
* @param {string} [sep] the platform separator readlink reports (override for tests)
* @returns {{ok: true, value: string} | {ok: false, reason: string}}
*/
export function normalizeSymlinkTarget(rootDir, entryRelPath, rawTarget) {
if (!path.isAbsolute(rawTarget)) {
return { ok: true, value: rawTarget };
}
export function normalizeSymlinkTarget(rootDir, entryRelPath, rawTarget, sep = path.sep) {
const target = sep === "\\" ? rawTarget.replaceAll("\\", "/") : rawTarget;
const rootResolved = path.resolve(rootDir);
const resolvedTarget = path.resolve(rawTarget);
const symlinkDir = path.dirname(path.join(rootResolved, ...entryRelPath.split("/")));
const resolvedTarget = path.resolve(symlinkDir, target);
const relFromRoot = path.relative(rootResolved, resolvedTarget);
if (relFromRoot === "" || relFromRoot.startsWith("..") || path.isAbsolute(relFromRoot)) {
const leavesTree =
relFromRoot === "" ||
relFromRoot === ".." ||
relFromRoot.startsWith(`..${path.sep}`) ||
path.isAbsolute(relFromRoot);
const isAbsolute = path.isAbsolute(target);
if (leavesTree && (isAbsolute || entryRelPath.startsWith("standalone/"))) {
return {
ok: false,
reason: `symlink ${entryRelPath} target escapes the tree root: ${rawTarget}`,
};
}
const symlinkDir = path.dirname(path.join(rootResolved, ...entryRelPath.split("/")));
if (!isAbsolute) {
return { ok: true, value: target };
}
const relFromSymlink = path.relative(symlinkDir, resolvedTarget).split(path.sep).join("/");
return { ok: true, value: relFromSymlink };
}

View File

@@ -179,19 +179,32 @@ export async function createTarGz(srcDir, outFile) {
// ─── extraction ──────────────────────────────────────────────────────────────────
/** Decompressed bytes held ahead of the extractor before the stream is paused. */
export const READ_AHEAD_BYTES = 8 * 1024 * 1024;
/**
* Promise-based byte source over a gunzip stream. `read(n)` waits until `n`
* bytes are buffered (or EOF); `readSome()` returns whatever is available, for
* streaming large payloads into files without whole-file buffering.
* bytes are buffered (or EOF); `readSome(max)` returns up to `max` of whatever
* is available, for streaming large payloads into files without whole-file
* buffering.
*
* Chunks stay in a queue and reads hand out subarrays of them, so the cost per
* entry does not depend on how much is buffered. Gunzip outruns the extractor,
* which writes one file at a time; pausing it keeps the queue small on bundles
* of a hundred thousand entries.
*/
class BlockSource {
export class BlockSource {
constructor(stream) {
this.buffer = Buffer.alloc(0);
this.stream = stream;
this.chunks = [];
this.buffered = 0;
this.error = null;
this.ended = false;
this.waiter = null;
stream.on("data", (chunk) => {
this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
this.chunks.push(chunk);
this.buffered += chunk.length;
if (this.buffered >= READ_AHEAD_BYTES) stream.pause();
this.notify();
});
stream.on("end", () => {
@@ -212,15 +225,21 @@ class BlockSource {
}
}
readSome() {
take(max) {
const head = this.chunks[0];
const out = head.length > max ? head.subarray(0, max) : head;
if (out.length === head.length) this.chunks.shift();
else this.chunks[0] = head.subarray(max);
this.buffered -= out.length;
if (this.buffered < READ_AHEAD_BYTES && this.stream.isPaused()) this.stream.resume();
return out;
}
readSome(max = Infinity) {
return new Promise((resolve, reject) => {
const attempt = () => {
if (this.error) return reject(this.error);
if (this.buffer.length > 0) {
const out = this.buffer;
this.buffer = Buffer.alloc(0);
return resolve(out);
}
if (this.chunks.length > 0) return resolve(this.take(max));
if (this.ended) return resolve(null);
this.waiter = attempt;
};
@@ -228,28 +247,16 @@ class BlockSource {
});
}
unshift(buf) {
if (buf && buf.length > 0) this.buffer = Buffer.concat([buf, this.buffer]);
}
async read(n) {
let acc = null;
const parts = [];
let remaining = n;
while (remaining > 0) {
const chunk = await this.readSome();
const chunk = await this.readSome(remaining);
if (chunk === null) return null; // EOF before n bytes
if (chunk.length > remaining) {
acc = acc
? Buffer.concat([acc, chunk.subarray(0, remaining)])
: chunk.subarray(0, remaining);
this.unshift(chunk.subarray(remaining));
remaining = 0;
} else {
acc = acc ? Buffer.concat([acc, chunk]) : chunk;
remaining -= chunk.length;
}
parts.push(chunk);
remaining -= chunk.length;
}
return acc ?? Buffer.alloc(0);
return parts.length === 1 ? parts[0] : Buffer.concat(parts, n);
}
}
@@ -277,14 +284,12 @@ function checksumMatches(header) {
async function copyN(reader, size, outStream) {
let remaining = size;
while (remaining > 0) {
const chunk = await reader.readSome();
const chunk = await reader.readSome(remaining);
if (chunk === null) {
throw new Error(`unexpected EOF after ${size - remaining} of ${size} bytes`);
}
const take = chunk.length > remaining ? chunk.subarray(0, remaining) : chunk;
if (chunk.length > remaining) reader.unshift(chunk.subarray(remaining));
remaining -= take.length;
if (!outStream.write(take)) await once(outStream, "drain");
remaining -= chunk.length;
if (!outStream.write(chunk)) await once(outStream, "drain");
}
}
@@ -298,7 +303,16 @@ export async function extractTarGz(archiveFile, destDir) {
const gunzip = createGunzip();
src.pipe(gunzip);
const reader = new BlockSource(gunzip);
try {
return await extractEntries(reader, gunzip, destDir);
} finally {
// A paused gunzip never reaches `end`, so it has to be closed explicitly.
src.destroy();
gunzip.destroy();
}
}
async function extractEntries(reader, gunzip, destDir) {
const zeros = Buffer.alloc(BLOCK);
let longName = null;
let longLink = null;
@@ -381,7 +395,6 @@ export async function extractTarGz(archiveFile, destDir) {
entries += 1;
}
src.destroy();
return { entries };
}

View File

@@ -7,6 +7,12 @@ sonar.test.inclusions=tests/**/*.test.ts,tests/**/*.spec.ts
sonar.exclusions=tests/**,src/i18n/messages/**,docs/**,coverage/**,.next/**,dist/**,build/**,node_modules/**
sonar.javascript.lcov.reportPaths=coverage/lcov.info
# The JS/TS analyzer runs in its own Node.js process. On the hosted runner (16 GB) its default
# heap is 4288 MB, which this codebase outgrew: the first scan that really executed after the
# move to the cheaper-inference org (2026-11-05, CI run 42015741745) stopped with "the Node.js
# process running out of memory (heap size limit 4288 MB)". Value is in MB.
sonar.javascript.node.maxspace=10240
# ── "Clean as You Code" — new-code quality gate ───────────────────────────
# New code must not introduce any bug, vulnerability, hotspot or code smell.
# Legacy code is grandfathered (sonar.newCode.referenceBranch baseline).

View File

@@ -4,6 +4,7 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { createHash } from "node:crypto";
import { PassThrough } from "node:stream";
/**
* Stage 8 (issue #10321) — shared standalone web bundle.
@@ -20,6 +21,7 @@ import { createHash } from "node:crypto";
const bundleMod = await import("../../../scripts/build/standaloneBundle.mjs");
const manifestMod = await import("../../../scripts/build/standaloneManifest.mjs");
const hydrateMod = await import("../../../scripts/build/hydrateNativeDeps.mjs");
const tarballMod = await import("../../../scripts/build/standaloneTarball.mjs");
const { runPack, runRestore } = bundleMod as typeof bundleMod & {
runPack: (opts: { dir?: string; out: string; manifest?: string }) => Promise<{
@@ -53,6 +55,18 @@ const { hydratePlatformNatives, verifyBundledNatives } = hydrateMod as typeof hy
};
};
const { BlockSource, READ_AHEAD_BYTES, createTarGz, extractTarGz } =
tarballMod as typeof tarballMod & {
READ_AHEAD_BYTES: number;
BlockSource: new (stream: PassThrough) => {
buffered: number;
read: (n: number) => Promise<Buffer | null>;
readSome: (max?: number) => Promise<Buffer | null>;
};
createTarGz: (srcDir: string, outFile: string) => Promise<void>;
extractTarGz: (archive: string, destDir: string) => Promise<{ entries: number }>;
};
const IS_WINDOWS = process.platform === "win32";
function tmpDir(prefix: string): string {
@@ -166,6 +180,79 @@ test("restore rejects a corrupted archive before extraction", async () => {
}
});
test("extraction does not recopy the buffered stream for every entry", async () => {
const src = tmpDir("s8-copies-src-");
const outDir = tmpDir("s8-copies-out-");
const dst = tmpDir("s8-copies-dst-");
const realConcat = Buffer.concat;
try {
// Restore time on a real bundle (123k entries) went quadratic through
// Buffer.concat. Counting the copied bytes proves it without a timing assertion.
const entries = 2000;
const body = Buffer.alloc(1500, "x");
for (let i = 0; i < entries; i++) {
fs.writeFileSync(path.join(src, `f${i}.txt`), body);
}
const archive = path.join(outDir, "web-bundle.tar.gz");
await createTarGz(src, archive);
const tarBytes = entries * (512 + 2048);
let copied = 0;
Buffer.concat = ((list: Uint8Array[], length?: number) => {
const joined = realConcat(list, length);
copied += joined.length;
return joined;
}) as typeof Buffer.concat;
const result = await extractTarGz(archive, dst);
Buffer.concat = realConcat;
assert.equal(result.entries, entries);
assert.equal(fs.readFileSync(path.join(dst, "f1999.txt")).length, body.length);
assert.ok(copied <= tarBytes, `copied ${copied} bytes extracting a ${tarBytes}-byte tar`);
} finally {
Buffer.concat = realConcat;
fs.rmSync(src, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(outDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(dst, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("BlockSource pauses the stream ahead of a slow reader and loses no bytes", async () => {
const stream = new PassThrough({ highWaterMark: 64 * 1024 });
const source = new BlockSource(stream);
const chunk = Buffer.alloc(256 * 1024, 7);
const total = (READ_AHEAD_BYTES / chunk.length) * 4 * chunk.length;
let written = 0;
const writer = (async () => {
while (written < total) {
written += chunk.length;
if (!stream.write(chunk)) await new Promise((resolve) => stream.once("drain", resolve));
}
stream.end();
})();
await new Promise((resolve) => setTimeout(resolve, 50));
assert.ok(stream.isPaused(), "a reader that is behind must pause the stream");
assert.ok(
source.buffered <= READ_AHEAD_BYTES + chunk.length,
`read-ahead ${source.buffered} exceeds the limit`
);
assert.ok(written < total, "the writer must feel the backpressure");
// Reads cross chunk boundaries and must see every byte exactly once.
let received = 0;
const odd = 1000;
for (;;) {
const part = await source.read(odd);
if (part === null) break;
assert.equal(part.length, odd);
received += part.length;
}
await writer;
assert.equal(received, Math.floor(total / odd) * odd);
assert.equal(await source.readSome(), null, "the stream is exhausted");
});
test("manifest verification flags modified and smuggled files in a restored tree", async () => {
const src = tmpDir("s8-verify-");
const outDir = tmpDir("s8-verify-out-");

View File

@@ -9,19 +9,31 @@ import path from "node:path";
* manifest verification on Windows because symlink targets were stored as
* absolute, packing-machine-specific paths).
*
* npm ci on the ubuntu web-build leg is observed (run 33238093090) to
* produce at least one ABSOLUTE .bin symlink target (an npm/bin-links
* implementation detail) instead of the RELATIVE target a local `npm
* install` produces for the identical file
* (node_modules/global-agent/node_modules/.bin/semver -> ../semver/bin/semver.js).
* An absolute target is not portable: it dangles once the packing machine's
* path is gone (silent false-positive on POSIX) and Windows'
* CreateSymbolicLink rewrites it to a drive-relative path on read-back
* (outright verification failure) -- both symptoms share the same root
* cause of never normalizing to a relative, tree-anchored form.
* The one absolute link in a real bundle is
* standalone/node_modules/global-agent/node_modules/.bin/semver. npm writes it
* relative; colocateLlmlinguaOptionals' cpSync rewrote it to the build
* checkout's root node_modules, which is OUTSIDE the packed `.build/next` tree
* (run 33238093090 on the hosted runner, run 42206099467 on omni-build). Such a
* target is not portable: it dangles once the packing machine's path is gone
* and Windows' CreateSymbolicLink rewrites it to a drive-relative path on
* read-back. The colocation cases below reproduce that shape; the #11979 cases
* cover an absolute target that does sit inside the tree.
*/
const bundleMod = await import("../../../scripts/build/standaloneBundle.mjs");
const colocateMod = await import("../../../scripts/build/colocateOptionals.mjs");
const manifestMod = await import("../../../scripts/build/standaloneManifest.mjs");
const { runPack, runRestore } = bundleMod as typeof bundleMod & {
runPack: (opts: { dir?: string; out: string }) => Promise<{ files: number }>;
runRestore: (opts: { archive: string; dir?: string }) => Promise<{ files: number }>;
};
const { colocateLlmlinguaOptionals } = colocateMod as typeof colocateMod & {
colocateLlmlinguaOptionals: (opts: {
rootDir: string;
targetNodeModulesDir: string;
seeds: string[];
}) => { skipped: boolean; copied?: number };
};
const { buildStandaloneManifest, verifyStandaloneManifest, normalizeSymlinkTarget } =
manifestMod as typeof manifestMod & {
buildStandaloneManifest: (
@@ -34,7 +46,8 @@ const { buildStandaloneManifest, verifyStandaloneManifest, normalizeSymlinkTarge
normalizeSymlinkTarget: (
rootDir: string,
entryRelPath: string,
rawTarget: string
rawTarget: string,
sep?: string
) => { ok: true; value: string } | { ok: false; reason: string };
};
@@ -107,10 +120,7 @@ test("#11979: a relativized manifest restores to a working (non-dangling) symlin
"bin"
);
fs.mkdirSync(semverDir2, { recursive: true });
fs.writeFileSync(
path.join(semverDir2, "semver.js"),
"#!/usr/bin/env node\n// fake semver cli\n"
);
fs.writeFileSync(path.join(semverDir2, "semver.js"), "#!/usr/bin/env node\n// fake semver cli\n");
const binDir = path.join(restoredRoot, "node_modules", "global-agent", "node_modules", ".bin");
fs.mkdirSync(binDir, { recursive: true });
fs.symlinkSync(entry.symlink!, path.join(binDir, "semver"));
@@ -173,3 +183,268 @@ test("#11979: normalizeSymlinkTarget rejects an absolute target that escapes the
assert.equal(result.ok, false);
fs.rmSync(rootDir, { recursive: true, force: true });
});
const BIN_LINK = "node_modules/global-agent/node_modules/.bin/semver";
/**
* A build checkout laid out like the real one: global-agent installed by npm at
* the root with its relative .bin link, and a `.build/next` tree that Next did
* not trace global-agent into.
*/
function makeBuildCheckout(checkout: string): { packDir: string; standaloneNm: string } {
const pkg = path.join(checkout, "node_modules", "global-agent");
const nestedBin = path.join(pkg, "node_modules", ".bin");
const semverBin = path.join(pkg, "node_modules", "semver", "bin");
fs.mkdirSync(nestedBin, { recursive: true });
fs.mkdirSync(semverBin, { recursive: true });
fs.writeFileSync(
path.join(pkg, "package.json"),
JSON.stringify({ name: "global-agent", version: "4.1.3", main: "index.js" })
);
fs.writeFileSync(path.join(pkg, "index.js"), "module.exports = {};\n");
fs.writeFileSync(path.join(semverBin, "semver.js"), "// semver cli\n");
fs.symlinkSync("../semver/bin/semver.js", path.join(nestedBin, "semver"));
const packDir = path.join(checkout, ".build", "next");
const standaloneNm = path.join(packDir, "standalone", "node_modules");
fs.mkdirSync(standaloneNm, { recursive: true });
fs.writeFileSync(path.join(packDir, "standalone", "server.js"), "// server\n");
return { packDir, standaloneNm };
}
function colocateGlobalAgent(checkout: string, standaloneNm: string): void {
const result = colocateLlmlinguaOptionals({
rootDir: checkout,
targetNodeModulesDir: standaloneNm,
seeds: ["global-agent"],
});
assert.equal(result.copied, 1, "global-agent must be co-located");
}
test("colocation keeps a package's nested .bin link relative", () => {
const checkout = tmpDir("standalone-checkout-");
try {
const { standaloneNm } = makeBuildCheckout(checkout);
colocateGlobalAgent(checkout, standaloneNm);
assert.equal(
fs.readlinkSync(path.join(standaloneNm, "global-agent/node_modules/.bin/semver")),
"../semver/bin/semver.js",
"the copy must not point back at the build checkout's root node_modules"
);
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
}
});
test("colocation fills a partially traced package without breaking its .bin link", () => {
const checkout = tmpDir("standalone-checkout-");
try {
const { standaloneNm } = makeBuildCheckout(checkout);
// The trace left the link and the manifest but not the entrypoint, so the
// package is not intact and the copy merges over the existing link.
const traced = path.join(standaloneNm, "global-agent");
fs.mkdirSync(path.join(traced, "node_modules", ".bin"), { recursive: true });
fs.copyFileSync(
path.join(checkout, "node_modules", "global-agent", "package.json"),
path.join(traced, "package.json")
);
fs.symlinkSync("../semver/bin/semver.js", path.join(traced, "node_modules", ".bin", "semver"));
colocateGlobalAgent(checkout, standaloneNm);
assert.ok(fs.existsSync(path.join(traced, "index.js")), "missing entrypoint filled in");
assert.equal(
fs.readlinkSync(path.join(traced, "node_modules", ".bin", "semver")),
"../semver/bin/semver.js"
);
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
}
});
test("a colocated tree packs here and restores where the build checkout is gone", async () => {
const checkout = tmpDir("standalone-checkout-");
const outDir = tmpDir("standalone-out-");
const restored = path.join(outDir, "restored");
try {
const { packDir, standaloneNm } = makeBuildCheckout(checkout);
colocateGlobalAgent(checkout, standaloneNm);
const archive = path.join(outDir, "web-bundle.tar.gz");
await runPack({ dir: packDir, out: archive });
// Only the archive and its manifest travel to the desktop legs.
fs.rmSync(checkout, { recursive: true, force: true });
await runRestore({ archive, dir: restored });
const restoredLink = path.join(restored, "standalone", BIN_LINK);
assert.equal(fs.readlinkSync(restoredLink), "../semver/bin/semver.js");
assert.equal(fs.readFileSync(restoredLink, "utf8"), "// semver cli\n");
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});
test("pack refuses an absolute link into the build checkout even when the tree has that path", async () => {
const checkout = tmpDir("standalone-checkout-");
const outDir = tmpDir("standalone-out-");
try {
const { packDir, standaloneNm } = makeBuildCheckout(checkout);
colocateGlobalAgent(checkout, standaloneNm);
// The exact v3.8.53 shape: the same file exists inside the tree, but the
// link itself leaves it. Guessing the in-tree twin would hide a broken build.
const link = path.join(standaloneNm, "global-agent/node_modules/.bin/semver");
fs.rmSync(link);
fs.symlinkSync(
path.join(checkout, "node_modules/global-agent/node_modules/semver/bin/semver.js"),
link
);
await assert.rejects(
() => runPack({ dir: packDir, out: path.join(outDir, "web-bundle.tar.gz") }),
/target escapes the tree root/
);
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});
for (const [label, target] of [
["absolute", "/opt/actions-runner/_work/secret.txt"],
["relative", "../../../../secret.txt"],
] as const) {
test(`pack refuses a ${label} link that escapes the tree`, async () => {
const root = tmpDir("standalone-escape-");
const outDir = tmpDir("standalone-out-");
try {
const binDir = path.join(root, "standalone", "node_modules", ".bin");
fs.mkdirSync(binDir, { recursive: true });
fs.symlinkSync(target, path.join(binDir, "escape"));
await assert.rejects(
() => runPack({ dir: root, out: path.join(outDir, "web-bundle.tar.gz") }),
/target escapes the tree root/
);
} finally {
fs.rmSync(root, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});
}
test("pack keeps Turbopack's relative externals, which resolve in every checkout", async () => {
const checkout = tmpDir("standalone-checkout-");
const outDir = tmpDir("standalone-out-");
const restoredCheckout = tmpDir("standalone-leg-");
try {
const { packDir } = makeBuildCheckout(checkout);
// The real shape: 15 of these sit beside standalone/ in a Turbopack build.
const links = [
["ws-a972e7ffa40ff725", "../../../node_modules/ws"],
["@ngrok/ngrok-0f98e1294a0b09d5", "../../../../node_modules/@ngrok/ngrok"],
];
for (const [name, target] of links) {
const link = path.join(packDir, "node_modules", name);
fs.mkdirSync(path.dirname(link), { recursive: true });
fs.symlinkSync(target, link);
}
const archive = path.join(outDir, "web-bundle.tar.gz");
await runPack({ dir: packDir, out: archive });
const restored = path.join(restoredCheckout, ".build", "next");
await runRestore({ archive, dir: restored });
for (const [name, target] of links) {
assert.equal(fs.readlinkSync(path.join(restored, "node_modules", name)), target);
}
// The leg's own npm ci provides the package the link lands on.
fs.mkdirSync(path.join(restoredCheckout, "node_modules", "ws"), { recursive: true });
fs.writeFileSync(path.join(restoredCheckout, "node_modules", "ws", "index.js"), "// ws\n");
assert.equal(
fs.readFileSync(path.join(restored, "node_modules", links[0][0], "index.js"), "utf8"),
"// ws\n"
);
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
fs.rmSync(restoredCheckout, { recursive: true, force: true });
}
});
test("under standalone/, normalizeSymlinkTarget refuses relative targets that leave the tree", () => {
const entry = "standalone/node_modules/.bin/tool";
const rootDir = tmpDir("standalone-root-");
try {
assert.deepEqual(normalizeSymlinkTarget(rootDir, entry, "../tool/cli.js"), {
ok: true,
value: "../tool/cli.js",
});
assert.deepEqual(normalizeSymlinkTarget(rootDir, entry, "../../../static/app.css"), {
ok: true,
value: "../../../static/app.css",
});
for (const target of ["../../../..", "../../../../outside.js", "../../..", "../../../"]) {
const result = normalizeSymlinkTarget(rootDir, entry, target);
assert.equal(result.ok, false, `${target} must be refused`);
}
} finally {
fs.rmSync(rootDir, { recursive: true, force: true });
}
});
test("Windows read-back of a relative link compares equal to the manifest's posix target", () => {
const entry = "standalone/node_modules/global-agent/node_modules/.bin/semver";
const rootDir = tmpDir("standalone-root-");
try {
// fs.symlinkSync stores `\\` on Windows, so readlink never returns the `/`
// the Linux pack recorded.
assert.deepEqual(normalizeSymlinkTarget(rootDir, entry, "..\\semver\\bin\\semver.js", "\\"), {
ok: true,
value: "../semver/bin/semver.js",
});
const escaping = "..\\..\\..\\..\\..\\..\\outside.js";
assert.equal(normalizeSymlinkTarget(rootDir, entry, escaping, "\\").ok, false);
// A backslash is an ordinary filename character on POSIX.
assert.deepEqual(normalizeSymlinkTarget(rootDir, entry, "..\\semver"), {
ok: true,
value: "..\\semver",
});
} finally {
fs.rmSync(rootDir, { recursive: true, force: true });
}
});
test("verification still fails a restored link that was retargeted", async () => {
const checkout = tmpDir("standalone-checkout-");
const outDir = tmpDir("standalone-out-");
const restored = path.join(outDir, "restored");
try {
const { packDir, standaloneNm } = makeBuildCheckout(checkout);
colocateGlobalAgent(checkout, standaloneNm);
const archive = path.join(outDir, "web-bundle.tar.gz");
await runPack({ dir: packDir, out: archive });
await runRestore({ archive, dir: restored });
const manifest = JSON.parse(fs.readFileSync(`${archive}.manifest.json`, "utf8"));
const link = path.join(restored, "standalone", BIN_LINK);
fs.rmSync(link);
fs.symlinkSync("../semver/package.json", link);
const inTree = await verifyStandaloneManifest(restored, manifest);
assert.equal(inTree.ok, false);
assert.match(
(inTree as { errors: string[] }).errors.join("; "),
/symlink target \.\.\/semver\/package\.json != \.\.\/semver\/bin\/semver\.js/
);
fs.rmSync(link);
fs.symlinkSync("../../../../../../../outside.js", link);
await assert.rejects(
() => verifyStandaloneManifest(restored, manifest),
/target escapes the tree root/
);
} finally {
fs.rmSync(checkout, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});