Skip to content

Commit

Permalink
fix: non-deterministic order when running pdm export
Browse files Browse the repository at this point in the history
Fix #1786

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Mar 28, 2023
1 parent 872521e commit d60bca2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions news/1786.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the random failure of `pdm export` due to non-deterministic order of group iteration.
16 changes: 9 additions & 7 deletions src/pdm/cli/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def all(self) -> list[str] | None:
return list(self)

@cached_property
def _translated_groups(self) -> set[str]:
def _translated_groups(self) -> list[str]:
"""Translate default, dev and groups containing ":all" into a list of groups"""
if self.is_unset:
# Default case, return what is in the lock file
locked_groups = self.project.lockfile.groups
if locked_groups:
return set(locked_groups)
return locked_groups
default, dev, groups = self.default, self.dev, self.groups
if dev is None: # --prod is not set, include dev-dependencies
dev = True
Expand All @@ -79,10 +79,7 @@ def _translated_groups(self) -> set[str]:
if ":all" in groups:
groups_set.discard(":all")
groups_set.update(optional_groups)
if default:
groups_set.add("default")
# Sorts the result in ascending order instead of in random order
# to make this function pure

invalid_groups = groups_set - set(project.iter_groups())
if invalid_groups:
project.core.ui.echo(
Expand All @@ -93,7 +90,12 @@ def _translated_groups(self) -> set[str]:
extra_groups = project.lockfile.compare_groups(groups_set)
if extra_groups:
raise PdmUsageError(f"Requested groups not in lockfile: {','.join(extra_groups)}")
return groups_set
# Sorts the result in ascending order instead of in random order
# to make this function pure
result = sorted(groups_set)
if default:
result.insert(0, "default")
return result

def __iter__(self) -> Iterator[str]:
return iter(self._translated_groups)
Expand Down

0 comments on commit d60bca2

Please sign in to comment.