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

Fix corner case in cached dependency resolution #362

Merged
merged 2 commits into from
Feb 22, 2017
Merged
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
31 changes: 19 additions & 12 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import print_function

import itertools
import os
import shutil
import time
Expand Down Expand Up @@ -238,20 +239,26 @@ def __init__(self, cache, cache_ttl, *args, **kw):

# Short-circuiting package iterator.
def package_iterator(self, resolvable, existing=None):
iterator = Iterator(fetchers=[Fetcher([self.__cache])])
packages = self.filter_packages_by_interpreter(
resolvable.compatible(iterator), self._interpreter, self._platform)

if packages:
if resolvable.exact:
return packages

if self.__cache_ttl:
packages = self.filter_packages_by_ttl(packages, self.__cache_ttl)
if packages:
packages = []
if self.__cache_ttl or resolvable.exact:
iterator = Iterator(fetchers=[Fetcher([self.__cache])])
packages = self.filter_packages_by_interpreter(
resolvable.compatible(iterator),
self._interpreter,
self._platform
)

if packages:
if resolvable.exact:
return packages

return super(CachingResolver, self).package_iterator(resolvable, existing=existing)
if self.__cache_ttl:
packages = self.filter_packages_by_ttl(packages, self.__cache_ttl)

return itertools.chain(
packages,
super(CachingResolver, self).package_iterator(resolvable, existing=existing)
)

# Caching sandwich.
def build(self, package, options):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from twitter.common.contextutil import temporary_dir

from pex.common import safe_copy
from pex.crawler import Crawler
from pex.fetcher import Fetcher
from pex.package import EggPackage, SourcePackage
from pex.resolvable import ResolvableRequirement
Expand Down Expand Up @@ -48,6 +49,29 @@ def test_diamond_local_resolve_cached():
assert len(dists) == 2


def test_cached_dependency_pinned_unpinned_resolution_multi_run():
# This exercises the issue described here: https://github.com/pantsbuild/pex/issues/178
project1_0_0 = make_sdist(name='project', version='1.0.0')
project1_1_0 = make_sdist(name='project', version='1.1.0')

with temporary_dir() as td:
for sdist in (project1_0_0, project1_1_0):
safe_copy(sdist, os.path.join(td, os.path.basename(sdist)))
fetchers = [Fetcher([td])]
with temporary_dir() as cd:
# First run, pinning 1.0.0 in the cache
dists = resolve(['project', 'project==1.0.0'], fetchers=fetchers, cache=cd, cache_ttl=1000)
assert len(dists) == 1
assert dists[0].version == '1.0.0'
# This simulates separate invocations of pex but allows us to keep the same tmp cache dir
Crawler.reset_cache()
# Second, run, the unbounded 'project' req will find the 1.0.0 in the cache. But should also
# return SourcePackages found in td
dists = resolve(['project', 'project==1.1.0'], fetchers=fetchers, cache=cd, cache_ttl=1000)
assert len(dists) == 1
assert dists[0].version == '1.1.0'


def test_resolve_prereleases():
stable_dep = make_sdist(name='dep', version='2.0.0')
prerelease_dep = make_sdist(name='dep', version='3.0.0rc3')
Expand Down