perf(cli): cache pnpm run ktx builds against a stamp file (#113)

The staleness check compared source mtimes against packages/cli/dist/bin.js,
but tsc only rewrites outputs whose source actually changed. Editing any
non-bin source (e.g. setup.ts) left bin.js untouched, so its mtime stayed
older than the sources forever and every `pnpm run ktx` invocation
rebuilt the whole workspace. Write a dedicated .ktx-build-stamp after a
successful build and check sources against that instead.
This commit is contained in:
Andrey Avtomonov 2026-05-15 15:49:39 +02:00 committed by GitHub
parent 50ffebd98b
commit f9532f549b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 166 additions and 15 deletions

View file

@ -2,7 +2,12 @@
import { spawn } from 'node:child_process';
import { constants } from 'node:fs';
import { access as fsAccess, readdir as fsReaddir, stat as fsStat } from 'node:fs/promises';
import {
access as fsAccess,
readdir as fsReaddir,
stat as fsStat,
writeFile as fsWriteFile,
} from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
@ -14,6 +19,10 @@ function cliBinPath(rootDir) {
return resolve(rootDir, 'packages', 'cli', 'dist', 'bin.js');
}
function buildStampPath(rootDir) {
return resolve(rootDir, 'packages', 'cli', 'dist', '.ktx-build-stamp');
}
async function fileExists(path, access) {
try {
await access(path, constants.R_OK);
@ -66,17 +75,17 @@ async function newestMtimeMs(path, fs) {
return newest;
}
async function isBuildStale(rootDir, binPath, fs) {
let binStats;
async function isBuildStale(rootDir, stampPath, fs) {
let stampStats;
try {
binStats = await fs.stat(binPath);
stampStats = await fs.stat(stampPath);
} catch {
return true;
}
const inputPaths = await packageBuildInputPaths(rootDir, fs.readdir);
for (const inputPath of inputPaths) {
if ((await newestMtimeMs(inputPath, fs)) > binStats.mtimeMs) {
if ((await newestMtimeMs(inputPath, fs)) > stampStats.mtimeMs) {
return true;
}
}
@ -137,7 +146,9 @@ export async function runWorkspaceKtx(argv, options = {}) {
stat: options.stat ?? fsStat,
readdir: options.readdir ?? fsReaddir,
};
const writeFile = options.writeFile ?? fsWriteFile;
const binPath = cliBinPath(rootDir);
const stampPath = buildStampPath(rootDir);
const runCommand =
options.runCommand ??
(options.execFile
@ -146,7 +157,7 @@ export async function runWorkspaceKtx(argv, options = {}) {
const commandEnv = options.env;
const binExists = await fileExists(binPath, access);
const needsBuild = !binExists || (await isBuildStale(rootDir, binPath, fs));
const needsBuild = !binExists || (await isBuildStale(rootDir, stampPath, fs));
if (needsBuild) {
stderr.write(
binExists
@ -160,6 +171,7 @@ export async function runWorkspaceKtx(argv, options = {}) {
);
return buildExitCode;
}
await writeFile(stampPath, '');
}
return await runCommand(process.execPath, [binPath, ...cliArgv], { cwd: rootDir, env: commandEnv });