Repository navigation
feat(scripts): one command checks what the branch changed #22767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| interface WorkspacePackage { | ||
| readonly name: string; | ||
| readonly dir: string; | ||
| readonly hasTest: boolean; | ||
| readonly deps: ReadonlyArray<string>; | ||
| } | ||
|
|
||
| export function readWorkspace(): Array<WorkspacePackage>; | ||
|
|
||
| export function decide( | ||
| files: ReadonlyArray<string> | null, | ||
| packages: ReadonlyArray<WorkspacePackage>, | ||
| full: boolean, | ||
| ): { | ||
| affected: Array<string>; | ||
| outputs: { | ||
| typecheck: boolean; | ||
| build: boolean; | ||
| test: boolean; | ||
| test_packages: string; | ||
| test_web: boolean; | ||
| test_server: boolean; | ||
| rust: boolean; | ||
| release_smoke: boolean; | ||
| mobile_native: boolean; | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // @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. | ||
| // | ||
| // vp run check:changed compare against the merge base with origin/main | ||
| // vp run check:changed <ref> compare against <ref> | ||
| // | ||
| // Uncommitted and untracked files count. The package mapping comes from | ||
| // .github/scripts/ci-changes.cjs, so this typechecks what CI would typecheck. | ||
| import * as NodeChildProcess from "node:child_process"; | ||
| import * as NodeFS from "node:fs"; | ||
| import * as NodePath from "node:path"; | ||
| import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs"; | ||
|
|
||
| const root = NodePath.resolve(import.meta.dirname, ".."); | ||
| const CODE = /\.[cm]?[jt]sx?$/; | ||
|
|
||
| const git = (...args: Array<string>) => | ||
| NodeChildProcess.execFileSync("git", args, { cwd: root, encoding: "utf8" }) | ||
| .split("\n") | ||
| .filter(Boolean); | ||
|
|
||
| const base = process.argv[2] ?? git("merge-base", "HEAD", "origin/main")[0] ?? "origin/main"; | ||
| // --no-renames lists a rename as its old and new path, as CI does. | ||
| const untracked = git("ls-files", "--others", "--exclude-standard"); | ||
| const files = [ | ||
| ...new Set([...git("diff", "--name-only", "--no-renames", base, "--"), ...untracked]), | ||
| ].sort(); | ||
|
|
||
| if (files.length === 0) { | ||
| console.log(`check:changed: no changes since ${base.slice(0, 12)}.`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const present = files.filter((file) => NodeFS.existsSync(NodePath.join(root, file))); | ||
| const code = present.filter((file) => CODE.test(file)); | ||
| const packages = readWorkspace(); | ||
| const { affected, outputs } = decide(files, packages, false); | ||
|
|
||
| // Changed test files, and the test files beside a changed source file | ||
| // (foo.ts -> foo.test.ts, foo.browser.test.tsx). Vitest's import-graph | ||
| // `related` reaches most of a package: one leaf server file pulled in 252 of | ||
| // 406 server test files and took 7 minutes. CI runs the rest. | ||
| const TEST = /\.test\.[cm]?[jt]sx?$/; | ||
| const tests = new Set(code.filter((file) => TEST.test(file))); | ||
| for (const file of code) { | ||
| const dir = NodePath.dirname(file); | ||
| const prefix = `${NodePath.basename(file).replace(CODE, "")}.`; | ||
| for (const entry of NodeFS.readdirSync(NodePath.join(root, dir))) { | ||
| if (entry.startsWith(prefix) && TEST.test(entry)) tests.add(NodePath.join(dir, entry)); | ||
| } | ||
| } | ||
|
|
||
| // 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", | ||
| ); | ||
|
|
||
| const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]); | ||
| const steps = [ | ||
| present.length > 0 && { | ||
| name: "format", | ||
| args: ["fmt", "--check", "--no-error-on-unmatched-pattern", ...present], | ||
| }, | ||
| code.length > 0 && { name: "lint", args: ["lint", ...code] }, | ||
| runKnip && { name: "knip", args: ["run", "knip:check"] }, | ||
| affected.length > 0 && { | ||
| name: `typecheck (${affected.length} packages)`, | ||
| args: ["run", "--cache", "--concurrency-limit", "4", ...filters(affected), "typecheck"], | ||
| }, | ||
| // From the repo root, the root vite.config.ts hands each file to its package's config. | ||
| tests.size > 0 && { | ||
| name: `test (${tests.size} files)`, | ||
| args: ["test", "run", "--passWithNoTests", ...tests], | ||
| }, | ||
| ].filter((step) => step !== false); | ||
|
|
||
| console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)}`); | ||
| const results = steps.map((step) => { | ||
| console.log(`\n> vp ${step.name}`); | ||
| const started = performance.now(); | ||
| const { status } = NodeChildProcess.spawnSync("vp", step.args, { cwd: root, stdio: "inherit" }); | ||
| const seconds = ((performance.now() - started) / 1000).toFixed(1); | ||
| return { name: step.name, ok: status === 0, seconds }; | ||
| }); | ||
|
|
||
| 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], | ||
| ); | ||
| if (ciOnly.length > 0) console.log(` CI also runs: ${ciOnly.join(", ")}`); | ||
| if (!runKnip) console.log(" knip skipped: no files, exports, or dependencies changed"); | ||
| if (results.some((result) => !result.ok)) process.exit(1); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Report spawn failures as failures.
If
vpis not onPATH,spawnSyncreturnsstatus: nulland setserror. The code then reportsFAILwithout a cause. Printerror.messageso the user can see why the step failed. The exit code is still correct.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents