fix(hooks): find console.log in modified files from a subdirectory session

check-console-log took the paths from `git diff --name-only HEAD`, which are relative to the repository root, and checked them with fs.existsSync from the hook's working directory. In a session started in a subdirectory, such as a monorepo package, no path existed from there, so the hook never warned. Resolve each path from the repository root. The exclusion patterns and the warning still use the root-relative path.
This commit is contained in:
L4XB committed 2026-10-22 22:42:08 +02:00
1 parent 90606c8315
commit adf435d36f
2 files changed
+31 -3

No files matched your search

+7 -3
View File
@@ -14,7 +14,8 @@
*/
const fs = require('fs');
const { isGitRepo, getGitModifiedFiles, readFile, log } = require('../lib/utils');
const path = require('path');
const { isGitRepo, getGitModifiedFiles, readFile, log, runCommand } = require('../lib/utils');
// Files where console.log is expected and should not trigger warnings
const EXCLUDED_PATTERNS = [
@@ -66,14 +67,17 @@ process.stdin.on('end', () => {
return;
}
// git reports paths from the repository root, and the session can run in a
// subdirectory of it, so read each file from the root.
const root = runCommand('git rev-parse --show-toplevel').output;
const files = getGitModifiedFiles(['\\.tsx?$', '\\.jsx?$'])
.filter(f => fs.existsSync(f))
.filter(f => fs.existsSync(path.join(root, f)))
.filter(f => !EXCLUDED_PATTERNS.some(pattern => pattern.test(f)));
let hasConsole = false;
for (const file of files) {
const content = readFile(file);
const content = readFile(path.join(root, file));
if (content && content.includes('console.log')) {
log(`[Hook] WARNING: console.log found in ${file}`);
hasConsole = true;
+24
View File
@@ -1201,6 +1201,30 @@ async function runTests() {
passed++;
else failed++;
if (
await asyncTest('finds console.log in a modified file when the session runs in a subdirectory', async () => {
// git reports paths from the repository root, not from the session's directory.
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-console-log-'));
try {
const appDir = path.join(repo, 'packages', 'app');
fs.mkdirSync(path.join(appDir, 'src'), { recursive: true });
execFileSync('git', ['init', '-q'], { cwd: repo });
fs.writeFileSync(path.join(appDir, 'src', 'a.ts'), 'export const a = 1;\n');
execFileSync('git', ['add', '-A'], { cwd: repo });
execFileSync('git', ['-c', 'user.email=t@t.local', '-c', 'user.name=t', '-c', 'commit.gpgsign=false', 'commit', '-q', '-m', 'seed'], { cwd: repo });
fs.writeFileSync(path.join(appDir, 'src', 'a.ts'), 'export const a = 1;\nconsole.log(a);\n');
const result = await runScript(path.join(scriptsDir, 'check-console-log.js'), '{}', {}, appDir);
assert.strictEqual(result.code, 0);
assert.ok(result.stderr.includes('console.log found in packages/app/src/a.ts'), `Should warn about the file, got: ${result.stderr}`);
} finally {
fs.rmSync(repo, { recursive: true, force: true });
}
})
)
passed++;
else failed++;
// session-end.js tests
console.log('\nsession-end.js:');