Skip to content
21 changes: 20 additions & 1 deletion scripts/dev-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,29 @@ check_executable() {
check_quiet_command() {
local label="$1"
local remediation="$2"
local classification command_status matcher_status pipeline_result
local -a pipeline_status
shift 2

if "$@" >/dev/null 2>&1; then
# Drain the complete stream and retain one classification word. This bounds
# shell memory and keeps command output out of the doctor report.
pipeline_result="$(
"$@" 2>&1 | awk '
index($0, "JavaScript heap out of memory") { found = 1 }
END { printf "%s", found ? "heap" : "other" }
'
pipeline_status=("${PIPESTATUS[@]}")
printf '|%s|%s' "${pipeline_status[0]}" "${pipeline_status[1]}"
)"
IFS='|' read -r classification command_status matcher_status <<<"${pipeline_result}"

if [ "${command_status}" = "0" ] && [ "${matcher_status}" = "0" ]; then
pass "${label}"
elif [ "${classification}" = "heap" ] && [ "${matcher_status}" = "0" ]; then
# Node.js derives its default old-space limit from host memory, so this
# reports the host size rather than a fault in the checked sources.
fail "${label}: ran out of Node.js heap" \
"Raise the limit, then rerun: export NODE_OPTIONS=--max-old-space-size=5120"
else
fail "${label}: failed" "${remediation}"
fi
Expand Down
58 changes: 58 additions & 0 deletions test/dev-setup-doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,64 @@ describe("contributor environment doctor", () => {
expect(result.output).toContain("Next: Run: npm run build:cli");
});

it("names the Node.js heap limit when a type check exhausts it (#8688)", () => {
const fixture = createFixture();
writeExecutable(
path.join(fixture.repo, "node_modules", ".bin", "tsc"),
[
"#!/usr/bin/env bash",
"echo 'tsc-path-that-must-not-be-reported'",
"echo 'FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory' >&2",
"exit 134",
].join("\n"),
);

const result = runDoctor(fixture);

expect(result.status).toBe(1);
expect(result.output).toContain("CLI type check: ran out of Node.js heap");
expect(result.output).toContain("--max-old-space-size=5120");
expect(result.output).not.toContain("tsc-path-that-must-not-be-reported");
});

it("reports the heap remediation after large discarded type-check output (#8688)", () => {
const fixture = createFixture();
writeExecutable(
path.join(fixture.repo, "node_modules", ".bin", "tsc"),
[
"#!/usr/bin/env bash",
"echo 'FATAL ERROR: Allocation failed - JavaScript heap out of memory' >&2",
"i=0",
'while [ "$i" -lt 8192 ]; do',
" echo 'large-tsc-path-that-must-not-be-reported'",
" i=$((i + 1))",
"done",
"exit 134",
].join("\n"),
);

const result = runDoctor(fixture);

expect(result.status).toBe(1);
expect(result.output).toContain("CLI type check: ran out of Node.js heap");
expect(result.output).toContain("--max-old-space-size=5120");
expect(result.output).not.toContain("large-tsc-path-that-must-not-be-reported");
});

it("keeps the build remediation when a type check fails for another reason (#8688)", () => {
const fixture = createFixture();
writeExecutable(
path.join(fixture.repo, "node_modules", ".bin", "tsc"),
["#!/usr/bin/env bash", "echo 'error TS2304: Cannot find name.' >&2", "exit 2"].join("\n"),
);

const result = runDoctor(fixture);

expect(result.status).toBe(1);
expect(result.output).toContain("CLI type check: failed");
expect(result.output).toContain("Next: Run: npm run typecheck:cli");
});

it("redacts failed GitHub and Docker command output", () => {
const fixture = createFixture();

Expand Down
Loading