← All tasks
pythonpypa/hatch #1381Not a task: already works

A direct reference with context formatting causes an error violating URL validation of packaging

envgap__pypa__hatch-1381

01 / FAILURE SIGNATURE

As reported upstream

With this setting, when the installed `packaging` version is `<23.2`, `hatch build -t foo` causes the following error.
Not a benchmark task.
  • The project already builds and runs before the fix, so there is nothing to repair.

02 / ENVIRONMENT RECIPE

Base commit
55101204ced1b6a17051b0bc21b133cdd24989cd
Manifest
pyproject.toml
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / ORIGINAL ISSUE TEXT

pypa/hatch #1381 · read the original issue
## Description

I created a builder plugin to introduce a build target `foo`.

The builder plugin is in a subdir of the repo so its dependency is defined as below using a direct reference.

```toml

[tool.hatch.build.targets.foo]

path = "src/hatch-foo-builder"

dependencies = [

  "hatch-foo-builder @ {root:uri}/../src/hatch_foo_builder"

]

```



With this setting, when the installed `packaging` version is `<23.2`, `hatch build -t foo` causes the following error.

> InvalidRequirement: Invalid URL: {root:uri}/../src/hatch_foo_builder



<details>

<summary>Error log</summary>



```

❯ python -m hatch build -t foo

─────────────────────────────────────────────────────────────────────────────────────────────────────── foo ────────────────────────────────────────────────────────────────────────────────────────────────────────

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/hatch/cli/__init__.py:221 in main                                            │

│                                                                                                  │

│   218                                                                                            │

│   219 def main():  # no cov                                                                      │

│   220 │   try:                                                                                   │

│ ❱ 221 │   │   return hatch(prog_name='hatch', windows_expand_args=False)                         │

│   222 │   except Exception:  # noqa: BLE001                                                      │

│   223 │   │   from rich.console import Console                                                   │

│   224                                                                                            │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/core.py:1157 in __call__                                               │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/core.py:1078 in main                                                   │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/core.py:1688 in invoke                                                 │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/core.py:1434 in invoke                                                 │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/core.py:783 in invoke                                                  │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/click/decorators.py:45 in new_func                                           │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/hatch/cli/build/__init__.py:116 in build                                     │

│                                                                                                  │

│   113 │   │   │   with environment.get_env_vars(), EnvVars(env_vars):                            │

│   114 │   │   │   │   dependencies.extend(builder.config.dependencies)                           │

│   115 │   │   │                                                                                  │

│ ❱ 116 │   │   │   with app.status_if(                                                            │

│   117 │   │   │   │   'Setting up build environment', condition=not environment.build_environm   │

│   118 │   │   │   ) as status, environment.build_environment(dependencies) as build_environmen   │

│   119 │   │   │   │   status.stop()                                                              │

│                                                                                                  │

│ /root/xxxxxxxx/.anyenv/envs/pyenv/versions/3.10.6/lib/python3.10/contextlib.py:135 in __enter__  │

│                                                                                                  │

│   132 │   │   # they are only needed for recreation, which is not possible anymore               │

│   133 │   │   del self.args, self.kwds, self.func                                                │

│   134 │   │   try:                                                                               │

│ ❱ 135 │   │   │   return next(self.gen)                                                          │

│   136 │   │   except StopIteration:                                                              │

│   137 │   │   │   raise RuntimeError("generator didn't yield") from None                         │

│   138                                                                                            │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/hatch/env/virtual.py:154 in build_environment                                │

│                                                                                                  │

│   151 │   │                                                                                      │

│   152 │   │   with self.get_env_vars(), self.build_virtual_env:                                  │

│   153 │   │   │   if not dependencies_in_sync(                                                   │

│ ❱ 154 │   │   │   │   [Requirement(d) for d in dependencies],                                    │

│   155 │   │   │   │   sys_path=self.build_virtual_env.sys_path,                                  │

│   156 │   │   │   │   environment=self.build_virtual_env.environment,                            │

│   157 │   │   │   ):                                                                             │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/hatch/env/virtual.py:154 in <listcomp>                                       │

│                                                                                                  │

│   151 │   │                                                                                      │

│   152 │   │   with self.get_env_vars(), self.build_virtual_env:                                  │

│   153 │   │   │   if not dependencies_in_sync(                                                   │

│ ❱ 154 │   │   │   │   [Requirement(d) for d in dependencies],                                    │

│   155 │   │   │   │   sys_path=self.build_virtual_env.sys_path,                                  │

│   156 │   │   │   │   environment=self.build_virtual_env.environment,                            │

│   157 │   │   │   ):                                                                             │

│                                                                                                  │

│ /workspace/xxxxxxxxxxxxxxxxxx/path/to/hatch-requirements-context-formatting-test/.venv/lib/pytho │

│ n3.10/site-packages/packaging/requirements.py:48 in __init__                                     │

│                                                                                                  │

│   45 │   │   │   elif not (parsed_url.scheme and parsed_url.netloc) or (                         │

│   46 │   │   │   │   not parsed_url.scheme and not parsed_url.netloc                             │

│   47 │   │   │   ):                                                                              │

│ ❱ 48 │   │   │   │   raise InvalidRequirement(f"Invalid URL: {parsed.url}")                      │

│   49 │   │   │   self.url: Optional[str] = parsed.url                                            │

│   50 │   │   else:                                                                               │

│   51 │   │   │   self.url = None                                                                 │

╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

InvalidRequirement: Invalid URL: {root:uri}/../src/hatch_foo_builder

```



(Paths are masked)



</details>



## Reproduce the error

Clone https://github.com/whitphx/hatch-requirements-context-formatting-test

```sh

python -m venv .venv

. .venv/bin/activate

python -m pip install hatch packaging==22.0

python -m hatch build -t foo

```



Looks like the URL validation of `packaging` which was removed in 23.2 is the cause.

> Remove URL validation from requirement parsing

> https://github.com/pypa/packaging/releases/tag/23.2



If you install `packaging>=23.2`, this problem doesn't happen.
Continue on GitHub ↗

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]