astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.81k stars 467 forks source link

Adding dependencies with `--git <url>` fails #158

Closed whi-tw closed 1 year ago

whi-tw commented 1 year ago

Steps to Reproduce

  1. rye init
  2. rye add --git https://github.com/MrSage/django_discord.py.git --branch main django_discord_py

Expected Result

adds django_discord_py @ git+https://github.com/MrSage/django_discord.py.git@main to pyproject.toml project.dependencies

Actual Result

Error: failed to resolve package django_discord_py @ git+https://github.com/MrSage/django_discord.py.git@main
Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "<string>", line 20, in <listcomp>
  File "<string>", line 18, in <genexpr>
  File "/Users/tom.whitwell/.rye/self/lib/python3.10/site-packages/packaging/version.py", line 196, in __init__
    match = self._regex.search(version)
TypeError: expected string or bytes-like object

Version Info

rye 0.1.0
commit: unknown (10b0875f3 2023-05-12)
platform: macos (aarch64)
self-python: cpython@3.10

Stacktrace

Stack backtrace:
   0: anyhow::backtrace::capture::Backtrace::capture
   1: anyhow::error::<impl anyhow::Error>::msg
   2: rye::cli::add::find_best_matches
   3: rye::cli::add::execute
   4: rye::cli::execute
   5: rye::main
   6: std::sys_common::backtrace::__rust_begin_short_backtrace
   7: std::rt::lang_start::{{closure}}
   8: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:287:13
      std::panicking::try::do_call
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
      std::panicking::try
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
      std::panic::catch_unwind
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
      std::rt::lang_start_internal::{{closure}}
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/rt.rs:148:48
      std::panicking::try::do_call
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:487:40
      std::panicking::try
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:451:19
      std::panic::catch_unwind
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panic.rs:140:14
      std::rt::lang_start_internal
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/rt.rs:148:20
   9: _main
whi-tw commented 1 year ago

After manually adding the dependency to pyproject.toml:

[project]
dependencies = [
  ...
  "django_discord_py @ git+https://github.com/MrSage/django_discord.py.git@main",
]

rye sync fails. Adding:

[tool.hatch.metadata]
allow-direct-references = true

resolves the problem (https://hatch.pypa.io/latest/config/metadata/#allowing-direct-references)

ischaojie commented 1 year ago

The main reason for the error is that the PackageFinder method in unearth returns version None for direct references, which causes the final failure. I'm not sure if mitsuhiko has any other ideas, but I can try to fix this bug if it needsthis.

mitsuhiko commented 1 year ago

@ischaojie would be super happy to get a PR for this. Probably will take a bit until I get to it otherwise.

whi-tw commented 1 year ago

Thank you @ischaojie!

yozachar commented 1 year ago

This issue still seems to persist.

$ rye --version
rye 0.8.0
commit: 0.8.0 (a6d6c7935 2023-06-18)
platform: windows (x86_64)
self-python: cpython@3.10
symlink support: false
$ rye add validators --git https://github.com/python-validators/validators --tag latest
$ rye sync
Reusing already existing virtualenv
Generating production lockfile: $HOME/Documents/DevSpace/Projects/linkeeper/requirements.lock
    error: subprocess-exited-with-error

    × Preparing metadata (pyproject.toml) did not run successfully.
    │ exit code: 1
    ╰─> [24 lines of output]
        Traceback (most recent call last):
          File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
            main()
          File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
            json_out['return_val'] = hook(**hook_input['kwargs'])
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 152, in prepare_metadata_for_build_wheel
            whl_basename = backend.build_wheel(metadata_directory, config_settings)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/build.py", line 56, in build_wheel
            return os.path.basename(next(builder.build(wheel_directory, ['standard'])))
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/builders/plugin/interface.py", line 93, in build
            self.metadata.validate_fields()
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/metadata/core.py", line 244, in validate_fields
            self.core.validate_fields()
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/metadata/core.py", line 1325, in validate_fields
            getattr(self, attribute)
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/metadata/core.py", line 1200, in dependencies
            self._dependencies = list(self.dependencies_complex)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^
          File "$HOME/AppData/Local/Temp/pip-build-env-2sgjz9ry/overlay/Lib/site-packages/hatchling/metadata/core.py", line 1186, in dependencies_complex
            raise ValueError(message)
        ValueError: Dependency #4 of field `project.dependencies` cannot be a direct reference unless field `tool.hatch.metadata.allow-direct-references` is set to `true`
        [end of output]

    note: This error originates from a subprocess, and is likely not a problem with pip.
Traceback (most recent call last):
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/operations/build/metadata.py", line 
35, in generate_metadata
    distinfo_dir = backend.prepare_metadata_for_build_wheel(metadata_dir)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/utils/misc.py", line 713, in prepare_metadata_for_build_wheel
    return super().prepare_metadata_for_build_wheel(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_vendor/pyproject_hooks/_impl.py", line 186, in prepare_metadata_for_build_wheel
    return self._call_hook('prepare_metadata_for_build_wheel', {
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_vendor/pyproject_hooks/_impl.py", line 311, in _call_hook
    self._subprocess_runner(
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/utils/subprocess.py", line 252, in runner
    call_subprocess(
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/utils/subprocess.py", line 224, in call_subprocess
    raise error
pip._internal.exceptions.InstallationSubprocessError: Preparing metadata (pyproject.toml) exited with 1

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "$HOME/.rye/pip-tools/cpython@3.11/Scripts/pip-compile.exe/__main__.py", line 7, in <module>
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/click/decorators.py", line 26, in new_func        
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/piptools/scripts/compile.py", line 592, in cli    
    results = resolver.resolve(max_rounds=max_rounds)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/piptools/resolver.py", line 593, in resolve       
    is_resolved = self._do_resolve(
                  ^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/piptools/resolver.py", line 625, in _do_resolve   
    resolver.resolve(
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/resolver.py", 
line 73, in resolve
    collected = self.factory.collect_root_requirements(root_reqs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 491, in collect_root_requirements
    req = self._make_requirement_from_install_req(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 453, in _make_requirement_from_install_req
    cand = self._make_candidate_from_link(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 185, in _make_candidate_from_link
    self._editable_candidate_cache[link] = EditableCandidate(
                                           ^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 318, in __init__
    super().__init__(
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 156, in __init__
    self.dist = self._prepare()
                ^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 225, in _prepare
    dist = self._prepare_distribution()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 328, in _prepare_distribution
    return self._factory.preparer.prepare_editable_requirement(self._ireq)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/operations/prepare.py", line 687, in prepare_editable_requirement
    dist = _get_prepared_distribution(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/operations/prepare.py", line 69, in 
_get_prepared_distribution
    abstract_dist.prepare_distribution_metadata(
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/distributions/sdist.py", line 61, in prepare_distribution_metadata
    self.req.prepare_metadata()
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/req/req_install.py", line 555, in prepare_metadata
    self.metadata_directory = generate_metadata(
                              ^^^^^^^^^^^^^^^^^^
  File "$HOME/.rye/pip-tools/cpython@3.11/Lib/site-packages/pip/_internal/operations/build/metadata.py", line 
37, in generate_metadata
    raise MetadataGenerationFailed(package_details=details) from error
pip._internal.exceptions.MetadataGenerationFailed: metadata generation failed
Error: could not write production lockfile for project

Caused by:
    failed to generate lockfile

Current workaround (should it be documented?) is to add the following in pyproject.toml:

[tool.hatch.metadata]
allow-direct-references = true