Skip to content

Commit cb8be2d

Browse files
authored
feat(sam nodejs debug): don't break on async_hooks #3846
Problem: Debugging a nodejs sam lambda always breaks at the nodejs internal module `internal/async_hooks` before reaching the first user-specified breakpoint. This is an old problem with `--inspect-brk`. #2758 Solution: - For "Image" packagetype, send `NODE_OPTIONS=--inspect=…` in the container environment variables instead of `--inspect-brk`. - For "Zip" packagetype set the `continueOnAttach` vscode nodejs debugger option, to workaround sam cli's hardcoded NODE_OPTIONS: https://github.com/aws/aws-sam-cli/blob/1adc080b82476288804c41c553c5e2ad86f28298/samcli/local/docker/lambda_debug_settings.py#L165
1 parent 9e38af1 commit cb8be2d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "SAM: Debugging a nodejs Lambda always hits breakpoint in nodejs async_hooks module before reaching the user-specified breakpoint"
4+
}

src/shared/sam/debugger/typescriptSamDebug.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ export async function makeTypescriptConfig(config: SamLaunchRequestArgs): Promis
7979
const isImageLambda = isImageLambdaConfig(config)
8080

8181
if (isImageLambda && !config.noDebug) {
82+
// Need --inspect to enable debugging. SAM CLI doesn't send env vars for "Image" packagetype.
8283
config.containerEnvVars = {
83-
NODE_OPTIONS: `--inspect-brk=0.0.0.0:${config.debugPort} --max-http-header-size 81920`,
84+
NODE_OPTIONS: `--inspect=0.0.0.0:${config.debugPort} --max-http-header-size 81920`,
8485
}
8586
}
8687

@@ -110,7 +111,11 @@ export async function makeTypescriptConfig(config: SamLaunchRequestArgs): Promis
110111
remoteRoot: remoteRoot ?? '/var/task',
111112
protocol: 'inspector',
112113
// Stop at first user breakpoint, not the runtime bootstrap file.
113-
stopOnEntry: config.stopOnEntry === undefined ? false : !!config.stopOnEntry,
114+
stopOnEntry: config.stopOnEntry ?? false,
115+
// See `continueOnAttach` at: https://code.visualstudio.com/docs/nodejs/nodejs-debugging
116+
// Workaround SAM CLI's outdated settings: https://github.com/aws/aws-sam-cli/blob/1adc080b82476288804c41c553c5e2ad86f28298/samcli/local/docker/lambda_debug_settings.py#L165
117+
// Not needed (but harmless) for "Image" packagetype: we set "NODE_OPTIONS:--inspect=…" above.
118+
continueOnAttach: config.continueOnAttach ?? true,
114119
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
115120
}
116121

src/test/shared/sam/debugger/samDebugConfigProvider.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ describe('SamDebugConfigurationProvider', async function () {
586586
protocol: 'inspector',
587587
remoteRoot: '/var/task',
588588
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
589+
continueOnAttach: true,
589590
stopOnEntry: false,
590591
}
591592

@@ -739,6 +740,7 @@ describe('SamDebugConfigurationProvider', async function () {
739740
protocol: 'inspector',
740741
remoteRoot: '/var/task',
741742
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
743+
continueOnAttach: true,
742744
stopOnEntry: false,
743745
}
744746

@@ -893,6 +895,7 @@ describe('SamDebugConfigurationProvider', async function () {
893895
protocol: 'inspector',
894896
remoteRoot: '/var/task',
895897
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
898+
continueOnAttach: true,
896899
stopOnEntry: false,
897900
}
898901

@@ -995,7 +998,7 @@ describe('SamDebugConfigurationProvider', async function () {
995998
baseBuildDir: actual.baseBuildDir, // Random, sanity-checked by assertEqualLaunchConfigs().
996999
containerEnvFile: `${actual.baseBuildDir}/container-env-vars.json`,
9971000
containerEnvVars: {
998-
NODE_OPTIONS: `--inspect-brk=0.0.0.0:${actual.debugPort} --max-http-header-size 81920`,
1001+
NODE_OPTIONS: `--inspect=0.0.0.0:${actual.debugPort} --max-http-header-size 81920`,
9991002
},
10001003
envFile: undefined,
10011004
eventPayloadFile: undefined,
@@ -1025,14 +1028,15 @@ describe('SamDebugConfigurationProvider', async function () {
10251028
protocol: 'inspector',
10261029
remoteRoot: '/var/task',
10271030
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
1031+
continueOnAttach: true,
10281032
stopOnEntry: false,
10291033
parameterOverrides: undefined,
10301034
}
10311035

10321036
assertEqualLaunchConfigs(actual, expected)
10331037
assertFileText(
10341038
expected.containerEnvFile!,
1035-
`{"NODE_OPTIONS":"--inspect-brk=0.0.0.0:${actual.debugPort} --max-http-header-size 81920"}`
1039+
`{"NODE_OPTIONS":"--inspect=0.0.0.0:${actual.debugPort} --max-http-header-size 81920"}`
10361040
)
10371041

10381042
//
@@ -1159,6 +1163,7 @@ describe('SamDebugConfigurationProvider', async function () {
11591163
protocol: 'inspector',
11601164
remoteRoot: '/var/task',
11611165
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
1166+
continueOnAttach: true,
11621167
stopOnEntry: false,
11631168
parameterOverrides: undefined,
11641169
}
@@ -2928,6 +2933,7 @@ describe('SamDebugConfigurationProvider', async function () {
29282933
runtime: 'nodejs12.x',
29292934
runtimeFamily: lambdaModel.RuntimeFamily.NodeJS,
29302935
skipFiles: ['/var/runtime/node_modules/**/*.js', '<node_internals>/**/*.js'],
2936+
continueOnAttach: true,
29312937
stopOnEntry: false,
29322938
}
29332939

0 commit comments

Comments
 (0)