嘿,各位开发者伙伴们!
有没有遇到过这样的场景:你刚刚完成了一段惊艳的代码,心满意足地敲下 git commit -m "feat: Add awesome feature"
,期待着将成果纳入版本库,结果…… “啪叽” 一声,终端跳出一堆红色的错误信息,告诉你 pre-commit hook failed
!
就像这样:
2025-04-14 23:03:10.457 [info] > git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file - [1324ms]
2025-04-14 23:03:10.457 [info] > running pre-commit hook: lint-staged
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.{js,vue}
[STARTED] vue-cli-service lint
[FAILED] vue-cli-service lint [FAILED]
[...]
✖ vue-cli-service lint:
Browserslist: caniuse-lite is outdated. Please run:
npx update-browserslist-db@latest
Why you should do it regularly: https://github.com/browserslist/update-db#readme
=============
WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.
[...]
SUPPORTED TYPESCRIPT VERSIONS: >=3.2.1 <3.6.0
YOUR TYPESCRIPT VERSION: 3.9.10
[...]
=============
error: Parsing error: x-invalid-end-tag (vue/no-parsing-error) at src/views/login/index3.vue:739:1:
737 | }
738 |
> 739 |
| ^
1 error found.
pre-commit hook failed (add --no-verify to bypass)
[...]
别担心,这其实是件好事!这说明你的项目配置了 pre-commit
钩子 (hook),它在你提交代码 之前 自动运行了一些检查(比如代码风格检查 Linter),确保你不会把有问题或不规范的代码提交上去。今天,我们就来解剖这个常见的错误场景,看看如何快速定位并解决问题。
让我们一步步分解日志信息:
git commit ...
: 你尝试进行一次 Git 提交。running pre-commit hook: lint-staged
: Git 触发了预提交钩子,这个钩子由 lint-staged
这个工具管理。lint-staged
的作用是只对你这次 git add
暂存区里的文件执行指定的命令(比如 Linter 或 Formatter)。Running tasks for *.{js,vue}
: lint-staged
检测到暂存区有 .js
或 .vue
文件,准备对它们执行任务。vue-cli-service lint
: lint-staged
配置的任务是执行 Vue CLI 提供的 lint 命令。[FAILED] vue-cli-service lint [FAILED]
: 关键来了!Lint 命令执行失败了。❌✖ vue-cli-service lint:
: 这里开始是 Linter 输出的具体错误信息。pre-commit hook failed (add --no-verify to bypass)
: 因为 Linter 失败,整个 pre-commit 钩子失败,最终导致你的 git commit
被中止。提示信息说可以用 --no-verify
参数跳过检查,但这通常不是推荐的做法(治标不治本)。Linter 的输出信息通常包含两部分:警告 (Warnings) 和 错误 (Errors)。
警告 ⚠️ (Warnings):
Browserslist: caniuse-lite is outdated...
: 提示你项目中用于浏览器兼容性查询的 caniuse-lite
数据库有点旧了。建议运行 npx update-browserslist-db@latest
来更新它。这通常不影响提交,但保持更新是个好习惯。WARNING: ...TypeScript which is not officially supported by typescript-estree...
: 你的 TypeScript 版本(3.9.10)比 typescript-estree
(ESLint 解析 TS 代码的核心库)官方支持的版本范围(❤️.6.0)要新。这 可能 会导致一些未知的解析问题,但在此次失败中,它只是个警告,不是直接原因。错误 ❌ (Error):
error: Parsing error: x-invalid-end-tag (vue/no-parsing-error) at src/views/login/index3.vue:739:1:
: 这才是导致 lint 失败、提交被阻止的罪魁祸首!
Parsing error
: 说明代码有语法错误,Linter 无法正确解析。x-invalid-end-tag
: 无效的结束标签。src/views/login/index3.vue:739:1
: 错误发生在 src/views/login/index3.vue
文件的第 739 行,第 1 列。 737 | }
738 |
> 739 |
| ^
一目了然!在第 738 行已经有一个
结束标签了,第 739 行又来一个,这显然是多余的,导致了 Vue 模板解析失败。现在问题已经很清楚了,解决起来就非常简单:
src/views/login/index3.vue
。
标签。npx update-browserslist-db@latest
更新浏览器数据库。typescript-estree
的支持范围,或者暂时忽略这个警告(如果目前没遇到实际问题)。git add src/views/login/index3.vue
git commit
命令。这次,pre-commit 钩子应该就能顺利通过,你的代码也就成功提交啦!✅
类型 | 问题 | 文件路径 | 行号 | 原因 | 解决方案 |
---|---|---|---|---|---|
错误 ❌ | Parsing error: x-invalid-end-tag |
src/views/login/index3.vue |
739 | 多余的 结束标签 |
打开文件,删除第 739 行的
|
警告 ⚠️ | Browserslist: caniuse-lite is outdated |
(项目配置) | N/A | 浏览器数据库版本过旧 | 运行 npx update-browserslist-db@latest |
警告 ⚠️ | TypeScript version not supported by typescript-estree |
(项目配置) | N/A | TS 版本超出解析器支持范围 | 评估风险,考虑升级/降级 TS 或 ESLint 相关依赖,或暂时忽略(如果项目运行正常) |
遇到 pre-commit hook failed
并不可怕,它反而是保障我们代码质量的忠实卫士。通过仔细阅读错误日志,理解 Linter 的输出,我们就能快速定位问题并修复它。记住,大部分时候,问题可能就是一个小小的语法错误或者不符合规范的写法。
下次再遇到类似情况,希望这篇博客能帮你从容应对!Happy coding!