See Ash Code logo See Ash Code

The script worked. The installed pre-commit hook did not.

Ashlynn Antrobus
• • 2 min read

The comment-removal function could work from its own checkout and still fail as somebody else’s pre-commit hook.

My original hook entry was:

entry: python remove_comments.py

That command depends on finding remove_comments.py relative to the current working directory. When pre-commit runs against a consumer repository, that directory belongs to the consumer. It isn’t necessarily the checkout where my script lives.

The v0.2.0 change gives the installed package a console entry point instead.

A dragon carries a toolbox from one workshop toward another dragon in a neighboring workshop.

Illustration: bringing a tool to a different workspace.

Let installation create the command

The package declares:

[project.scripts]
remove-comments = "remove_comments:main"

The hook then uses:

entry: remove-comments
language: python

The environment containing the installed package supplies the command. Calling it imports the module and invokes main; the consumer doesn’t need a copy of my script in its root directory.

This changes how the program is located. It doesn’t change the Python token rules that decide which comments to remove.

A consumer check has to start elsewhere

A small temporary Git repository can use this pinned configuration:

repos:
  - repo: https://github.com/ashrobertsdragon/remove-comments-pre-commit
    rev: 038411f128537f6e37a426459238e827eb23f6de
    hooks:
      - id: remove-comments

The pinned revision matters. A check against a moving branch could silently exercise a different package from the release I’m discussing.

Give the consumer a file named sample.py:

value = "# keep"  # remove this

Then ask pre-commit to run the installed hook on that file:

pre-commit run remove-comments --files sample.py

The resulting file is:

value = "# keep"

This example crosses the packaging boundary as well as the comment-removal boundary. The hook’s command has to exist in its environment, accept a path from another repository, and preserve the hash inside the string.

A changed file makes the hook fail on purpose

On the first run, pre-commit reports that the hook failed because it modified the file. The script returns exit code one when it makes a change. That gives me a chance to review and stage the transformed source.

Running the same hook again on the cleaned file should pass without another change. That distinguishes a successful automatic edit from a tool that keeps rewriting the same input.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.