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.54k stars 266 forks source link

`cz bump` fails when running with `rye` #1118

Open my1e5 opened 6 months ago

my1e5 commented 6 months ago

Description

I'm using rye to manage my python project - it's similar to poetry. Running cz bump through rye gives

$ rye run cz bump
fatal: not a git repository (or any of the parent directories): .git

Steps to reproduce

Similar to poetry, we add commitizen as a dev dependency.

$ rye add commitizen --dev

Then

$ rye run cz init
Welcome to commitizen!

Answer the questions to configure your project.
For further configuration visit:

https://commitizen-tools.github.io/commitizen/config/

? Please choose a supported config file:  pyproject.toml
? Please choose a cz (commit rule): (default: cz_conventional_commits) cz_conventional_commits
? Choose the source of the version: pep621: Get and set version from pyproject.toml:project.version field
No Existing Tag. Set tag to v0.0.1
? Choose version scheme:  pep440
? Please enter the correct version format: (default: "$version")
? Create changelog automatically on bump Yes
? Keep major version zero (0.x) during breaking changes Yes
? What types of pre-commit hook you want to install? (Leave blank if you don't want to install) done

You can bump the version running:

        cz bump

Configuration complete 🚀

Current behavior

The way to run a script/executable you've added to your python project is using rye run

$ rye run cz bump
fatal: not a git repository (or any of the parent directories): .git

Is this because cz.exe is stored in .venv/Scripts/cz.exe?

So it can't find the .git folder which is two levels above?

Desired behavior

No fatal errors. cz is able to find the .git repo.

Screenshots

No response

Environment

$ rye run cz version
3.25.0
$ rye run python --version
Python 3.12.2
$ rye --version
rye 0.33.0
commit: 0.33.0 (58523f69f 2024-04-24)
platform: windows (x86_64)
self-python: cpython@3.12.2
symlink support: true
uv enabled: true
my1e5 commented 6 months ago

To add to this. If I install cz globally using pip.

$ pip install --user -U Commitizen

Then cz bump works in all my non-rye python projects. But in my rye project even running cz directly gives

$ cd my-rye-managed-project/
$ ls -a
.  ..  .git .gitignore  .pre-commit-config.yaml  .python-version  .ruff_cache  .venv  dist  pyproject.toml  README.md  requirements.lock  requirements-dev.lock  src
$ cz bump
fatal: not a git repository (or any of the parent directories): .git
my1e5 commented 6 months ago

Did some more digging

$ rye run cz --debug bump
fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\my1e5\Projects\my1e5\myproject\.venv\Scripts\cz.exe\__main__.py", line 8, in <module>
  File "C:\Users\my1e5\Projects\my1e5\myproject\.venv\Lib\site-packages\commitizen\cli.py", line 607, in main
    args.func(conf, arguments)()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my1e5\Projects\my1e5\myproject\.venv\Lib\site-packages\commitizen\commands\bump.py", line 41, in __init__
    raise NotAGitProjectError()
commitizen.exceptions.NotAGitProjectError: fatal: not a git repository (or any of the parent directories): .git

Looking specifically at the output of cmd.run

def is_git_project() -> bool:
    c = cmd.run("git rev-parse --is-inside-work-tree")
    print(c)
$ rye run cz --debug bump
Command(out='', err="'git' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n", stdout=b'', stderr=b"'git' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n", return_code=1)

But then even ls doesn't work. Changing it to c = cmd.run("ls")

$ rye run cz --debug bump
Command(out='', err="'ls' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n", stdout=b'', stderr=b"'ls' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n", return_code=1)

However, for some reason, in cmd.py if I change shell=False. Then it works.

def run(cmd: str, env=None) -> Command:
    if env is not None:
        env = {**os.environ, **env}
    process = subprocess.Popen(
        cmd,
        shell=False,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
        env=env,
    )
$ rye run cz --debug bump
Tag 0.1.0 could not be found. 
Possible causes:
- version in configuration is not the current version
- tag_format is missing, check them using 'git tag --list'

? Is this the first tag created? (Y/n)

Just as a reminder - in all my rye-managed projects running cz directly (i.e. not using rye run) gives the same error. But this can also be fixed using shell=False. I have no issues running cz directly in my other python projects not managed by rye.

woile commented 6 months ago

Would you mind submitting a PR? I think we can set shell=Falseif the system is windows. I haven't had that issue on mac