From adf435d36f4ad2ef320305ca620e57803ae63f8c Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 22 Oct 2026 22:42:08 +0200 Subject: [PATCH] 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. --- scripts/hooks/check-console-log.js | 10 +++++++--- tests/hooks/hooks.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/scripts/hooks/check-console-log.js b/scripts/hooks/check-console-log.js index 28f3ac6db..03a5899f6 100755 --- a/scripts/hooks/check-console-log.js +++ b/scripts/hooks/check-console-log.js @@ -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; diff --git a/tests/hooks/hooks.test.js b/tests/hooks/hooks.test.js index 899b05a69..71eb2e8ca 100644 --- a/tests/hooks/hooks.test.js +++ b/tests/hooks/hooks.test.js @@ -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:');