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

aws-lambda-nodejs: Default entry finding fails when defining file starts with "file://" #21630

Open
jonbarksdale opened this issue Aug 16, 2022 · 12 comments
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@jonbarksdale
Copy link

Describe the bug

Attempting to create a lambda with NodejsFunction, which should auto find the handler based on the defining files path.

I get an error that looks like this:

Error: Cannot find handler file file:///Users/stack.handler.ts, file:///Users/<path>/stack.handler.js or file:///Users/<path>/stack.handler.mjs

where the middle of the path has been replaced with for brevity and privacy.

This implies the paths being checked start with "file://", which I've tested and does not work with Node 16.13.1.

Those paths are produced from this function, which I believe is incorrect.

This is running on macos 12.5, with the a fore mentioned version of nodejs.

Expected Behavior

I expected it to find the file, as the path was correct without the file:// prefix.

Current Behavior

Error: Cannot find handler file file:///Users/stack.handler.ts, file:///Users//stack.handler.js or file:///Users//stack.handler.mjs

Reproduction Steps

Create a lambda function using NodejsFunction with no entry parameter specified, and synthesize.

Possible Solution

callsites() produces paths that start with file:// when they are local files, and findDefiningFile doesn't scrub the path. Maybe make findDefining file remove the prefix when it's file://

Additional Information/Context

No response

CDK CLI Version

2.37.1 (build f15dee0)

Framework Version

No response

Node.js Version

16.13.1

OS

macos 12.5

Language

Typescript

Language Version

4.7.2

Other information

No response

@jonbarksdale jonbarksdale added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 16, 2022
@corymhall
Copy link
Contributor

@jonbarksdale I've tested this out on the same node version and macos version and have not been able to reproduce the issue. Can you try reproducing on a fresh project and let me know if you can reproduce?

@corymhall corymhall added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. needs-reproduction This issue needs reproduction. labels Aug 22, 2022
@github-actions
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Aug 24, 2022
@okko
Copy link

okko commented Aug 29, 2022

Workaround: sindresorhus/callsites#18 (comment)

Node 16.17.0 was expected to fix this according to the callsites issue 18 above, nodejs/node#39787 but I'm experiencing still the same with Node 16.17.0 when using ESM.

@okko
Copy link

okko commented Aug 29, 2022

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Aug 29, 2022
@corymhall
Copy link
Contributor

corymhall commented Aug 29, 2022

@okko thanks for the additional info and the reproduction! I think I'm fine with the solution that you suggest, i.e. adding replace('file://', '').

We would definitely accept contributions!

@corymhall corymhall added p2 effort/small Small work item – less than a day of effort and removed needs-reproduction This issue needs reproduction. needs-triage This issue or PR still needs to be triaged. labels Aug 29, 2022
@corymhall corymhall removed their assignment Aug 29, 2022
@jonbarksdale
Copy link
Author

Thanks @okko, appreciate you stepping in with the reproduction.

@okko
Copy link

okko commented Nov 2, 2022

Workaround until issue is resolved in aws-cdk:

const handler = new lambdaNodejs.NodejsFunction(scope, functionId, {
  entry: new URL(import.meta.url.replace(/(.*)(\..+)/, '$1.' + functionId + '$2')).pathname, // https://github.com/aws/aws-cdk/pull/21802#issuecomment-1249940400
})

@e3dio
Copy link

e3dio commented Jul 8, 2023

So the default way to use Node.js Lambda function does not work, and no progress has been made for 11 months to fix it? I am getting same error

@nickej
Copy link

nickej commented Oct 11, 2024

Is entry: new URL(import.meta.url.replace(/(.*)(\..+)/, '$1.' + functionId + '$2')).pathname still the best workaround for this issue? It looks lite the PR by @okko was abandoned. I could look into making a new PR with the replace('file://', '') fix since this seems to still be an issue.

@okko
Copy link

okko commented Oct 14, 2024

Yes, it is still the best workaround. I appreciate if you @nickej want to continue with the code from https://github.com/aws/aws-cdk/pull/21802/files. At the time it was blocked due to no tooling for ESM in the tests and @TheRealAmazonKendra requiring integration tests. The NodeJSFunction has been broken for ESM functions for over 2 years.

@nickej
Copy link

nickej commented Oct 14, 2024

Ok, thanks for confirming @okko. I've worked a bit in my own fork on this but unfortunately it looks like there is still no easy way to run integration-tests using ESM. At least not something I found. Writing the test itself is easy enough but setting it up so that the code under test will be generated towards ESM is not something I had any luck with.

I have asked for support in the cdk-dev Slack.

@Ys-Zhou
Copy link

Ys-Zhou commented Dec 23, 2024

It has been 2 years. Since node18 will be EOL in monthes, I really think cdk should support ESM by default and give a flag to anyone who wants to keep on CJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
6 participants