serverless-esbuild icon indicating copy to clipboard operation
serverless-esbuild copied to clipboard

Cannot use debug breakpoint tool on VSCode

Open sinh117801 opened this issue 3 years ago • 16 comments

When I config this code in serverless.yml, I cannot use debugger breakpoint on VSCode.

  esbuild:
    bundle: true
    minify: true
    sourcemap: true
    keepNames: true

sinh117801 avatar Feb 14 '22 09:02 sinh117801

Hey @sinh117801, not quite sure what you mean. How are you running it? Can you give us a example?

samchungy avatar Feb 19 '22 04:02 samchungy

I think he wants to run the lambda with attached debugger from node

Setitch avatar Mar 30 '22 09:03 Setitch

@sinh117801 here's what I used to get VSCode debugging with this plugin:

Add a launch configuration that starts serverless offline with the debugger attached:

  "launch": {
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Debug Lambdas - Launch",
                "sourceMaps": true,
                "program": "${workspaceFolder}/node_modules/serverless/bin/serverless.js",
                "args": [
                    "offline",
                    "--config", "serverless.ts", // or serverless.yml, whatever you use
                    "--stage", "your-stage"
                ],
            }
        ]
    }

I had to disable minification when debugging, I couldn't inspect values of local variables otherwise. So put minify: false (if I'm missing something, do put a comment here). I also had to put sourcemap to 'linked'; external did not work for me, VSCode wouldn't load the sourcemaps. So my config is this one:

    esbuild:
      minify: false
      sourcemap: linked
      keepNames: true

coyoteecd avatar Jun 23 '22 12:06 coyoteecd

@sinh117801 here's what I used to get VSCode debugging with this plugin:

Add a launch configuration that starts serverless offline with the debugger attached:

  "launch": {
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Debug Lambdas - Launch",
                "sourceMaps": true,
                "program": "${workspaceFolder}/node_modules/serverless/bin/serverless.js",
                "args": [
                    "offline",
                    "--config", "serverless.ts", // or serverless.yml, whatever you use
                    "--stage", "your-stage"
                ],
            }
        ]
    }

I had to disable minification when debugging, I couldn't inspect values of local variables otherwise. So put minify: false (if I'm missing something, do put a comment here). I also had to put sourcemap to 'linked'; external did not work for me, VSCode wouldn't load the sourcemaps. So my config is this one:

    esbuild:
      minify: false
      sourcemap: linked
      keepNames: true

Thanks @coyoteecd. I really appreciate this. I will try it.

sinh117801 avatar Jul 05 '22 17:07 sinh117801

guys, all right.

I tried the same configuration but with "linked"

image

it doesn't work, I've already used some configurations but to debug correctly with esbuild I'm not succeeding.

image

if using config sourcemap: true it generates the .map but even so the breakpoint execution is still intermittent.

Has anyone ever experienced this ?

thiago-js avatar Jan 31 '23 14:01 thiago-js

Hi guys! 👋🏻 i have the same issue and i can't use the plugin with vscode debugger. I mean the breakpoints don't stop the execution. Also i tried all configuration you mention before without success. I share this repo where you can reproduce the case. If i comment the serverless-esbuild in serverless.yml the vscode debugger works fine!

ftschopp avatar Feb 14 '23 19:02 ftschopp

Pointing the outFiles of your launch configuration to ${workspaceFolder}/.esbuild/.build/**/*.js worked for me:

{
	"type": "node",
	"request": "launch",
	"name": "Debug",
	"preLaunchTask": "tsc: build - tsconfig.json",
	"program": "${workspaceFolder}/node_modules/.bin/sls",
	"console": "integratedTerminal",
	"runtimeVersion": "18.7.0",
	"runtimeArgs": ["--lazy"],
	"runtimeExecutable": "node",
	"args": [
		"offline",
		"-c", "serverless.ts",
		"--noTimeout",
		"--stage", "dev",
		"--region", "us-east-1",
		"--noAuth"
	],

	"outFiles": [
		"${workspaceFolder}/.esbuild/.build/**/*.js" // 👈 This is the important line, added `.esbuild`
	],
	"windows": {
		"program": "${workspaceRoot}\\node_modules\\serverless\\bin\\serverless",
	},
	"autoAttachChildProcesses": true,
	"sourceMaps": true,
	"smartStep": true
}

I also noticed that setting serverless.ts -> custom -> esbuild -> exclude to [] prevents it from working:

// serverless.ts
custom: {
  esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      keepNames: true,
      // exclude: [],   👈👈👈👈  comment this out
      target: 'node18',
      define: { 'require.resolve': undefined },
      platform: 'node',
      concurrency: 10,
    },
}

alxAgu avatar Feb 17 '23 08:02 alxAgu

Is there any accepted solution @floydspace?

None of the approaches here worked for me.

When I attach the debugger, breakpoints remain unbound:

image

colesiegel avatar Apr 24 '23 13:04 colesiegel

@sinh117801 here's what I used to get VSCode debugging with this plugin:

Add a launch configuration that starts serverless offline with the debugger attached:

  "launch": {
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Debug Lambdas - Launch",
                "sourceMaps": true,
                "program": "${workspaceFolder}/node_modules/serverless/bin/serverless.js",
                "args": [
                    "offline",
                    "--config", "serverless.ts", // or serverless.yml, whatever you use
                    "--stage", "your-stage"
                ],
            }
        ]
    }

I had to disable minification when debugging, I couldn't inspect values of local variables otherwise. So put minify: false (if I'm missing something, do put a comment here). I also had to put sourcemap to 'linked'; external did not work for me, VSCode wouldn't load the sourcemaps. So my config is this one:

    esbuild:
      minify: false
      sourcemap: linked
      keepNames: true

This worked for me but only after making a request, the first request breakpoints are unbound and they become bound after a test request. Any ideas what might cause this?

colesiegel avatar Apr 25 '23 12:04 colesiegel

This worked for me but only after making a request, the first request breakpoints are unbound and they become bound after a test request. Any ideas what might cause this?

Happens to me too

JoshuaAlzate avatar May 04 '23 13:05 JoshuaAlzate

This worked for me but only after making a request, the first request breakpoints are unbound and they become bound after a test request. Any ideas what might cause this?

Also seeing the same thing.

dpeacock avatar May 18 '23 19:05 dpeacock

anyone get hot reloading to work?

Having this same issue

This worked for me but only after making a request, the first request breakpoints are unbound and they become bound after a test request. Any ideas what might cause this?

Happens to me too

Same thing with me

thiagodeg avatar May 29 '23 14:05 thiagodeg

I had to ensure that the --data flag was present in my launch.json. Without it, the VSCode debugger wasn't stopping on my breakpoints

eugene-kim-pipe17 avatar Jun 11 '23 23:06 eugene-kim-pipe17

guys, all right.

I tried the same configuration but with "linked"

image

it doesn't work, I've already used some configurations but to debug correctly with esbuild I'm not succeeding.

image

if using config sourcemap: true it generates the .map but even so the breakpoint execution is still intermittent.

Has anyone ever experienced this ?

This work for me, thanks very much.

arcaela avatar Jun 29 '23 15:06 arcaela

As many of you, I too observer the issue, whereby the breakpoints are unbound when starting the debug script.

Looking at the Call Stack, in VSC - I noticed that, there is a worker that gets invoked after my first lambda invocation.

before after
CleanShot 2024-02-13 at 13 27 15 CleanShot 2024-02-13 at 13 27 28
CleanShot 2024-02-13 at 13 29 55 CleanShot 2024-02-13 at 13 30 19

It would appear, that when the debugger starts, it is not aware of the "application". This is due to us inspecting serverless offline which until the lambda it's running has been invoked is not aware of its code.

b3nk3 avatar Feb 13 '24 13:02 b3nk3