aws / aws-lambda-builders

Python library to compile, build & package AWS Lambda functions for several runtimes & framework
Apache License 2.0
341 stars 140 forks source link

Missing PKG-INFO in case of python-pip workflow #114

Closed pushrbx closed 2 years ago

pushrbx commented 5 years ago

Description: There are cases where there is no PKG-INFO file in the package's egg-info directory. Instead it exists in the package's root folder. I assume this is the case for those packages which doesn't have binary distribution. The problem rises with SAM CLI when I try to build my lambda app.

Steps to reproduce the issue:

  1. Download the following zip: reprosamissue.zip - it contains a sample project
  2. Unzip it in a folder.
  3. Create a virtual env.
  4. Activate virtual env in a terminal
  5. Navigate to the project folder
  6. cd hello-world
  7. pip install -r requirements.txt
  8. cd ..
  9. sam build -b .\dist

Observed result:

PS C:\Users\pushrbx\PycharmProjects\reprosamissue> sam build --debug -b .\dist
2019-05-16 15:26:05 Using SAM Template at C:\Users\pushrbx\PycharmProjects\reprosamissue\template.yaml
2019-05-16 15:26:05 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-05-16 15:26:05 Changing event name from before-call.apigateway to before-call.api-gateway
2019-05-16 15:26:05 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-05-16 15:26:05 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-05-16 15:26:05 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-05-16 15:26:05 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-05-16 15:26:05 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section 2019-05-16 15:26:05 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-05-16 15:26:05 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-05-16 15:26:05 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-05-16 15:26:05 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-05-16 15:26:05 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-05-16 15:26:05 Changing event name from before-call.apigateway to before-call.api-gateway
2019-05-16 15:26:05 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-05-16 15:26:05 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-05-16 15:26:05 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-05-16 15:26:05 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-05-16 15:26:05 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section 2019-05-16 15:26:05 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-05-16 15:26:05 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-05-16 15:26:05 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-05-16 15:26:05 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-05-16 15:26:05 'build' command is called
2019-05-16 15:26:05 No Parameters detected in the template
2019-05-16 15:26:05 2 resources found in the template
2019-05-16 15:26:05 Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2019-05-16 15:26:05 Building resource 'HelloWorldFunction'
2019-05-16 15:26:05 Loading workflow module 'aws_lambda_builders.workflows'
2019-05-16 15:26:05 Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2019-05-16 15:26:05 Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2019-05-16 15:26:05 Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2019-05-16 15:26:05 Running workflow 'PythonPipBuilder'
2019-05-16 15:26:05 Running PythonPipBuilder:ResolveDependencies
2019-05-16 15:26:05 calling pip download -r C:\Users\pushrbx\PycharmProjects\reprosamissue\hello_world\requirements.txt --dest C:\Users\TAMAS~1.SZE\AppData\Local\Temp\tmp2hcw4uih
2019-05-16 15:26:14 PythonPipBuilder:ResolveDependencies raised unhandled exception
Traceback (most recent call last):
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflow.py", line 248, in run
    action.execute()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\actions.py", line 39, in execute
    self.scratch_dir
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 143, in build_dependencies
    requirements_path, artifacts_dir_path, scratch_dir_path)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 209, in build_site_packages
    scratch_directory, requirements_filepath)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 234, in _download_dependencies
    requirements_filename, directory)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 332, in _download_all_dependencies
    in self._osutils.get_directory_contents(directory)}
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 331, in <setcomp>
    deps = {Package(directory, filename) for filename
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 437, in __init__
    self._name, self._version = self._calculate_name_and_version()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 480, in _calculate_name_and_version
    sdist_path)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 541, in get_package_name_and_version
    pkg_info_filepath = self._generate_egg_info(package_dir)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py", line 524, in _generate_egg_info
    egg_info_dir, info_contents[0], 'PKG-INFO')
IndexError: list index out of range

Build Failed
Error: PythonPipBuilder:ResolveDependencies - list index out of range

Expected result:

C:\Users\pushrbx\PycharmProjects\reprosamissue> sam build -b .\dist
2019-05-16 15:17:06 Building resource 'HelloWorldFunction'
2019-05-16 15:17:06 Running PythonPipBuilder:ResolveDependencies
2019-05-16 15:17:16 Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : dist
Built Template   : dist\template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t dist\template.yaml
[*] Package: sam package --s3-bucket <yourbucket> --template-file dist\template.yaml

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

OS: Windows 10 Attempted in a powershell console. Python version: 3.6 (for the virtualenv)

Possible fix The following change completely cured the problem: https://github.com/pushrbx/aws-lambda-builders/commit/5449ac38f54de50b9c780a8c29c861d4322101b4

I'm not clear on the environment requirements to run builds with SAM CLI but this change only worked after I've installed the wheel package in my virtualenv.

federicopuntoni commented 5 years ago

same here with googleads package in my virtualenv

OscarVanL commented 5 years ago

Same error here with Amazon's own sagemaker package.

