Bug 描述 / Description
Running yarn start (or yarn dev) hangs silently with no output and no listening port when the project is not a git repository. The terminal shows the command was accepted but vite never prints its ready banner and http://localhost:5173/ returns nothing.
This affects anyone who downloads the source as a ZIP, clones only the inner project folder, or otherwise runs without a .git directory at or above the project root.
复现步骤 / Reproduction
- Remove (or never have)
.git at or above the project root — e.g. download a ZIP from GitHub instead of git clone.
yarn install (note: husky will warn fatal: not a git repository ... skipping install — that part is expected and harmless).
yarn start
- Observe: terminal shows the command running but nothing is printed — no
VITE v4.x ready in xxx ms, no Local: http://localhost:5173/. The process just sits there.
netstat -ano | findstr :5173 shows nothing listening.
期望行为 / Expected
vite starts normally and serves on http://localhost:5173/.
根因 / Root Cause
vite.config.ts calls git-last-commit to inject the latest commit hash as LATEST_COMMIT_HASH:
// vite.config.ts (current)
const latestCommitHash = await new Promise<string>((resolve) => {
return getLastCommit((err, commit) => (err ? 'unknown' : resolve(commit.shortHash)))
})
The err branch returns the string 'unknown' without calling resolve, so when git-last-commit errors (which it always does outside a git repo), the Promise never settles. Because defineConfig is called with an async factory, the config object is never returned, and vite sits idle forever waiting for a config that will never arrive.
That's why there's no output and no port — vite is genuinely stuck, not crashed.
建议修复 / Suggested Fix
Call resolve in the error branch:
const latestCommitHash = await new Promise<string>((resolve) => {
getLastCommit((err, commit) => {
if (err) resolve('unknown')
else resolve(commit.shortHash)
})
})
Verified locally: after this one-line fix, yarn start prints VITE v4.3.8 ready in 314 ms and serves on http://localhost:5173/ normally.
环境 / Environment
- Node: v18+
- vite: 4.3.8 (as pinned in package.json)
- OS: Windows 11
- Reproduces in any environment where
git rev-parse fails inside the project directory
附注
yarn install already prints a husky warning about the missing git repo, which is the first hint. But the yarn start failure is completely silent — no error, no stack, no exit — making this very hard to debug for users who don't already know vite internals.
Bug 描述 / Description
Running
yarn start(oryarn dev) hangs silently with no output and no listening port when the project is not a git repository. The terminal shows the command was accepted but vite never prints its ready banner andhttp://localhost:5173/returns nothing.This affects anyone who downloads the source as a ZIP, clones only the inner project folder, or otherwise runs without a
.gitdirectory at or above the project root.复现步骤 / Reproduction
.gitat or above the project root — e.g. download a ZIP from GitHub instead ofgit clone.yarn install(note: husky will warnfatal: not a git repository ... skipping install— that part is expected and harmless).yarn startVITE v4.x ready in xxx ms, noLocal: http://localhost:5173/. The process just sits there.netstat -ano | findstr :5173shows nothing listening.期望行为 / Expected
vite starts normally and serves on
http://localhost:5173/.根因 / Root Cause
vite.config.tscallsgit-last-committo inject the latest commit hash asLATEST_COMMIT_HASH:The
errbranch returns the string'unknown'without callingresolve, so whengit-last-commiterrors (which it always does outside a git repo), the Promise never settles. BecausedefineConfigis called with anasyncfactory, the config object is never returned, and vite sits idle forever waiting for a config that will never arrive.That's why there's no output and no port — vite is genuinely stuck, not crashed.
建议修复 / Suggested Fix
Call
resolvein the error branch:Verified locally: after this one-line fix,
yarn startprintsVITE v4.3.8 ready in 314 msand serves onhttp://localhost:5173/normally.环境 / Environment
git rev-parsefails inside the project directory附注
yarn installalready prints a husky warning about the missing git repo, which is the first hint. But theyarn startfailure is completely silent — no error, no stack, no exit — making this very hard to debug for users who don't already know vite internals.