Skip to content

Hook with pass_filenames: false doesn't trigger when affected files are deleted #3502

@mxr

Description

@mxr

search you tried in the issue tracker

deleted

describe your issue

Seems to be related to the explanation in #749 (comment)

Consider the attached config and this (toy) hook that counts all the bytes across all Python files

#!/usr/bin/env python3
from __future__ import annotations

import glob
import json
import os


def main() -> int:
    try:
        with open("bytes.json") as f:
            cur_count = json.load(f)['count']
    except FileNotFoundError:
        cur_count = -1

    new_count = sum(os.path.getsize(filename) for filename in glob.glob("*.py"))

    if cur_count != new_count:
        with open("bytes.json", "w+") as f:
            f.write(json.dumps({"count": new_count}))
            f.write("\n")
        return 1

    return 0


if __name__ == "__main__":
    raise SystemExit(main())

When I update any Python file, the hook runs and the total count is updated. But when I delete a Python file, I can see in the output that the hook is Skipped.

I understand that I can run the hook with always_run: true and CI will autofix the code since I do run pre-commit run --all-files in CI. These feel like clunky workarounds.

I'm not passing individual files to the hook, since the intention of the hook is to operate on the collective set of all Python files. It's odd/asymmetric behavior that the hook runs when the collective set/contents of files changes under modification/addition but not deletion.

Because of that would it be possible for pre-commit to operate consistently in both directions, and still run hooks configured as pass_filenames: false hooks when affected files are deleted?

pre-commit --version

pre-commit 4.2.0

.pre-commit-config.yaml

repos:
- repo: local
  hooks:
  - id: total-byte-counter
    name: total byte counter
    entry: bin/total_byte_counter.py
    language: python
    files: .+py$
    pass_filenames: false

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy