ServerlessLife / lambda-live-debugger

Remote debugging AWS Lambda functions
https://www.lldebugger.com/
Mozilla Public License 2.0
35 stars 3 forks source link

Not possible to debug CDK ES module project that uses import.meta #44

Closed kristiandreher closed 2 months ago

kristiandreher commented 2 months ago

Thanks for a really cool project!

However when trying to debug a CDK ES module project that uses import.meta to resolve dirname lld throws an error.

▲ [WARNING] "import.meta" is not available with the "cjs" output format and will be empty [empty-import-meta]

    lib/esm-test-app-stack.ts:8:49:
      8 │ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
        ╵                                                  ~~~~~~~~~~~

  You need to set the output format to "esm" for "import.meta" to work correctly.

Error: Error running CDK code in worker: Invalid URL

lib/esm-test-app-stack:

import { Duration, Stack, type StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as url from "url";
import { resolve } from "path";
import { NodejsFunction, OutputFormat } from "aws-cdk-lib/aws-lambda-nodejs";
import { Runtime } from "aws-cdk-lib/aws-lambda";

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

export class EsmTestAppStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    new NodejsFunction(this, "Lambda", {
      timeout: Duration.seconds(20),
      memorySize: 1024,
      runtime: Runtime.NODEJS_20_X,
      entry: resolve(__dirname, "./handler.ts"),
      environment: {
        NODE_OPTIONS: "--enable-source-maps",
        LOG_LEVEL: "DEBUG",
      },
      bundling: {
        minify: true,
        sourceMap: true,
        externalModules: [],
        platform: "node",
        // ESM important properties:
        mainFields: ["module", "main"],
        format: OutputFormat.ESM,
        banner:
          "const require = (await import('node:module')).createRequire(import.meta.url);",
      },
    });
  }
}
ServerlessLife commented 2 months ago

Thank you, @kristiandreher, for reporting the issue.

I will try to reproduce the problem. If, by any chance, you already have a sample project, that would help.

ServerlessLife commented 2 months ago

I hopefully made the same environment as you have: https://github.com/ServerlessLife/lambda-live-debugger/tree/44-fix-CDK-ES-module/test/cdk-esm

CDK with ES modules is not officially supported. I guess you used the tsx npm module and modified the cdk.json the same way I did.

I managed to reproduce the problem and fix it.

I will do some more testing tomorrow and then merge the PR.

kristiandreher commented 2 months ago

Great! Yes, I am using tsx and the env you have created looks similar to mine. I was not aware that CDK with ES modules is not officially supported. We have been using it for a while without any problems.

ServerlessLife commented 2 months ago

I see that my fix breaks other less standard CDK configurations. I will need a little time to find the solution.

ServerlessLife commented 2 months ago

The issue is fixed. Can you verify?

kristiandreher commented 1 month ago

The fix works! Thanks a lot!