Search code examples
pre-commitpre-commit.com

How to run pre-commit on separate folders?


I use pre-commit, and I have a repository with two folders:

.
├── backend
│   └── (backend files)
├── frontend
│   └── (frontend files)
└── .pre-commit-config.yaml

Each folder has a Dockerfile and is independent from the other.

This is my .pre-commit-config.yaml:

repos:
  - repo: local
    hooks:
      - id: go-docker
        name: go
        language: docker_image
        entry: backend:latest go fmt
        files: backend/

      - id: prettier-docker
        name: prettier
        language: docker_image
        entry: frontend:latest npm run format
        files: frontend/

I want to run pre-commit on both folders with Docker, but I'm getting:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /src/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/src/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

According to the pre-commit documentation: pre-commit will automatically mount the repository source as a volume using -v $PWD:/src:rw,Z and set the working directory using --workdir /src.

So the issue seems to be that the frontend folder is located at src/frontend but the path that pre-commit uses is just src.

I tried cd frontend before the Docker command with no luck. Any ideas?

Thanks in advance!


Solution

  • you're correct that you need to cd, but you'll need to do that inside of docker

    for example:

          - id: prettier-docker
            name: prettier
            language: docker_image
            entry: frontend:latest bash -c 'cd frontend && npm run format'
            files: frontend/
    

    if I recall correctly you'll want pass_filenames: false as well