You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go编写的自定义pre-commit钩子执行时提示“找不到main可执行文件”求助

问题原因与解决方案

核心问题

pre-commit的golang语言类型钩子,工作逻辑是自动编译指定的Go源文件,而非直接调用预编译的可执行文件。同时你预编译的scripts/main大概率被.gitignore排除、未提交到Git仓库,导致pre-commit在缓存目录中找不到该文件。

正确配置步骤

1. 修改.pre-commit-hooks.yaml

将entry指向你的Go源文件,而非预编译的可执行文件:

- id: comche
  name: comche
  description: "Check Python files for TODO, BUG, and FIXME comments"
  entry: cmd/main.go
  language: golang
  language_version: system
  types: [python]
  additional_dependencies: []

2. 提交Go模块文件

确保go.mod(以及生成的go.sum)已提交到Git仓库,pre-commit需要这些文件完成依赖拉取与构建。

3. 移除本地预编译依赖

无需手动构建scripts/main,pre-commit会在钩子运行前自动编译cmd/main.go,生成临时可执行文件执行检查。

4. 重新测试钩子

在目标仓库执行以下命令重新安装并测试:

pre-commit uninstall
pre-commit install
pre-commit run --all-files

额外说明

  • 若你的.gitignore包含可执行文件(如scripts/main),这是合理的,无需提交预编译文件,pre-commit会自动处理构建流程。
  • 若项目有额外Go依赖,确保go.mod中已正确声明,pre-commit会自动拉取这些依赖完成构建。

内容的提问来源于stack exchange,提问作者Adrien Riaux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 18:49:52