Skip to content

Commit 5d087b7

Browse files
authored
fix(config): check correct path to prevent re-prompting about pre-hooks (#765)
The `isFirstHooksRun` check looked for `.vite-hooks/pre-commit` (user-defined hook) but `install()` only creates `.vite-hooks/_/pre-commit` (internal shim). This caused `vp config` to re-prompt about hooks on every run.
1 parent 775616a commit 5d087b7

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

packages/cli/src/config/__tests__/hooks.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { existsSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
14
import { describe, expect, it } from 'vitest';
25

3-
import { hookScript } from '../hooks.js';
6+
import { hookScript, install } from '../hooks.js';
47

58
function countDirnameCalls(script: string): number {
69
// Count nested dirname calls in the `d=...` line
@@ -11,6 +14,34 @@ function countDirnameCalls(script: string): number {
1114
return (match[1].match(/dirname/g) ?? []).length;
1215
}
1316

17+
describe('install', () => {
18+
it('should create _/pre-commit but not pre-commit in hooks dir root', () => {
19+
const { execSync } = require('node:child_process');
20+
const { mkdtempSync, rmSync } = require('node:fs');
21+
const { tmpdir } = require('node:os');
22+
23+
const tmp = mkdtempSync(join(tmpdir(), 'hooks-test-'));
24+
const originalCwd = process.cwd();
25+
try {
26+
// Set up a temporary git repo
27+
execSync('git init', { cwd: tmp, stdio: 'ignore' });
28+
process.chdir(tmp);
29+
30+
const hooksDir = '.vite-hooks';
31+
const result = install(hooksDir);
32+
expect(result.isError).toBe(false);
33+
34+
// install() creates the internal shim at _/pre-commit
35+
expect(existsSync(join(tmp, hooksDir, '_', 'pre-commit'))).toBe(true);
36+
// install() does NOT create pre-commit at the hooks dir root
37+
expect(existsSync(join(tmp, hooksDir, 'pre-commit'))).toBe(false);
38+
} finally {
39+
process.chdir(originalCwd);
40+
rmSync(tmp, { recursive: true, force: true });
41+
}
42+
});
43+
});
44+
1445
describe('hookScript', () => {
1546
it('should compute correct depth for simple dir', () => {
1647
// ".vite-hooks" → 1 segment → depth 3

packages/cli/src/config/bin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function main() {
6262

6363
// --- Step 1: Hooks setup ---
6464
const hooksDir = dir ?? '.vite-hooks';
65-
const isFirstHooksRun = !existsSync(join(root, hooksDir, 'pre-commit'));
65+
const isFirstHooksRun = !existsSync(join(root, hooksDir, '_', 'pre-commit'));
6666

6767
let shouldSetupHooks = true;
6868
if (interactive && isFirstHooksRun && !dir) {

0 commit comments

Comments
 (0)