Repository navigation
Description
Hello,
I manage a project using poetry and since importlib-metadata 6.5.1 my install has been failing with the following error. I'm trying to install an updated version of ansible while a current one is already installed. I think the issue is that pathlib is having trouble finding a relative path between the ansible binary /usr/local/bin/ansible and site-packages.
ansible [core 2.13.9]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.8.16 (default, Feb 11 2023, 02:53:15) [GCC 10.2.1 20210110]
jinja version = 3.1.2
libyaml = True
---
00:40:59 #12 42.71 ValueError
00:40:59 #12 42.71
00:40:59 #12 42.71 '/usr/local/bin/ansible' does not start with '/usr/local/lib/python3.8/site-packages'
00:40:59 #12 42.71
00:40:59 #12 42.71 at /usr/lib64/python3.8/pathlib.py:908 in relative_to
00:40:59 #12 42.83 904│ n = len(to_abs_parts)
00:40:59 #12 42.83 905│ cf = self._flavour.casefold_parts
00:40:59 #12 42.83 906│ if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
00:40:59 #12 42.83 907│ formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
00:40:59 #12 42.83 → 908│ raise ValueError("{!r} does not start with {!r}"
00:40:59 #12 42.83 909│ .format(str(self), str(formatted)))
00:40:59 #12 42.83 910│ return self._from_parsed_parts('', root if n == 1 else '',
00:40:59 #12 42.83 911│ abs_parts[n:])
00:40:59 #12 42.83 912│
This error used to be caught by a way to broad broad contextlib.suppress(Exception) suppression which @jaraco's recent PR removed. I'm not that familiar with pythons package/importing mechanisms but could we add back a specific suppress(ValueError) to _read_files_egginfo_installed? We could also completely avoid the error by manually loop through and checking the path before finding its relative. I'm happy to open a PR with either change
Activity
jaraco commentedon Jun 19, 2023
Yes, maybe. I'd first want to concoct a test that captures the expectation that's being missed by not trapping it. That is, I want to have a better understanding of the circumstances that lead to the failure, confirm that those circumstances are legitimate and supported, and then replicate them in a test to ensure that the library continues to honor that circumstance.
Can you investigate what it is about the interactions between ansible and poetry that lead to the failure and create a (ideally minimal) reproducer that replicates that state without using poetry or ansible?
dan-blanchard commentedon Jun 28, 2023
I just encountered a similar instance of this issue, and the root cause is that
pathlib.Path.relative_to()is not smart enough to add leading..to make a path relative tosite-packageseven when it should. (See https://bugs.python.org/issue23082#msg366433)If you switch to using
os.path.relpath, it will add the leading..instead of crashing.I subclassed
Distributionlocally to work around this and changedDistribution._read_files_egginfo_installedto:I may change it to just always use
os.path.relpathinstead of handling the exception. Happy to make a PR if this seems reasonable.jaraco commentedon Dec 3, 2023
Thanks dan-blanchard for the additional explanation. It does sound as if there may be a bug here. What I still don't understand is what are the conditions under which this failure occurs? Is there a way to replicate the conditions that trigger the failure? I'd like to avoid rewriting that function in a way that introduces extra branching and looping and mutated variables. If there is a deficiency in
Path.relative_to, I'd like to see that deficiency documented (in a bug if a bug else in the docs) and a wrapper function replacing the.relative_to()call that encapsulates the workaround... something like:Such an approach would be minimally invasive to the current behavior and provide context for a future reader to understand why that method exists (and possibly when it can be removed).
But more important is to find an example case that fails and write a test from it that captures the missed expectation.
If you'd like to start on a PR with that test, that would be fantastic. Or if you're only able to devise a set of steps to recreate the failure, that would be useful for someone else to create such a test.
dan-blanchard commentedon Jan 5, 2024
I just put up a PR that I think should fix (and test) this.
P403n1x87 commentedon Jan 11, 2024
Based on a report that we had, and that is now linked in this issue, I don't think a missing
..is all there is to it. From the following tracebackwe can see a path in the
binarea of a venv, and the other in thelibarea. My hunch is that the distribution is shipping entry points as part of its file contents, which would be located under thebinarea. Trying to match that under thesite-customizearea will fail.dan-blanchard commentedon Jan 11, 2024
I think we're both saying the same thing in different ways. Some packages include absolute paths in
installed-files.txtthat are outside ofsite-packages. When I was looking into the issue, this seems to happen pretty commonly with jupyterlab extension packages.pathlib.Path.relative_to()does not support trying to figure out how two absolute paths are relative to each other (unless one is a subpath of the other), whereasos.path.relpathdoes, so using that instead seems to solve the issue.For one of Coiled's projects where we kept running into this issue, I already worked around it by subclassing
Distributionas mentioned in #455 (comment), and things generally worked as expected. #482 should fix it as well.fix: pin importlib to 6.5.0 (#8075)
fix: pin importlib to 6.5.0 (#8075)
fix: pin importlib to 6.5.0 (#8075)
fix: pin importlib to 6.5.0 (#8075)
fix: pin importlib to 6.5.0 [backport 2.5] (#8196)
fix: pin importlib to 6.5.0 [backport 2.3] (#8194)
fix: pin importlib to 6.5.0 [backport 2.4] (#8195)