Skip to content

Commit

Permalink
fix: discard dependency-groups when resolving with uv
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Oct 31, 2024
1 parent d954e79 commit edfa314
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/pdm/builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def check_requirements(self, reqs: Iterable[str]) -> Iterable[Requirement]:
ws = WorkingSet(libs)
for req in reqs:
parsed_req = parse_requirement(req)
parsed_req.groups = ["default"]
if parsed_req.marker and not parsed_req.marker.matches(self._env.spec):
logger.debug(
"Skipping requirement %s: mismatching marker %s",
Expand Down
18 changes: 12 additions & 6 deletions src/pdm/formats/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def build_pyproject_toml(self) -> Path:
data = self.project.pyproject._data.unwrap()
data.setdefault("project", {})["requires-python"] = self.requires_python
data.setdefault("project", {})["dependencies"] = []
data.pop("dependency-groups", None)
data.pop("build-system", None)
data.setdefault("project", {}).pop("optional-dependencies", None)
sources = {}
collected_deps: dict[str, list[str]] = {}
Expand All @@ -47,7 +49,9 @@ def build_pyproject_toml(self) -> Path:
else:
entry = dep.as_line()
for group in dep.groups:
collected_deps.setdefault(group, []).append(entry)
collected = collected_deps.setdefault(group, [])
if entry not in collected:
collected.append(entry)

for group, deps in collected_deps.items():
if group == "default":
Expand Down Expand Up @@ -97,13 +101,15 @@ def build_uv_lock(self, include_self: bool = False) -> Path:
dependencies: list[dict[str, Any]] = []
optional_dependencies: dict[str, list[dict[str, Any]]] = {}
for req in self.requirements:
group = req.groups[0]
if (dep := self._make_dependency(None, req)) is None:
continue
if group == "default":
dependencies.append(dep)
else:
optional_dependencies.setdefault(group, []).append(dep)
for group in req.groups:
if group == "default":
target_group = dependencies
else:
target_group = optional_dependencies.setdefault(group, [])
if dep not in target_group:
target_group.append(dep)
if dependencies:
this_package["dependencies"] = dependencies # type: ignore[assignment]
if optional_dependencies:
Expand Down
6 changes: 5 additions & 1 deletion src/pdm/resolver/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def make_requirement(dep: dict[str, Any]) -> str:
return req.as_line()

for package in data["package"]:
if self.project.name and package["name"] == normalize_name(self.project.name) and not self.keep_self:
if (
self.project.name
and package["name"] == normalize_name(self.project.name)
and (not self.keep_self or package["source"].get("virtual"))
):
continue
req: Requirement
if url := package["source"].get("url"):
Expand Down

0 comments on commit edfa314

Please sign in to comment.