Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom committer plugin not found when using cz check via git hook #453

Closed
nadavsinai-philips opened this issue Nov 11, 2021 · 8 comments
Closed

Comments

@nadavsinai-philips
Copy link

Description

I wrote a custom plugin to enforce our desired commit message template, it works fine when I invoke cz check but when I call cz check via the git commit hook it fails

The committer has not been found in the system.

Try running 'pip install cz_algotec'

my githook is written in python and invoked via shebang, I tried adding an install via pip to the custom plugin package (locally avilable0 but it doesn't help. I verified the package is found when using pip list and also it works when I invoke cz check from the command line
I don't know what's the difference from the hook invocation (calling sub_process.run - with shell:True)

Steps to reproduce

  1. write a custom plugin, use it's name in the ..cz.toml file
  2. call cz check to invoke commitizen from the commit-msg script

Current behavior

fail to run the custom logic

Desired behavior

custom logic runs,

Environment

cz version 2.20.0
Python Version: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
Operating System: Windows

@nadavsinai-philips
Copy link
Author

I have this clue (perhaps)
I tried to extend the conventional commit plugin, when I use the baseclass - it works
what happens when I run python shell and import my plugin (cz_algotec) is this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\Data\Repos\PACS\scm\git\validation-plugin\cz_algotec.py", line 3, in <module>
    from commitizen.cz.conventional_commits import ConventionalCommitsCz
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\commitizen\__init__.py", line 6, in <module>
    from commitizen.cz.base import BaseCommitizen
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\commitizen\cz\__init__.py", line 17, in <module>
    for finder, name, ispkg in pkgutil.iter_modules()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\commitizen\cz\__init__.py", line 18, in <dictcomp>
    if name.startswith("cz_")
AttributeError: module 'cz_algotec' has no attribute 'discover_this'

so basically when my plugin imports the base class it ends up importing the default inits of commitizen which end up requiering the plugin and since it wasn't loaded yet - fail, basicaly it's a circular dependency
I can copy/paste your code from ConventionalCommitsCz but that would be a bummer

p

@nadavsinai-philips
Copy link
Author

I think I found the cause, it related to this ros/rosdistro#18116 - the use of _ (underscopes) in pip package names, when I use pip list it shows me I have cz-algotec (with hypen) and that's why it can't find it ???? still reseaching, some help would be appriciated, has anyone managed to use custom rules at all??

@nadavsinai-philips
Copy link
Author

no, not related to the above, I see that in your code you use iter_modules() via pkgutil - it work with the underscope name, that bring me back to the cyclic dependency possibility

@Lee-W
Copy link
Member

Lee-W commented Nov 15, 2021

Hi @nadavsinai-philips , how do you install commitizen itself? is it in the same env as your local plugin?

@nadavsinai-philips
Copy link
Author

nadavsinai-philips commented Nov 15, 2021 via email

@KyleKing
Copy link
Contributor

KyleKing commented Jan 16, 2022

I think the issue is that pypi treats underscores and dashes interchangeably:

i.e. pip install cz-legacy and pip install cz_legacy both install the same package, but commitizen won't recognize the former

With an EAFP approach proposed in #474, the name comparison could check either cz_ or cz-

In the meantime, this issue could be resolved by making sure to always use underscores when pip-installing cz plugins

Update: I spoke too soon. I re-read the above comments and the reported error isn't an issue at all with dashes or underscores (#453 (comment))

@KyleKing
Copy link
Contributor

(FYI I corrected my previous post. You're likely right about the circular import issue. I had a similar problem reported in #474)

In case this is helpful, I use my custom plugin cz_legacy in pre-commit as an extra dependency:

  - repo: https://github.com/commitizen-tools/commitizen
    rev: v2.20.3
    hooks:
      - id: commitizen
        # yamllint disable-line rule:line-length
        additional_dependencies:
          [
            "git+https://github.com/KyleKing/cz_legacy.git@bfff2ab4134c26a31f1e7f9c3a8d4bedba824b08",
          ]
        stages: [commit-msg]

https://github.com/KyleKing/calcipy/blob/128a8837467172aa518155e1ae4bd2e0d68af7b2/.pre-commit-config.yaml#L39-L48

@nadavsinai-philips
Copy link
Author

(FYI I corrected my previous post. You're likely right about the circular import issue. I had a similar problem reported in #474)

In case this is helpful, I use my custom plugin cz_legacy in pre-commit as an extra dependency:

  - repo: https://github.com/commitizen-tools/commitizen
    rev: v2.20.3
    hooks:
      - id: commitizen
        # yamllint disable-line rule:line-length
        additional_dependencies:
          [
            "git+https://github.com/KyleKing/cz_legacy.git@bfff2ab4134c26a31f1e7f9c3a8d4bedba824b08",
          ]
        stages: [commit-msg]

KyleKing/calcipy@128a883/.pre-commit-config.yaml#L39-L48

thanks @KyleKing , I ditched commitizen for now and moved to use commitlint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants