Skip to content

Commit 358fa6e

Browse files
committed
fix(ci): size guards also catch type changes, odd file names, unreadable blobs and bad budgets
1 parent 4aff7c5 commit 358fa6e

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎scripts/check-large-files.sh‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ elif [ "$#" -eq 0 ]; then
4545
fi
4646

4747
# Emit the paths to check, NUL-separated and unquoted (git quotes non-ASCII,
48-
# quotes and tabs otherwise). Added/Copied/Modified/Renamed only.
48+
# quotes and tabs otherwise). Added/Copied/Modified/Renamed/Type-changed only.
4949
list_files() {
5050
if [ -n "$RANGE_BASE" ]; then
51-
git diff -z --name-only --diff-filter=ACMR "$RANGE_BASE" "$RANGE_HEAD"
51+
git diff -z --name-only --diff-filter=ACMRT "$RANGE_BASE" "$RANGE_HEAD"
5252
elif [ -n "$BLOB_REV" ]; then
53-
git diff -z --cached --name-only --diff-filter=ACMR
53+
git diff -z --cached --name-only --diff-filter=ACMRT
5454
else
5555
printf '%s\0' "$@"
5656
fi
@@ -63,10 +63,17 @@ read_bytes() {
6363
violations="$(mktemp)"
6464
errors="$(mktemp)"
6565
paths="$(mktemp)"
66-
trap 'rm -f "$violations" "$errors" "$paths"' EXIT INT TERM
66+
blob="$(mktemp)"
67+
trap 'rm -f "$violations" "$errors" "$paths" "$blob"' EXIT INT TERM
6768

6869
# A git error (bad ref, not a repo) must fail the check, not pass an empty list.
6970
list_files "$@" > "$paths" || { echo "ERROR: could not list the files to check." >&2; exit 2; }
71+
# Paths are NUL-separated, so a newline byte can only come from a filename; sh cannot
72+
# keep such a name whole, so refuse it rather than check the wrong files.
73+
if [ "$(tr -cd '\n' < "$paths" | wc -c)" -gt 0 ]; then
74+
echo "ERROR: a file name contains a newline; rename it." >&2
75+
exit 2
76+
fi
7077

7178
tr '\0' '\n' < "$paths" | while IFS= read -r f; do
7279
[ -n "$f" ] || continue
@@ -100,9 +107,11 @@ tr '\0' '\n' < "$paths" | while IFS= read -r f; do
100107
# `grep -I` treats a file containing NUL bytes as binary, the same heuristic
101108
# git uses to print "Binary files differ". A generated blob of text is still
102109
# caught by review, not here.
103-
read_bytes "$f" 2>/dev/null | grep -qI . && continue
110+
# Read once into a file so a failed read is an error, never an empty pass.
111+
read_bytes "$f" > "$blob" || { printf '%s\n' "$f" >> "$errors"; continue; }
112+
grep -qI . "$blob" && continue
104113

105-
bytes="$(read_bytes "$f" | wc -c | tr -d ' ')"
114+
bytes="$(wc -c < "$blob" | tr -d ' ')"
106115
# Ceiling division: a sub-1024-byte file must report >=1 KB, never 0, so it
107116
# can't slip past a strict threshold (e.g. HF_MAX_NONLFS_KB=0). Plain
108117
# `bytes / 1024` would round a 512-byte binary down to 0 and pass it.

‎scripts/check-large-files.test.mjs‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert/strict";
22
import { spawnSync } from "node:child_process";
3-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { chmodSync, mkdtempSync, rmSync, symlinkSync, truncateSync, writeFileSync } from "node:fs";
44
import { tmpdir } from "node:os";
55
import { join } from "node:path";
66
import { describe, it } from "node:test";
@@ -30,7 +30,7 @@ function withRepo(run) {
3030
};
3131
try {
3232
git("init", "-q");
33-
run({ commit, range });
33+
run({ commit, range, dir, git });
3434
} finally {
3535
rmSync(dir, { recursive: true, force: true });
3636
}
@@ -137,4 +137,39 @@ describe("check-large-files", () => {
137137
assert.equal(range("no-such-ref", head).ok, false);
138138
});
139139
});
140+
141+
it("--range refuses a file name with a newline instead of checking the wrong files", () => {
142+
withRepo(({ commit, range }) => {
143+
const base = commit({ left: "l", right: "r" });
144+
const head = commit({ "left\nright": bigBinary });
145+
const { ok, stderr } = range(base, head);
146+
assert.equal(ok, false);
147+
assert.match(stderr, /newline/);
148+
});
149+
});
150+
151+
it("--range rejects a symlink replaced by a large binary", () => {
152+
withRepo(({ commit, range, dir }) => {
153+
writeFileSync(join(dir, "target"), "t");
154+
symlinkSync("target", join(dir, "clip.bin"));
155+
const base = commit({});
156+
rmSync(join(dir, "clip.bin"));
157+
const head = commit({ "clip.bin": bigBinary });
158+
const { ok, stderr } = range(base, head);
159+
assert.equal(ok, false);
160+
assert.match(stderr, /clip\.bin/);
161+
});
162+
});
163+
164+
it("--range fails when a blob cannot be read", () => {
165+
withRepo(({ commit, range, dir, git }) => {
166+
const base = commit({ "seed.txt": "x" });
167+
const head = commit({ "big.bin": bigBinary });
168+
const id = git("rev-parse", `${head}:big.bin`).stdout.trim();
169+
const object = join(dir, ".git", "objects", id.slice(0, 2), id.slice(2));
170+
chmodSync(object, 0o644);
171+
truncateSync(object, 64);
172+
assert.equal(range(base, head).ok, false);
173+
});
174+
});
140175
});

‎scripts/verify-packed-manifests.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ export function listPackageSizeIssues(name, sizes, budget) {
488488
];
489489
}
490490
return ["packed", "unpacked"].flatMap((kind) => {
491+
if (!Number.isFinite(budget[kind]) || budget[kind] <= 0) {
492+
return [`${name} has an invalid ${kind} budget in ${SIZE_BUDGETS_FILE}: use a byte count.`];
493+
}
491494
if (sizes[kind] > budget[kind]) {
492495
return [
493496
`${name} ${kind} size ${sizes[kind]} bytes is over its budget of ${budget[kind]}. Keep source maps, tests and unused assets out of the tarball; raise the budget in ${SIZE_BUDGETS_FILE} only with the reason in the PR.`,

‎scripts/verify-packed-manifests.test.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ describe("packed manifest verifier", () => {
3939
assert.match(issues[0], /lower it to 525/);
4040
});
4141

42+
it("fails a malformed budget instead of skipping the metric", () => {
43+
const sizes = { packed: 100, unpacked: 400 };
44+
for (const budget of [{}, { paked: 100, unpacked: 400 }, { packed: "NaN", unpacked: 400 }]) {
45+
assert.match(listPackageSizeIssues("pkg", sizes, budget).join("\n"), /invalid packed budget/);
46+
}
47+
});
48+
4249
it("asks for a budget for a new published package", () => {
4350
assert.match(
4451
listPackageSizeIssues("pkg", { packed: 100, unpacked: 400 }, undefined)[0],

0 commit comments

Comments
 (0)