Skip to content

fix(hook): catch an option and its value passed as one argument - #165

Merged
thekaveman merged 1 commit into
compilerla:mainfrom
munitkala:fix/option-value-in-one-arg-rebased
Nov 18, 2026
Merged

thekaveman merged 1 commit into
compilerla:mainfrom
munitkala:fix/option-value-in-one-arg-rebased

Conversation

@munitkala

Copy link
Copy Markdown
Contributor

Fixes #129

Replaces #161, rebased on the latest main (with #163) as asked there.

Problem

pre-commit passes each item in args to the hook as one argument. With this config:

- id: conventional-pre-commit
  stages: [commit-msg]
  args: [--force-scope, --scopes api]

the hook gets the single string "--scopes api". argparse can't match it to an option, so it treats it as a commit type. --scopes is then never set, so the scope rule is silently skipped and a commit that should fail passes:

$ conventional-pre-commit --force-scope "--scopes api" .git/COMMIT_EDITMSG   # message: "fix(foobar): test commit"
$ echo $?
0

The same thing happens for any option given with its value in one item (--scopes api, --scopes a,b, etc.). Passing the CLI command through entry works, which is why the issue saw different results for the two configs.

Change

  • conventional_pre_commit/hook.py: after parsing, if any parsed type starts with -, the hook fails with a message explaining how to pass the option:
    conventional-pre-commit: error: "--scopes api" looks like an option and its value in one argument. Pass them separately, e.g. [--scopes, api] or [--scopes=api].
    
    No valid commit type starts with -, so this can't reject a config that works today.
  • README.md: a note under the pre-commit usage section that an option and its value go in separate args items ([--scopes, api]) or are joined with = ([--scopes=api]).

Tests

Two tests added to tests/test_hook.py:

  • test_main_fail__option_and_value_in_one_arg: --scopes api as one argument fails and prints the message.
  • test_main_success__option_and_value_with_equals: --scopes=scope still works.

Checks run locally:

  • tests/run.sh (pytest with coverage) on Python 3.14: 165 passed, 97% coverage
  • ran the hook through pre-commit with args: [--scopes api]: it now fails with the message above (on main the same config passes the commit)
  • black --check and flake8: clean
  • pre-commit run --all-files (black, flake8, bandit and the rest of this repo's hooks): all pass
  • git diff --check, LF line endings, and the commit message passes this repo's own conventional-pre-commit hook

pre-commit passes each item in args as a single argument, so
args: [--scopes api] reaches the hook as the one string "--scopes api".
argparse can't match it to an option, so it took it as a commit type. The
standard types were then added back and no scope rule was applied, so
commits that should fail passed.

The hook now fails with a message saying how to pass the option, and the
README notes that each option and value go in separate items.
@thekaveman
thekaveman merged commit 110d6bd into compilerla:main Nov 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in Argument Parsing for conventional-pre-commit Hook Configuration

2 participants