Skip to content

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

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

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

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.

This branch has not been deployed

No deployments
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

1 participant