joshpeng commented 5 years ago

Experiencing the same when using anything that requires cryptography.

anwarhusain commented 5 years ago

same here with gql

Mike-Nahmias commented 5 years ago

Same issue with pendulum

Adding --use-container to my sam build command seems to work

mkilp commented 5 years ago

Adding --use-container fixes this temporarily. However will this issue ever be addressed?

pushrbx commented 5 years ago

I might try to create a PR when I have free time. 😄

mkilp commented 5 years ago

Also Update: Using Pipenv instead of virtual env for dependencies fixes this issue for me.

andriihorpenko commented 5 years ago

Will it be fixed any soon?

kadrach commented 4 years ago

This affects dependencies that use PEP518 to specify a non-setuptools build system. E.g. a poetry-based package will not have a setup.py to execute and pull dependency information from: https://github.com/awslabs/aws-lambda-builders/blob/24f0aca6ed41ec2b08333655adbd1433f14cdc9e/aws_lambda_builders/workflows/python_pip/packager.py#L485-L486

kthblmfld commented 4 years ago

Just hit this issue. Switching over to pipenv fixed. Thanks @Nop0x!

awood45 commented 4 years ago

Summarizing this thread, some questions for us to understand the scope. If anyone who commented here had a different experience, please do correct me:

  1. This problem seems to happen on Windows, and never when using --use-container for building.
  2. Moving to pipenv over virtualenv solves the issue.

Are these statements correct for everyone? Especially the first one.

Yocker95k commented 4 years ago

I have the same Error as @pushrbx but when I change to --use-container I receive the following error:

Observed result

"C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd" build TelegramUpdateFunction --template C:\Users\user\PycharmProjects\TelegramBot\template.yaml --build-dir C:\Users\user\PycharmProjects\TelegramBot\.aws-sam\build --use-container
Starting Build inside a container
Building resource 'TelegramUpdateFunction'

Fetching lambci/lambda:build-python3.7 Docker container image......
Mounting C:\User\suser\PycharmProjects\TelegramBot\telegram_update as /tmp/samcli/source:ro,delegated inside runtime container

Build Failed
Running PythonPipBuilder:ResolveDependencies
Error: PythonPipBuilder:ResolveDependencies - Requirements file not found: /tmp/samcli/source/requirements.txt

As far as I can tell this whole problem occurs once I import the python-telegram-bot library. I am pretty new to python and not so familiar with this virtualenv, pip etc stuff so feel free to give advice 😄 I am coming from java and primary worked on webapps using spring / springboot

