Skip to content

Commit 9517aef

Browse files
committed
fix(config): skip git-hook prompt when staged config already exists
When a user clones a project with staged config in vite.config.ts and manually runs `vp config`, the prompt was incorrectly shown because hook shims don't exist after clone (they're gitignored). The existing staged config is sufficient signal that the project opted into hooks. Add `hasStagedConfigInViteConfig(root)` check to the prompt condition so projects with staged config auto-install hooks without prompting. Closes #1154
1 parent 5f52b6c commit 9517aef

6 files changed

Lines changed: 87 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "command-config-auto-hooks"
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
> git init
2+
> vp config # should install hooks automatically without prompting (staged config exists)
3+
> git config --local core.hooksPath # should be .vite-hooks/_
4+
.vite-hooks/_
5+
6+
> cat .vite-hooks/pre-commit # should have vp staged
7+
vp staged
8+
9+
> cat vite.config.ts # should remain unchanged
10+
import { defineConfig } from 'vite-plus';
11+
12+
export default defineConfig({
13+
staged: {
14+
"*": "vp check --fix"
15+
},
16+
17+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"commands": [
3+
{ "command": "git init", "ignoreOutput": true },
4+
"vp config # should install hooks automatically without prompting (staged config exists)",
5+
"git config --local core.hooksPath # should be .vite-hooks/_",
6+
"cat .vite-hooks/pre-commit # should have vp staged",
7+
"cat vite.config.ts # should remain unchanged"
8+
]
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite-plus';
2+
3+
export default defineConfig({
4+
staged: {
5+
'*': 'vp check --fix',
6+
},
7+
});

packages/cli/src/config/bin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { join } from 'node:path';
1010
import mri from 'mri';
1111

1212
import { vitePlusHeader } from '../../binding/index.js';
13-
import { ensurePreCommitHook } from '../migration/migrator.js';
13+
import { ensurePreCommitHook, hasStagedConfigInViteConfig } from '../migration/migrator.js';
1414
import { updateExistingAgentInstructions } from '../utils/agent.js';
1515
import { renderCliDoc } from '../utils/help.js';
1616
import { defaultInteractive, promptGitHooks } from '../utils/prompts.js';
@@ -62,9 +62,16 @@ async function main() {
6262
const isFirstHooksRun = !existsSync(join(root, hooksDir, '_', 'pre-commit'));
6363

6464
let shouldSetupHooks = true;
65-
if (interactive && isFirstHooksRun && !dir && !isPrepareScript) {
65+
if (
66+
interactive &&
67+
isFirstHooksRun &&
68+
!dir &&
69+
!isPrepareScript &&
70+
!hasStagedConfigInViteConfig(root)
71+
) {
6672
// --hooks-dir implies agreement; only prompt when using default dir on first run
6773
// prepare script implies the project opted into hooks — install automatically
74+
// existing staged config in vite.config.ts implies the project already opted in
6875
shouldSetupHooks = await promptGitHooks({ interactive });
6976
}
7077

rfcs/config-and-staged-commands.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,48 @@ Husky <9.0.0 is not supported by auto migration — `vp migrate` detects unsuppo
282282
| **`vp config`** | **Reinstall hook shims + agent setup** | **npm `prepare` lifecycle** |
283283
| **`vp staged`** | **Run staged linters on staged files** | **Pre-commit hook** |
284284

285+
## `vp config` Hooks Setup Flow
286+
287+
```
288+
vp config
289+
290+
├─ VITE_GIT_HOOKS=0 or HUSKY=0? ──→ Skip hooks (exit 0)
291+
292+
├─ Not inside a git repo? ──→ Skip hooks (exit 0)
293+
294+
├─ Should prompt user?
295+
│ Prompt ONLY when ALL of these are true:
296+
│ • Interactive terminal (not CI, not piped)
297+
│ • First run (hook shims don't exist yet)
298+
│ • No --hooks-dir flag
299+
│ • Not running from prepare lifecycle
300+
│ • No staged config in vite.config.ts ← NEW (#1154)
301+
302+
│ YES → Prompt "Set up pre-commit hooks?"
303+
│ User declines → skip hooks
304+
│ NO → Auto-install hooks
305+
306+
├─ core.hooksPath already set to a custom path?
307+
│ (not .vite-hooks/_, not .husky)
308+
│ └─ YES → Skip hooks, preserve custom config
309+
310+
├─ Set core.hooksPath → .vite-hooks/_
311+
├─ Create hook shims in .vite-hooks/_/
312+
├─ Ensure staged config in vite.config.ts
313+
└─ Ensure .vite-hooks/pre-commit contains "vp staged"
314+
```
315+
316+
### When does the prompt appear?
317+
318+
| Caller | Prompts? | Why |
319+
| ------------------------------------- | -------- | ------------------------------------------------ |
320+
| `npm install` → prepare | No | prepare lifecycle = auto-install |
321+
| Manual, project has `staged` config | No | staged config = already opted in **(#1154 fix)** |
322+
| Manual, no `staged` config, first run | **Yes** | No signal that project wants hooks |
323+
| Manual, already ran before | No | Hook shims exist = not first run |
324+
| CI / non-interactive | No | Non-interactive = auto-install |
325+
| `--hooks-dir` flag | No | Explicit flag = intent to install |
326+
285327
## Comparison with Other Tools
286328

287329
| Tool | Approach |

0 commit comments

Comments
 (0)