pre-commit icon indicating copy to clipboard operation
pre-commit copied to clipboard

bug: pre-commit's script is installed in migration mode

Open domsj-foodpairing opened this issue 1 year ago • 3 comments

search you tried in the issue tracker

migration

describe your issue

I'm using pre-commit inside a devcontainer. I use visual studio and the built-in vcs + gitlens extension Every now and then commit fails and I get the following message

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
bug: pre-commit's script is installed in migration mode
run `pre-commit install -f --hook-type pre-commit` to fix this

Please report this bug at https://github.com/pre-commit/pre-commit/issues
ruff.....................................................................Passed
black....................................................................Passed
mypy type checks.........................................................Passed
bandit...................................................................Passed
sqlfluff-dashboards-linter...........................(no files to check)Skipped
eslint...............................................(no files to check)Skipped
prettier.............................................(no files to check)Skipped
check for added large files..............................................Passed
fix end of files.........................................................Passed
don't commit to branch...................................................Passed

I then run pre-commit install -f --hook-type pre-commit after which things work again for some time

pre-commit --version

3.7.1

.pre-commit-config.yaml

fail_fast: true
repos:
    - repo: local
      hooks:
          - id: ruff
            name: ruff
            language: system
            entry: poetry run ruff check . --fix
            types: ["python"]

          - id: black
            name: black
            language: system
            entry: poetry run black .
            types: ["python"]

          - id: mypy
            name: mypy type checks
            language: system
            entry: poetry run mypy
            types: ["python"]

          - id: bandit
            name: bandit
            language: system
            entry: poetry run bandit -c pyproject.toml -r .
            types: ["python"]

          - id: sqlfluff-dashboards-linter
            name: sqlfluff-dashboards-linter
            language: system
            entry: poetry run python -m scripts.pre_commit_configs.sqlfluff_dashboards_linter
            files: ^dashboards\/.*\/.*\.sql$

# when DEV container is used for development, uncomment this and use forward slashes in .unimportedrc.json
#          - id: unimported
#            name: unimported
#            language: system
#            entry: bash -c 'cd frontends/cfi && npm run unimported'
#            pass_filenames: false
#            types: [file]
#            files: ^frontends\/cfi\/src\/.*\.[jt]sx?$
#            args: [--error]


    - repo: https://github.com/pre-commit/mirrors-eslint
      rev: "v8.48.0"
      hooks:
          - id: eslint
            args:
                [
                    --config,
                    frontends/cfi/.eslintrc.js,
                    --ignore-path,
                    frontends/cfi/.eslintignore,
                    --fix
                ]
            files: ^frontends\/cfi\/src\/.*\.[jt]sx?$
            types: [file]
            additional_dependencies:
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - [email protected]
                - "@typescript-eslint/[email protected]"
                - "@typescript-eslint/[email protected]"
                - [email protected]

    - repo: https://github.com/pre-commit/mirrors-prettier
      rev: "v3.0.3"
      hooks:
          - id: prettier
            args:
                [
                    --config,
                    frontends/cfi/.prettierrc.js,
                    --ignore-path,
                    frontends/cfi/.prettierignore,
                    --write,
                ]
            files: ^frontends\/cfi\/src\/.*\.(css|less|scss|graphql|gql|md|tsx|ts|js|jsx)$

    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.5.0
      hooks:
          - id: check-added-large-files
            args: ['--maxkb=200000']
          - id: end-of-file-fixer
          - id: no-commit-to-branch
            args: [--branch, master, --branch, develop]

~/.cache/pre-commit/pre-commit.log (if present)

No response

domsj-foodpairing avatar Aug 21 '24 14:08 domsj-foodpairing

are you maybe using a very very old version of pre-commit and running pre-commit install?

the error message indicates that .git/hooks/pre-commit got copied to .git/hooks/pre-commit.legacy somehow

pre-commit itself wouldn't do that so I'm a little suspicious of your dev container setup

asottile avatar Aug 21 '24 14:08 asottile

I did in fact install pre-commit using pre-commit install The version, as mentioned, is 3.7.1. Initial install may have been done with an older version.

Currently it lists

$ ls .git/hooks/pre-commit*
.git/hooks/pre-commit  .git/hooks/pre-commit.sample

(but of course in the meantime I did run pre-commit install -f --hook-type pre-commit)

domsj-foodpairing avatar Aug 21 '24 15:08 domsj-foodpairing

if it happens again -- can you check the contents of .git/hooks? additionally -- what are the contents in your vanilla dev container? do you have that image?

asottile avatar Aug 21 '24 15:08 asottile

$ ls -hl .git/hooks/
total 72K
-rwxr-xr-x 1 vscode vscode  478 Jul 10 13:50 applypatch-msg.sample
-rwxr-xr-x 1 vscode vscode  896 Jul 10 13:50 commit-msg.sample
-rwxr-xr-x 1 vscode vscode 4.7K Jul 10 13:50 fsmonitor-watchman.sample
-rwxr-xr-x 1 vscode vscode  189 Jul 10 13:50 post-update.sample
-rwxr-xr-x 1 vscode vscode  424 Jul 10 13:50 pre-applypatch.sample
-rwxr-xr-x 1 vscode vscode  626 Aug 29 18:26 pre-commit
-rwxr-xr-x 1 vscode vscode  626 Aug 29 18:26 pre-commit.legacy
-rwxr-xr-x 1 vscode vscode 1.7K Jul 10 13:50 pre-commit.sample
-rwxr-xr-x 1 vscode vscode  416 Jul 10 13:50 pre-merge-commit.sample
-rwxr-xr-x 1 vscode vscode 1.5K Jul 10 13:50 prepare-commit-msg.sample
-rwxr-xr-x 1 vscode vscode 1.4K Jul 10 13:50 pre-push.sample
-rwxr-xr-x 1 vscode vscode 4.8K Jul 10 13:50 pre-rebase.sample
-rwxr-xr-x 1 vscode vscode  544 Jul 10 13:50 pre-receive.sample
-rwxr-xr-x 1 vscode vscode 2.8K Jul 10 13:50 push-to-checkout.sample
-rwxr-xr-x 1 vscode vscode 2.3K Jul 10 13:50 sendemail-validate.sample
-rwxr-xr-x 1 vscode vscode 3.6K Jul 10 13:50 update.sample
$ cat .git/hooks/pre-commit
#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03

