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

Don't search secondary repositories if not required #5984

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions docs/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ There can be more than one secondary package source.

{{% note %}}

All package sources (including secondary sources) will be searched during the package lookup
process. These network requests will occur for all sources, regardless of if the package is
found at one or more sources.
Secondary sources are searched only for packages that are not found in primary
sources.

If you wish to avoid this, you may explicitly specify which source to search in for a particular
package.
You may explicitly specify which source to search in for a particular package.

```bash
poetry add --source pypi httpx
Expand Down
13 changes: 11 additions & 2 deletions src/poetry/repositories/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,17 @@ def find_packages(self, dependency: Dependency) -> list[Package]:
if repository is not None and not self._ignore_repository_names:
return self.repository(repository).find_packages(dependency)

packages = []
for repo in self._repositories:
packages: list[Package] = []
for index, repo in enumerate(self._repositories):
if (
self._secondary_start_idx is not None
and index >= self._secondary_start_idx
and packages
):
# We've found packages in the primary repositories, don't search
# secondary repositories.
break

packages += repo.find_packages(dependency)

return packages
Expand Down
21 changes: 21 additions & 0 deletions tests/repositories/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import pytest

from poetry.core.packages.package import Package
from poetry.core.semver.version import Version

from poetry.factory import Factory
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.repositories.exceptions import PackageNotFound
Expand Down Expand Up @@ -141,3 +143,22 @@ def test_repository_ordering():
assert pool.repositories == [default1, primary1, primary3, secondary1, secondary3]
with pytest.raises(ValueError):
pool.add_repository(default2, default=True)


def test_secondary_repository_is_not_always_searched() -> None:
package1 = Package("foo", "1.0.0")
primary = Repository("primary", [package1])

package2 = Package("foo", "2.0.0")
secondary = Repository("secondary", [package2])

pool = Pool()
pool.add_repository(primary)
pool.add_repository(secondary, secondary=True)

# Only the package in the primary repository is found, even though there's a newer
# version in a secondary repository.
dependency = Factory.create_dependency("foo", "*")
packages = pool.find_packages(dependency)
assert len(packages) == 1
assert packages[0].pretty_version == "1.0.0"