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

'[IndexError] list index out of range' on poetry install in Docker if packages is specified #1899

Closed
3 tasks done
kbakk opened this issue Jan 16, 2020 · 9 comments
Closed
3 tasks done
Labels
status/duplicate Duplicate issues

Comments

@kbakk
Copy link
Contributor

kbakk commented Jan 16, 2020

  • I am on the latest Poetry version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

Issue

A common technique when creating a Dockerfile is to copy pyproject.toml and poetry.lock, then running poetry install.

Like so:

COPY pyproject.toml poetry.lock ./
RUN poetry install --no-dev 
# then, add the rest of the project to only run
# poetry install when pyproject.toml/poetry.lock changes
COPY . /app

However, if the project is contains packages (see example pyproject.toml linked above), poetry will fail to find these, as they hasn't been copied over yet, and throw error:

[IndexError]
list index out of range

Traceback (most recent call last):
  File "/root/.poetry/lib/poetry/_vendor/py3.7/clikit/console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "/root/.poetry/lib/poetry/_vendor/py3.7/clikit/api/command/command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "/root/.poetry/lib/poetry/_vendor/py3.7/clikit/api/command/command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/root/.poetry/lib/poetry/_vendor/py3.7/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/root/.poetry/lib/poetry/console/commands/install.py", line 72, in handle
    builder = EditableBuilder(self.poetry, self._env, NullIO())
  File "/root/.poetry/lib/poetry/masonry/builders/builder.py", line 68, in __init__
    includes=self._package.include,
  File "/root/.poetry/lib/poetry/masonry/utils/module.py", line 72, in __init__
    source=package.get('from'),
  File "/root/.poetry/lib/poetry/masonry/utils/package_include.py", line 15, in __init__
    self.check_elements()
  File "/root/.poetry/lib/poetry/masonry/utils/package_include.py", line 37, in check_elements
    root = self._elements[0]

One can get around this by installing with --no-root flag:

RUN poetry install --no-dev --no-root

Related: #1247

@kbakk kbakk added the kind/bug Something isn't working as expected label Jan 16, 2020
@kbakk
Copy link
Contributor Author

kbakk commented Jan 16, 2020

Not sure why this line

root = self._elements[0]

comes before

if not self._elements:

Seems like this should have been in the other order, to give a more meaningful error.

@finswimmer finswimmer added area/error-handling Bad error messages/insufficient error handling and removed kind/bug Something isn't working as expected labels Jan 16, 2020
@willronchetti
Copy link

willronchetti commented Jan 16, 2020

Can report I am seeing the same error on readthedocs when I try to build documentation for a poetry project. Below is the traceback I get from the builder on readthedocs.

EDIT: my issue was I was not correctly specifying the packages directive in my pyproject.toml. It would be nice if an error like this was caught sooner and an appropriate response rendered, as the one below is not terribly helpful (thankfully my project is not terribly complex).

[rtd-command-info] start-time: 2020-01-16T19:40:58.600906Z, end-time: 2020-01-16T19:41:23.355646Z, duration: 24, exit-code: 1
/home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/bin/python -m pip install --upgrade --upgrade-strategy eager --cache-dir /home/docs/checkouts/readthedocs.org/user_builds/foursight/.cache/pip .[docs]
Processing /home/docs/checkouts/readthedocs.org/user_builds/foursight/checkouts/latest
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Installing backend dependencies: started
  Installing backend dependencies: finished with status 'done'
    Preparing wheel metadata: started
    Preparing wheel metadata: finished with status 'error'
    ERROR: Complete output from command /home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/bin/python /home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmpfjaowbjb:
    ERROR: Traceback (most recent call last):
      File "/home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 207, in <module>
        main()
      File "/home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 197, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
      File "/home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py", line 69, in prepare_metadata_for_build_wheel
        return hook(metadata_directory, config_settings)
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/api.py", line 38, in prepare_metadata_for_build_wheel
        builder = WheelBuilder(poetry, SystemEnv(Path(sys.prefix)), NullIO())
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/builders/wheel.py", line 44, in __init__
        super(WheelBuilder, self).__init__(poetry, env, io)
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/builders/builder.py", line 68, in __init__
        includes=self._package.include,
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/utils/module.py", line 72, in __init__
        source=package.get("from"),
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/utils/package_include.py", line 15, in __init__
        self.check_elements()
      File "/tmp/pip-build-env-h0r6dt0o/overlay/lib/python3.6/site-packages/poetry/masonry/utils/package_include.py", line 37, in check_elements
        root = self._elements[0]
    IndexError: list index out of range
    ----------------------------------------
