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

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

Closed
whitphx opened this issue Apr 12, 2024 · 2 comments · Fixed by #1384

Comments

@whitphx
Copy link
Contributor

whitphx commented Apr 12, 2024

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.

[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

Error log
❯ 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)

Reproduce the error

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

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.

@ofek
Copy link
Sponsor Collaborator

ofek commented Apr 12, 2024

Okay, I can bump that dependency, unless you want to open a PR instead.

@whitphx
Copy link
Contributor Author

whitphx commented Apr 13, 2024

Thank you! I created a PR: #1384.
Just wondered if some other works would be needed than bumping the dep version

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

Successfully merging a pull request may close this issue.

2 participants