Skip to content

Commit b2377db

Browse files
authored
ci: unused dependency patches fail in check:changed, not after push (pingdotgg#23988)
1 parent 24c063a commit b2377db

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

‎scripts/check-changed.ts‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Runs CI's fast checks for this branch's changes: format and lint of the
33
// changed files, knip, generated GraphQL operations, typecheck of the affected
44
// packages and their dependents, and the test files beside the changed files.
5-
// CI still runs the full test suites and the jobs listed in the summary.
5+
// A dependency change also gets the unused-patch install check below. CI still
6+
// runs the full test suites and the jobs listed in the summary.
67
//
78
// vp run check:changed compare against the merge base with origin/main
89
// vp run check:changed <ref> compare against the merge base with <ref>
@@ -11,6 +12,7 @@
1112
// .github/scripts/ci-changes.cjs, the same mapping CI uses to gate its jobs.
1213
import * as NodeChildProcess from "node:child_process";
1314
import * as NodeFS from "node:fs";
15+
import * as NodeOS from "node:os";
1416
import * as NodePath from "node:path";
1517
import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
1618

@@ -71,13 +73,81 @@ const runKnip = files.some(
7173
(file) => CODE.test(file) || NodePath.basename(file) === "package.json" || file === "knip.jsonc",
7274
);
7375

76+
// A version bump can strand a patch in pnpm-workspace.yaml: no resolved
77+
// version uses it and pnpm rejects the install with ERR_PNPM_UNUSED_PATCH.
78+
// CI catches that before merge through Release Smoke, because ci-changes.cjs
79+
// runs everything for any package.json and for root paths it does not map to
80+
// a package (pnpm-workspace.yaml, pnpm-lock.yaml, patches/). The local run
81+
// had no such check, so a stranded patch passed here and failed after push.
82+
const runPatchCheck = files.some(
83+
(file) =>
84+
file === "pnpm-workspace.yaml" ||
85+
file === "pnpm-lock.yaml" ||
86+
file === ".pnpmfile.cjs" ||
87+
file.startsWith("patches/") ||
88+
file.startsWith("apps/mobile/deps/") ||
89+
NodePath.basename(file) === "package.json",
90+
);
91+
92+
// Mirror Release Smoke's temp-copy install: copy the workspace manifests to
93+
// a temp dir, omit the lockfile, and install with --lockfile-only, so the
94+
// working tree and its lockfile stay untouched (the mobile modules are
95+
// mobile file: dependencies, not workspace packages). Local pnpm may resolve
96+
// from cached registry metadata, so this catches patches stranded by your
97+
// own change, not registry drift.
98+
const checkPatches = (): boolean => {
99+
const temp = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-patch-check-"));
100+
try {
101+
const copy = (relative: string) => {
102+
const dest = NodePath.join(temp, relative);
103+
NodeFS.mkdirSync(NodePath.dirname(dest), { recursive: true });
104+
NodeFS.cpSync(NodePath.join(root, relative), dest, { recursive: true });
105+
};
106+
copy("package.json");
107+
copy("pnpm-workspace.yaml");
108+
for (const pkg of packages) copy(`${pkg.dir}/package.json`);
109+
const modulesDir = NodePath.join(root, "apps/mobile/modules");
110+
if (NodeFS.existsSync(modulesDir)) {
111+
for (const entry of NodeFS.readdirSync(modulesDir)) {
112+
const manifest = `apps/mobile/modules/${entry}/package.json`;
113+
if (NodeFS.existsSync(NodePath.join(root, manifest))) copy(manifest);
114+
}
115+
}
116+
for (const extra of ["apps/mobile/deps", "patches", ".pnpmfile.cjs"]) {
117+
if (NodeFS.existsSync(NodePath.join(root, extra))) copy(extra);
118+
}
119+
const { status, stdout, stderr, error } = NodeChildProcess.spawnSync(
120+
"vp",
121+
["install", "--lockfile-only", "--ignore-scripts"],
122+
{ cwd: temp, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 },
123+
);
124+
if (status === 0) return true;
125+
if (stdout) console.log(stdout);
126+
if (stderr) console.error(stderr);
127+
if (error) console.error(error.message);
128+
if (`${stdout ?? ""}${stderr ?? ""}`.includes("ERR_PNPM_UNUSED_PATCH")) {
129+
console.error(
130+
"Unused dependency patches: remove each patch named above from " +
131+
"patchedDependencies in pnpm-workspace.yaml and from patches/, " +
132+
"update it to the version the install resolves, " +
133+
"or pin the dependency that pulls it in to the patched version.",
134+
);
135+
}
136+
return false;
137+
} finally {
138+
NodeFS.rmSync(temp, { recursive: true, force: true });
139+
}
140+
};
141+
74142
// CI's Lint job checks that generated GraphQL operations match their sources.
75143
const graphql = ["apps/server", "packages/source-control-github"].filter((dir) =>
76144
files.some((file) => file.startsWith(`${dir}/`)),
77145
);
78146

79147
const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);
80148
const steps = [
149+
// Runs first; it takes about a second.
150+
runPatchCheck && { name: "unused patches", run: checkPatches },
81151
present.length > 0 && {
82152
name: "format",
83153
args: ["fmt", "--check", "--no-error-on-unmatched-pattern", ...present],
@@ -103,7 +173,12 @@ console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`);
103173
const results = steps.map((step) => {
104174
console.log(`\n> vp ${step.name}`);
105175
const started = performance.now();
106-
const { status } = NodeChildProcess.spawnSync("vp", step.args, { cwd: root, stdio: "inherit" });
176+
const status =
177+
"args" in step
178+
? NodeChildProcess.spawnSync("vp", step.args, { cwd: root, stdio: "inherit" }).status
179+
: step.run()
180+
? 0
181+
: 1;
107182
const seconds = ((performance.now() - started) / 1000).toFixed(1);
108183
return { name: step.name, ok: status === 0, seconds };
109184
});
@@ -120,5 +195,6 @@ const ciOnly = Object.entries({
120195
"release smoke": outputs.release_smoke,
121196
}).flatMap(([job, runs]) => (runs ? [job] : []));
122197
if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);
198+
if (!runPatchCheck) console.log(" unused patch check skipped: no dependency files changed");
123199
if (!runKnip) console.log(" knip skipped: no code or manifest changed");
124200
if (results.some((result) => !result.ok)) process.exit(1);

0 commit comments

Comments
 (0)