# start templated
INSTALL_PYTHON=/workspaces/engineering-cfi/.venv/bin/python
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated

HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")

if [ -x "$INSTALL_PYTHON" ]; then
    exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
    exec pre-commit "${ARGS[@]}"
else
    echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
    exit 1
fi
$ cat .git/hooks/pre-commit.legacy 
#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03

# start templated
INSTALL_PYTHON=/workspaces/engineering-cfi/.venv/bin/python
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated

HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")

if [ -x "$INSTALL_PYTHON" ]; then
    exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
    exec pre-commit "${ARGS[@]}"
else
    echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
    exit 1
fi
$ cat .git/hooks/pre-commit.sample 
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
        against=HEAD
else
        # Initial commit: diff against an empty tree object
        against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
        # Note that the use of brackets around a tr range is ok here, (it's
        # even required, for portability to Solaris 10's /usr/bin/tr), since
        # the square bracket bytes happen to fall in the designated range.
        test $(git diff --cached --name-only --diff-filter=A -z $against |
          LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
        cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
        exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

fwiw meanwhile the pre-commit version is 3.8.0.

The devcontainer is based on mcr.microsoft.com/devcontainers/base:ubuntu-22.04 a minimized version of it would look like this (I removed some stuff related to installing gcloud, skaffold, nodejs, envoy, helm, ... that I think is certainly not relevant)

# syntax=docker/dockerfile:1.4
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04

RUN yes | unminimize

RUN sudo apt-get update -qq && \
    sudo apt-get -y install \
        tzdata build-essential curl wget vim man-db gh bash-completion \
        locales software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    sudo apt-get install -y python3.11 python3.11-dev python3.11-venv python3-pip

RUN apt-get install python-is-python3
RUN python3.11 -m pip install pipx

USER vscode


# adding poetry
RUN pipx install poetry==1.6.1 && \
    poetry completions bash > /home/vscode/.bash_completion

# add some convenience to ~/.bashrc
COPY --chown=vscode:vscode .bashrc-extras /home/vscode/.bashrc-extras
RUN echo ". ~/.bashrc-extras" >> /home/vscode/.bashrc

in the .bashrc-extras there is some code that calls

  if [ -z "${env_info}" ]; then
      echo "No poetry virtual envs found"
  else
    . ${env_info}/bin/activate
    ${env_info}/bin/pre-commit install
  fi

basically each time I open a terminal

domsj-foodpairing avatar Aug 30 '24 07:08 domsj-foodpairing

hmm do you happen to have the poetry lock that contains the pre-commit version (just so I can run exactly what you're running)

asottile avatar Aug 30 '24 16:08 asottile

sure

[[package]]
name = "pre-commit"
version = "3.8.0"
description = "A framework for managing and maintaining multi-language pre-commit hooks."
optional = false
python-versions = ">=3.9"
files = [
    {file = "pre_commit-3.8.0-py2.py3-none-any.whl", hash = "sha256:9a90a53bf82fdd8778d58085faf8d83df56e40dfe18f45b19446e26bf1b3a63f"},
    {file = "pre_commit-3.8.0.tar.gz", hash = "sha256:8bb6494d4a20423842e198980c9ecf9f96607a07ea29549e180eef9ae80fe7af"},
]

[package.dependencies]
cfgv = ">=2.0.0"
identify = ">=1.0.0"
nodeenv = ">=0.11.1"
pyyaml = ">=5.1"
virtualenv = ">=20.10.0"

and when I posted the issue earlier it was

[[package]]
name = "pre-commit"
version = "3.7.1"
description = "A framework for managing and maintaining multi-language pre-commit hooks."
optional = false
python-versions = ">=3.9"
files = [
    {file = "pre_commit-3.7.1-py2.py3-none-any.whl", hash = "sha256:fae36fd1d7ad7d6a5a1c0b0d5adb2ed1a3bda5a21bf6c3e5372073d7a11cd4c5"},
    {file = "pre_commit-3.7.1.tar.gz", hash = "sha256:8ca3ad567bc78a4972a3f1a477e94a79d4597e8140a6e0b651c5e33899c3654a"},
]

[package.dependencies]
cfgv = ">=2.0.0"
identify = ">=1.0.0"
nodeenv = ">=0.11.1"
pyyaml = ">=5.1"
virtualenv = ">=20.10.0"

domsj-foodpairing avatar Sep 02 '24 07:09 domsj-foodpairing

coming back to this now -- I'm able to build the docker image and if I add a git repo and pre-commit install in it then it seems to work fine -- I don't get the doubled legacy file 🤔

asottile avatar Sep 14 '24 23:09 asottile

yea most of the time things work as they should. haven't encountered it anymore since my last comment about it 🤷‍♂️

domsj-foodpairing avatar Sep 16 '24 07:09 domsj-foodpairing

weird -- well if you can come up with a reproducible way to get into the state please comment back!

asottile avatar Sep 16 '24 12:09 asottile