commitizen-tools / commitizen

Create committing rules for projects :rocket: auto bump versions :arrow_up: and auto changelog generation :open_file_folder:
https://commitizen-tools.github.io/commitizen/
MIT License
2.47k stars 264 forks source link

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

Closed nadavsinai-philips closed 2 years ago

nadavsinai-philips commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

I think I found the cause, it related to this https://github.com/ros/rosdistro/issues/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 commented 2 years ago

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 commented 2 years ago

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

nadavsinai-philips commented 2 years ago

Yes , both installed via the same pip

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Wei Lee @.> Sent: Monday, November 15, 2021 2:39:27 AM To: commitizen-tools/commitizen @.> Cc: Sinai, Nadav @.>; Mention @.> Subject: Re: [commitizen-tools/commitizen] custom committer plugin not found when using cz check via git hook (Issue #453)

Caution: This e-mail originated from outside of Philips, be careful for phishing.

Hi @nadavsinai-philipshttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnadavsinai-philips&data=04%7C01%7C%7C310c4e532287437f543208d9a7d0622a%7C1a407a2d76754d178692b3ac285306e4%7C0%7C0%7C637725335702393229%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=GmqVwy63Q4qP9OtLKaCk0N1oqFJF2GhPrZf9RO8fgyY%3D&reserved=0 , how do you install commitizen itself? is it in the same env as your local plugin?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fcommitizen-tools%2Fcommitizen%2Fissues%2F453%23issuecomment-968405528&data=04%7C01%7C%7C310c4e532287437f543208d9a7d0622a%7C1a407a2d76754d178692b3ac285306e4%7C0%7C0%7C637725335702403225%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=q26MoE3rYeo6yzEi7dxJ28PNkaIM8HF%2FR2DFvnsu%2FqQ%3D&reserved=0, or unsubscribehttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAUZZSC25VJMH2TIQGL3W7WLUMBJD7ANCNFSM5H22XBTA&data=04%7C01%7C%7C310c4e532287437f543208d9a7d0622a%7C1a407a2d76754d178692b3ac285306e4%7C0%7C0%7C637725335702413219%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=%2B7AqX3sLWAPQxsU7s3xOXjpiGF6h%2FKxW0W7GfIEUSC0%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C310c4e532287437f543208d9a7d0622a%7C1a407a2d76754d178692b3ac285306e4%7C0%7C0%7C637725335702413219%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=WHu4WbB7y1K9p8IyxGLVDiJwUGza%2BYhJYSw2gIEMNLk%3D&reserved=0 or Androidhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C310c4e532287437f543208d9a7d0622a%7C1a407a2d76754d178692b3ac285306e4%7C0%7C0%7C637725335702423213%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=2mSGoaaCd2N54KXwvVgFvfj5QhAdpuvjutfvvsWnql0%3D&reserved=0.


The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.

KyleKing commented 2 years ago

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 (https://github.com/commitizen-tools/commitizen/issues/453#issuecomment-968257817)

KyleKing commented 2 years ago

(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 commented 2 years ago

(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