perf: Share Cargo build cache across git worktrees (#60)

Use a common target directory for all worktrees to avoid recompiling the backend in every worktree directory.
This commit is contained in:
Solomon
2025-07-07 15:27:00 +01:00
committed by GitHub
parent f82b6fd246
commit dc008b70ca
3 changed files with 41 additions and 9 deletions

31
scripts/cargo.js Normal file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/env node
/**
* Drop-in replacement for the `cargo` binary.
* If CARGO_TARGET_DIR is already set we leave it alone.
* Otherwise we compute a path that is the same for every git-worktree:
* <main-clone-root>/target
*
* Usage : node scripts/cargo.js <cargo-subcommand> […]
* NPM : "backend:build": "node scripts/cargo.js build --release …"
*/
const { spawnSync, execSync } = require('node:child_process');
const path = require('node:path');
function sharedTarget() {
try {
// Works in any work-tree folder
const common = execSync('git rev-parse --git-common-dir', { encoding: 'utf8' }).trim();
return path.resolve(common, '..', 'target');
} catch {
return undefined; // not a git repo? -> let Cargo use ./target
}
}
const env = { ...process.env };
if (!env.CARGO_TARGET_DIR) {
const dir = sharedTarget();
if (dir) env.CARGO_TARGET_DIR = dir;
}
const result = spawnSync('cargo', process.argv.slice(2), { stdio: 'inherit', env });
process.exitCode = result.status;