Skip to content

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

Closed
munitkala wants to merge 1 commit into
compilerla:mainfrom
munitkala:fix/option-value-in-one-arg
Closed

munitkala wants to merge 1 commit into
compilerla:mainfrom
munitkala:fix/option-value-in-one-arg

Conversation

@munitkala

@munitkala munitkala commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #129

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): 165 passed
  • black --check and flake8: clean
  • bandit -ll (1.9.4, as pinned in .pre-commit-config.yaml): no issues
  • 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.
@munitkala

Copy link
Copy Markdown
Contributor Author

Heads up, the test failures aren't from this change. ubuntu-latest moved to 26.04 and setup-python has no 3.8/3.9 there, so those jobs fail at "Set up Python" and the rest get cancelled. main fails the same way right now.

@thekaveman

Copy link
Copy Markdown
Member

@munitkala can I ask you to please rebase this on the latest main?

We merged #163 which bumps the Python version support and should fix these test run issues.

@munitkala

Copy link
Copy Markdown
Contributor Author

@thekaveman done, rebased on the latest main (with #163) and opened it as #165 so this one can be closed. Same change, tests pass on 3.14 locally.

@munitkala munitkala closed this Nov 18, 2026
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