TypeScript
fix(scripts): check:changed no longer skips knip after a removed import (#22784)
Theo Browne1f4eae725314
diff --git a/AGENTS.md b/AGENTS.mdindex 4e42bead784..c8cd110dd1e 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -98,7 +98,7 @@ An empty database is a bad test. Seed your worktree's `.t3` with a copy of real## Verifying-- Before you push, run `vp run check:changed`. It runs CI checks for changed files: format, lint, tests beside changes, typecheck of affected packages and dependents, plus knip when files, exports, or dependencies change.+- Before you push, run `vp run check:changed`. It runs CI's fast checks for changed files: format, lint, knip, typecheck of affected packages and dependents, and tests beside changes. CI still runs the full test suites.- While you iterate, run `vp test run <files>`. For big files, add `-t "<name>"`.- Do not use `npx tsc`. Use `vp run typecheck`; it uses tsc-rs, which is 3x faster.- Run `node -e` snippets from `apps/server` or the owning package. `effect` does not resolve from the repo root.diff --git a/docs/operations/development.md b/docs/operations/development.mdindex be3902946d3..5fcd6883641 100644--- a/docs/operations/development.md+++ b/docs/operations/development.md@@ -102,12 +102,12 @@ for the security model.## Checks-`vp run check:changed` runs what CI runs for your branch's changes: format and lint of the-changed files, `knip:check` when files, exports, or dependencies change, typecheck of the affected-packages and their dependents, and the test files beside the changed files. It maps files to-packages with [ci-changes.cjs](../../.github/scripts/ci-changes.cjs), as CI does. It compares-against the merge base with `origin/main`, or a ref you pass. Tests that reach a changed file only-through imports run in CI.+`vp run check:changed` runs CI's fast checks for your branch's changes: format and lint of the+changed files, `knip:check` when code or a manifest changes, the generated GraphQL checks, typecheck+of the affected packages and their dependents, and the test files beside the changed files. It maps+files to packages with [ci-changes.cjs](../../.github/scripts/ci-changes.cjs), as CI does. It+compares against the merge base with `origin/main`, or with a ref you pass. CI still runs the full+test suites and the jobs the summary lists, so a local pass does not promise a green CI run.While you iterate, run single checks:diff --git a/scripts/check-changed.ts b/scripts/check-changed.tsindex 7fff4618970..d717c7bbbe6 100644--- a/scripts/check-changed.ts+++ b/scripts/check-changed.ts@@ -1,13 +1,14 @@// @effect-diagnostics nodeBuiltinImport:off globalConsole:off-// Runs locally what CI runs for this branch's changes: format and lint of the-// changed files, knip, typecheck of the affected packages and their dependents,-// and the test files beside the changed files.+// Runs CI's fast checks for this branch's changes: format and lint of the+// changed files, knip, generated GraphQL operations, typecheck of the affected+// packages and their dependents, and the test files beside the changed files.+// CI still runs the full test suites and the jobs listed in the summary.//// vp run check:changed compare against the merge base with origin/main-// vp run check:changed <ref> compare against <ref>+// vp run check:changed <ref> compare against the merge base with <ref>//-// Uncommitted and untracked files count. The package mapping comes from-// .github/scripts/ci-changes.cjs, so this typechecks what CI would typecheck.+// Uncommitted and untracked files count. Affected packages come from+// .github/scripts/ci-changes.cjs, the same mapping CI uses to gate its jobs.import * as NodeChildProcess from "node:child_process";import * as NodeFS from "node:fs";import * as NodePath from "node:path";@@ -21,7 +22,18 @@ const git = (...args: Array<string>) =>.split("\n").filter(Boolean);-const base = process.argv[2] ?? git("merge-base", "HEAD", "origin/main")[0] ?? "origin/main";+const ref = process.argv[2] ?? "origin/main";+const mergeBase = NodeChildProcess.spawnSync("git", ["merge-base", "HEAD", ref], {+ cwd: root,+ encoding: "utf8",+});+if (mergeBase.status !== 0) {+ console.error(+ `check:changed: no merge base with ${ref}. Run \`git fetch origin\` or pass a ref.`,+ );+ process.exit(1);+}+const base = mergeBase.stdout.trim();// --no-renames lists a rename as its old and new path, as CI does.const untracked = git("ls-files", "--others", "--exclude-standard");const files = [@@ -52,25 +64,17 @@ for (const file of code) {}}-// knip:check takes about 35 s and cannot be scoped to a package, so it runs-// only when the change can leave a file, export, or dependency unused.-const addedOrDeleted = new Set([- ...git("diff", "--name-only", "--no-renames", "--diff-filter=AD", base, "--"),- ...untracked,-]);-const changedExport =- code.length > 0 &&- git("diff", "--unified=0", base, "--", ...code).some((line) =>- /^[+-](?!\+\+ |-- ).*\bexport\b/.test(line),- );-const runKnip =- changedExport ||- files.some(- (file) =>- (CODE.test(file) && addedOrDeleted.has(file)) ||- NodePath.basename(file) === "package.json" ||- file === "knip.jsonc",- );+// knip:check takes about 35 s and cannot be scoped to a package. Any code+// change can leave a file, export, or dependency unused (removing the last+// import of a helper does), so it runs whenever code or a manifest changes.+const runKnip = files.some(+ (file) => CODE.test(file) || NodePath.basename(file) === "package.json" || file === "knip.jsonc",+);++// CI's Lint job checks that generated GraphQL operations match their sources.+const graphql = ["apps/server", "packages/source-control-github"].filter((dir) =>+ files.some((file) => file.startsWith(`${dir}/`)),+);const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);const steps = [@@ -80,6 +84,10 @@ const steps = [},code.length > 0 && { name: "lint", args: ["lint", ...code] },runKnip && { name: "knip", args: ["run", "knip:check"] },+ ...graphql.map((dir) => ({+ name: `graphql (${dir})`,+ args: ["run", "--filter", `./${dir}`, "generate", "--check"],+ })),affected.length > 0 && {name: `typecheck (${affected.length} packages)`,args: ["run", "--cache", "--concurrency-limit", "4", ...filters(affected), "typecheck"],@@ -104,9 +112,13 @@ console.log("\ncheck:changed summary");for (const result of results) {console.log(` ${result.ok ? "pass" : "FAIL"} ${result.name} ${result.seconds}s`);}-const ciOnly = (["build", "rust", "mobile_native", "release_smoke"] as const).filter(- (job) => outputs[job],-);+const ciOnly = Object.entries({+ "full test suites": outputs.test || outputs.test_web || outputs.test_server,+ build: outputs.build,+ rust: outputs.rust,+ "mobile native lint": outputs.mobile_native,+ "release smoke": outputs.release_smoke,+}).flatMap(([job, runs]) => (runs ? [job] : []));if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`);-if (!runKnip) console.log(" knip skipped: no files, exports, or dependencies changed");+if (!runKnip) console.log(" knip skipped: no code or manifest changed");if (results.some((result) => !result.ok)) process.exit(1);