I also tried this although don`t really know what wheel. Didn't work, but maybe I did something wrong.

Possible fix The following change completely cured the problem: pushrbx@5449ac3

I'm not clear on the environment requirements to run builds with SAM CLI but this change only worked after I've installed the wheel package in my virtualenv.

I will try the approach to use pipenv, but I first have to get familiar with it.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

I use Pycharm with a virtualenv on Windows. I have Docker for Windows installed and working.

Update

So now I switched to pipenv. When I run / build it with --use-container it works. Without container I am back at the Error: PythonPipBuilder:ResolveDependencies - list index out of range

nicolasfguillaume commented 4 years ago

Try chalice --debug deploy

For me (chalice 1.13.1, python 3.6.5, windows 10), the issue was in the requirements.txt: I had to downgrade pandas 1.0.0 to 0.24 then it worked!

malk0lm commented 4 years ago

I'm having this problem and in answer to @awood45 it happens when I use build -u. However, I get a different error.

PS C:\Users\...\project> sam build
Building resource 'LambdaExtractFunction'
Running PythonPipBuilder:ResolveDependencies

Build Failed
Error: PythonPipBuilder:ResolveDependencies - list index out of range
PS C:\Users\...\project> sam build -u
Starting Build inside a container
Building resource 'LambdaExtractFunction'

Fetching lambci/lambda:build-python3.8 Docker container image......
Mounting C:\Users\...\project\src as /tmp/samcli/source:ro,delegated inside runtime container

Build Failed
Error: PythonPipBuilder:ResolveDependencies - Requirements file not found: /tmp/samcli/source/requirements.txt
Running PythonPipBuilder:ResolveDependencies

I am using a Python 3.8 virtualenv that was configured using pycharm's Python Interpreter setup.

NINJA EDIT: Just want to pop in and say that this is some kind of cache/runtime issue. I rebooted and started from a clean project (fresh git clone) and it worked fine.

aaronzhongg commented 4 years ago

Could someone point me towards how to use pipenv with sam build?

Correct me if this is wrong, but I was under the impression that only requirements.txt is supported with the python-pip workflow? https://github.com/awslabs/aws-sam-cli/issues/817

nlothian commented 4 years ago

We hit this problem too.

All builds were on Linux.

requirements.txt looks like this

aws-xray-sdk>=2.4.2
opencv-python-headless==4.4.0.42
arnathan2k commented 3 years ago

Same here... any update on the solution?

nilskelleter commented 3 years ago

I've the same issue with sam build when I import aws-lambda-powertools on Windows 10 and Python 3.8. If I'm using --use-container the build is successful. But --use-container is much slower on my machine, therefore I wanted to find a solution. I've found a solution for myself, and I want to share it here. I don't know if it solves the complete complexity of this issue but at the moment its working for me.

For the solution I've changed C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\python_pip\packager.py

First I've added a debug log LOG.debug("Generate egg-info for: %s", package_dir) in line 500 to be informed which package raises the error. In my case the future package is the reason fo the error, because as @pushrbx mentioned in the initial post the PKG-INFO file is not part of the egg-info directory after the execution of setuptools and only exists in the package root directory. In fact the complete directory egg-info is empty after setuptools, which then raises the IndexError because info_contents is empty in line 500.

To solve the IndexError, I've edited line 500 to read the PKG-INFO file from the package root directory.

Existing: pkg_info_path = self._osutils.joinpath(egg_info_dir, info_contents[0], "PKG-INFO")

Edited:

if info_contents:
   pkg_info_path = self._osutils.joinpath(egg_info_dir, info_contents[0], "PKG-INFO")
else:
   pkg_info_path = self._osutils.joinpath(package_dir, "PKG-INFO")

At the moment I've no answer why the egg-info directory is completely empty, so it can be that I only fix the symptom and not the root cause.

diegonat commented 3 years ago

I am having the same problem here from Cloud9

2021-03-09 17:08:37,561 | calling pip download -r /home/ec2-user/environment/lambdaproxy/lambdaproxy/lambdaproxy/requirements.txt --dest /tmp/tmpzu7o5ygz 2021-03-09 17:08:40,415 | PythonPipBuilder:ResolveDependencies raised unhandled exception Traceback (most recent call last): File "aws_lambda_builders/workflow.py", line 269, in run File "aws_lambda_builders/workflows/python_pip/actions.py", line 42, in execute File "aws_lambda_builders/workflows/python_pip/packager.py", line 137, in build_dependencies File "aws_lambda_builders/workflows/python_pip/packager.py", line 204, in build_site_packages File "aws_lambda_builders/workflows/python_pip/packager.py", line 228, in _download_dependencies File "aws_lambda_builders/workflows/python_pip/packager.py", line 311, in _download_all_dependencies File "aws_lambda_builders/workflows/python_pip/packager.py", line 311, in File "aws_lambda_builders/workflows/python_pip/packager.py", line 417, in init File "aws_lambda_builders/workflows/python_pip/packager.py", line 459, in _calculate_name_and_version File "aws_lambda_builders/workflows/python_pip/packager.py", line 519, in get_package_name_and_version File "aws_lambda_builders/workflows/python_pip/packager.py", line 502, in _generate_egg_info IndexError: list index out of range

Build Failed

requirements.txt

aws-xray-sdk boto3 botocore

robertsigler commented 3 years ago

I'm also hitting this problem.

Specifically, it happens in a Python 3.6 SAM project, when running sam build with --use-container, on the dependency cryptography==3.4.6. When I don't use --use-container, the build works (on macOS 10.15.7).

I dug into it a little bit more and it looks like, when trying to _generate_egg_info in a containerized SAM build for the cryptography 3.4.6 package, the setup.py invocation here ( https://github.com/aws/aws-lambda-builders/blob/develop/aws_lambda_builders/workflows/python_pip/packager.py#L498 ) is failing. Grabbing the stdout and stderr output from the command, it's failing because setuptools_rust is missing. Making sure that setuptools_rust was installed in the container solved the problem.

I'm assuming that I didn't hit the error on macOS because the 'manylinux' package is used instead, thereby skipping the need for Rust compilation.

Since the cryptography package is a big deal, does it make sense to install setuptools_rust in the container image so that trying to _generate_egg_info for this package doesn't fail? I'd submit a pull request but I can't find the Dockerfile for amazon/aws-sam-cli-build-image-python3.6.

Furthermore, it seems like it would be a good idea to check the returncode on the setup.py command and fail or show some debug output if it didn't succeed, since it probably means that we won't be able to locate the PKG-INFO.

ech0server commented 3 years ago

I've was having this issue with sam build , I figure it out by:

If you are running sam build --use-container with private artifactories make sure you add the URL to the requirements.txt like

--extra-index-url https://artifactory-real-url.com/artifactory/api/pypi/pypi-repos/simple

As it it were any other package.

oscar-bardrick-h24 commented 3 years ago

also experiencing this issue with the deepdiff package

Tishka17 commented 3 years ago

Had same problem with my package dataclass-factory. Packaging it as bdist_wheel isntead of sdist fixed for me

pushrbx commented 3 years ago

Does #265 completely cure this?

prenx4x commented 3 years ago

Yes, it will.

hawflau commented 2 years ago

Closing as this was fixed in Lambda Builders 1.7.0. Feel free to re-open if this is not the case.