Adrien Riaux
Adrien Riaux

Reputation: 533

Executable not found while running custom pre-commit hook written in Go

I am trying to build a custom pre-commit hook written in Go. It should run on python file.

The project has the following architecture:

comche/
├── cmd/
│   └── main.go
├── scripts/
│   └── main -> build with "go build -o scripts/ cmd/main.go"
├── .pre-commit-hooks.yaml
├── go.mod
├── .gitignore
└── README.md

I have setup the .pre-commit-hooks.yaml has follow:

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

But when I install/setup the pre-commit on another repo to make test, I have the following error: "Executable main not found"!

Note that the hook is run by pre-commit and the installation works well, but when I commit a Python file (which trigger my hook), I have the previous error.

I have tried to change the entry point, such as, ./scripts/main, cmd/main.go, main.go or comche/scripts/main without success.

Do you have any id of what I am missing?

Thanks in advance for your help!

Upvotes: 1

Views: 705

Answers (2)

Adrien Riaux
Adrien Riaux

Reputation: 533

To give an alternative solution to this question using the same project architecture (and because I didn't manage to use language: golang), I was able to make it works using script language, since go build command build an executable.

I just change a little the .pre-commit-hooks.yaml file to match the correct requirements:

- id: comche
  name: comche
  description: "Check Python files for TODO, BUG, and FIXME comments"
  entry: ./scripts/main
  language: script
  types: [python]

However, it is important to remember to call the go build command at each update of the go source code.

Upvotes: 0

anthony sottile
anthony sottile

Reputation: 70175

for language: golang the executables must be present by running go install ./... -- your custom go build -o scripts command is not the convention that go follows and so your executable isn't being created in the way you expect

you'll need to make your go source tree adhere to go's standard conventions for executables in order for them to work with pre-commit


disclaimer: I wrote pre-commit

Upvotes: 1

Related Questions