aws / aws/aws-cdk

(aws-lambda-python-alpha): Bundling breaks if cdk.out is in the entry path

Open
#25,449 5 comments 6 reactions 1 assignee Claimed by @pahud View on GitHub
@aws-cdk/aws-lambda-python bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

In a project using [PythonFunction](https://docs.aws.amazon.com/cdk/api/v2/docs/@aws-cdk_aws-lambda-python-alpha.PythonFunction.html) and the same directory for the lambda entrypoint and the cdk app root, when running `cdk list`, cdk fails to bundle the asset with `RuntimeError: ENAMETOOLONG: name too long`

### Expected Behavior

`cdk list` lists the stack that uses the `PythonFunction`

### Current Behavior

```bash
$ cdk list
jsii.errors.JavaScriptError:
Error: ENAMETOOLONG: name too long, copyfile '/home/me/project/cdk.out/asset.3e3d591ada9871847d85c2d73040bddd35f67408d096082f05a5b6d2e65f9ac6/cdk.out/asset.3e3d591ada9871847d85c2d73040bddd35f67408d096082f05a5b6d2e65f9ac6/cdk.out/asset.......
at Object.copyFileSync (node:fs:2866:3)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1267)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)
at copyDirectory (/tmp/jsii-kernel-c554A9/node_modules/aws-cdk-lib/core/lib/fs/copy.js:1:1170)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/me/project/app.py", line 11, in
CollectStack(
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/jsii/_runtime.py", line 112, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/me/project/project/cdk/my_stack.py", line 28, in __init__
self.handler = self.create_api_handler()
File "/home/me/project/project/cdk/my_stack.py", line 31, in create_api_handler
handler = python.PythonFunction(
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/jsii/_runtime.py", line 112, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/aws_cdk/aws_lambda_python_alpha/__init__.py", line 1004, in __init__
jsii.create(self.__class__, self, [scope, id, props])
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 334, in create
response = self.provider.create(
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 363, in create
return self._process.send(request, CreateResponse)
File "/home/me/.cache/pypoetry/virtualenvs/project-py3.10/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 340, in send
raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: ENAMETOOLONG: name too long, copyfile .....

Subprocess exited with error 1
```

### Reproduction Steps

```python
from aws_cdk import CfnOutput, Stack
from aws_cdk import aws_lambda_python_alpha as python
from aws_cdk import aws_lambda as fn
from constructs import Construct

class MyStack(Stack):

def __init__(
self,
scope: Construct,
construct_id: str,
**kwargs,
) -> None:
super().__init__(scope, construct_id, **kwargs)

self.handler = self.create_api_handler()

def create_api_handler(self):
handler = python.PythonFunction(
self,
"Function",
entry=".",
runtime=fn.Runtime.PYTHON_3_10,
index="project/main.py",
handler="handler",
bundling=python.BundlingOptions(asset_excludes=["cdk.out"]),
)

return handler
```

### Possible Solution

Add `cdk.out` to [the exclude list used when bundling isn't required](https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-lambda-python-alpha/lib/bundling.ts#L10), or possibly copy the `asset_excludes`. The excludes list [is ignored](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3_assets.AssetOptions.html#exclude) when bundling is required. This is consistent with [other solutions](https://github.com/aws/aws-cdk/issues/3899#issuecomment-580394612) to this error

### Additional Information/Context

It is convenient to use one pyproject.toml for my whole application, including lambda handlers. So my repo looks like this:

```
pyproject.toml
poetry.lock
app.py
cdk.json
my_project
cdk
my_stack.py
handlers
my_handlers.py
```

I need to set the entrypoint to the dir with the pyproject.toml, which causes [cdk.out to get included in asset hashing](https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-lambda-python-alpha/lib/bundling.ts#L60) during `cdk list`

### CDK CLI Version

2.76.0 (build 78c411b)

### Framework Version

_No response_

### Node.js Version

v18.14.2

### OS

Pop!_OS 22.04 LTS

### Language

Python

### Language Version

Python 3.10.7

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.