madpah / requirements-parser

A Pip requirements file parser.
https://requirements-parser.readthedocs.io
Apache License 2.0
125 stars 41 forks source link

Incorrect relative path resolution #53

Closed hexagonrecursion closed 3 years ago

hexagonrecursion commented 3 years ago

Given a file r-bug.txt with the following contents

-r foo.txt

and a file foo.txt in the same directory with the following contents

requests==2.25.0

pip successfully installs requests even if the current directory is not the parent directory of r-bug.txt:

pip install --target "`mktemp -d`" -r ../r-bug.txt

but requirements fails to find foo.txt

import requirements
list(requirements.parse(Path('../r-bug.txt').read_text(encoding='utf8')))
~/.local/share/virtualenvs/pip-OwC7W1fl/lib/python3.8/site-packages/requirements/parser.py in parse(reqstr)
     35             new_file_path = os.path.join(os.path.dirname(filename or '.'),
     36                                          new_filename)
---> 37             with open(new_file_path) as f:
     38                 for requirement in parse(f):
     39                     yield requirement

FileNotFoundError: [Errno 2] No such file or directory: 'foo.txt'
hexagonrecursion commented 3 years ago

This might not be fixable with the current API: you have to know the file name in order to resolve relative paths.

hexagonrecursion commented 3 years ago

Never mind. I figured it out

import requirements
list(requirements.parse(Path('../r-bug.txt').open(encoding='utf8')))