From b6f21e59ea97c2c80e89e65e719d3299dddc0fc6 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 14 Feb 2026 18:38:56 +0100 Subject: [PATCH] fix(monorepo-tools): fix precommit hook This script was broken in some bizarre ways -- for example, it assumed that "monorepo root" and "root" were a) different and b) located at a fixed relative path to the filename of the precommit script, or it assumed that the repository would always have a `.gitmodules` file -- that make it very unclear to me how these issues went unnoticed for as long as they did. --- packages/monorepo-tools/src/precommit.ts | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/monorepo-tools/src/precommit.ts b/packages/monorepo-tools/src/precommit.ts index bf8d573e..142c9a67 100644 --- a/packages/monorepo-tools/src/precommit.ts +++ b/packages/monorepo-tools/src/precommit.ts @@ -6,24 +6,35 @@ import pkgUp from 'pkg-up'; import { promisify } from 'util'; import { execFile } from 'child_process'; import * as fs from 'fs/promises'; +import findUp from 'find-up'; const execFileAsync = promisify(execFile); -const monorepoRoot = path.resolve(__dirname, '..', '..'); -const repoRoot = path.resolve(monorepoRoot, '..'); - async function main(fileList: string[]) { + const monorepoGit = await findUp('.git', { + cwd: __dirname, + type: 'directory', + }); + if (!monorepoGit) { + throw new Error( + `Could not find .git directory for monorepo starting from ${__dirname}`, + ); + } + const monorepoRoot = path.dirname(monorepoGit); + const filesToPrettify: string[] = []; const submodules = [ ...( - await fs.readFile(path.resolve(repoRoot, '.gitmodules'), { - encoding: 'utf8', - }) + await fs + .readFile(path.resolve(monorepoRoot, '.gitmodules'), { + encoding: 'utf8', + }) + .catch(() => '') ).matchAll(/^\s+path = (?.*)$/gm), ] .map((r) => r.groups?.submodulePath) - .filter((p) => p) - .map((p) => path.resolve(repoRoot, p!)); + .filter((p): p is string => !!p) + .map((p) => path.resolve(monorepoRoot, p)); await Promise.all( fileList.map(async (filePath) => {