ERROR: Command "/home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/bin/python /home/docs/checkouts/readthedocs.org/user_builds/foursight/envs/latest/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmpfjaowbjb" failed with error code 1 in /tmp/pip-req-build-xyjkozyi

@regmeg
Copy link

regmeg commented Feb 17, 2020

Another workaround/hack would just create a package empty folder with __init__.py so that poetry can find it. This will not break the cache, and one will still be able to copy over project/package files after requirements are installed. For instance if app root is /usr/src/app/ and with local package name package :

RUN mkdir -p /usr/src/app/package
RUN touch  /usr/src/app/package/__init__.py

@mcouthon
Copy link
Contributor

mcouthon commented Apr 2, 2020

My current workaround for this is as follows:

# Only copying these files here in order to take advantage of Docker cache. We only want the
# next stage (poetry install) to run if these files change, but not the rest of the app.
COPY pyproject.toml poetry.lock ./

# Currently poetry install is significantly slower than pip install, so we're creating a
# requirements.txt output and running pip install with it.
# Follow this issue: https://github.com/python-poetry/poetry/issues/338
# Setting --without-hashes because of this issue: https://github.com/pypa/pip/issues/4995
RUN poetry config virtualenvs.create false \
                && poetry export --without-hashes -f requirements.txt --dev \
                |  poetry run pip install -r /dev/stdin \
                && poetry debug

COPY  . ./

# Because initially we only copy the lock and pyproject file, we can only install the dependencies
# in the RUN above, as the `packages` portion of the pyproject.toml file is not
# available at this point. Now, after the whole package has been copied in, we run `poetry install`
# again to only install packages, scripts, etc. (and thus it should be very quick).
# See this issue for more context: https://github.com/python-poetry/poetry/issues/1899
RUN poetry install --no-interaction

# We're setting the entrypoint to `poetry run` because poetry installed entry points aren't
# available in the PATH by default, but it is available for `poetry run`
ENTRYPOINT ["poetry", "run"]

Hope this helps someone.

@gimlidc
Copy link

gimlidc commented Oct 15, 2020

I have the same issue when I try to call poetry export, but there is no workaroud (--no-root is not an option for export).

IndexError

  list index out of range

  at /var/lang/pipx/venvs/poetry/lib/python3.8/site-packages/poetry/utils/exporter.py:76 in _export_requirements_txt
       72│             if not dev
       73│             else self._poetry.package.all_requires,
       74│             with_nested=True,
       75│         ):
    →  76│             package = repository.find_packages(dependency=dependency)[0]
       77│ 
       78│             # If a package is optional and we haven't opted in to it, continue
       79│             if package.optional and package.name not in extra_package_names:
       80│                 continue

@abn
Copy link
Member

abn commented Oct 15, 2020

@gimlidc please update to 1.1.3.

imbstack added a commit to mozilla-it/ctms-api that referenced this issue Feb 19, 2021
@jackton1
Copy link

Deleting poetry.lock worked for me.

@neersighted
Copy link
Member

Duplicate #1301.

@neersighted neersighted closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2022
@neersighted neersighted added status/duplicate Duplicate issues and removed area/error-handling Bad error messages/insufficient error handling labels Oct 5, 2022
Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status/duplicate Duplicate issues
Projects
None yet
Development

No branches or pull requests

9 participants