Bug: `yarn start` hangs silently when project is not a git repository (vite.config.ts lastCommitHash Promise never resolves)

Author: likangdi-codeCreated Jul 4, 2026Updated Jul 4, 2026

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

  1. Remove (or never have) .git at or above the project root — e.g. download a ZIP from GitHub instead of git clone.
  2. yarn install (note: husky will warn fatal: not a git repository ... skipping install — that part is expected and harmless).
  3. yarn start
  4. 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.
  5. 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:

typescript
// 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:

typescript
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.

Source: RealKai42/qwerty-learner