6
6
import * as vscode from 'vscode'
7
7
import * as _ from 'lodash'
8
8
import * as nls from 'vscode-nls'
9
- import { getCodeRoot , getHandlerName , getTemplateResource } from '../../../lambda/local/debugConfiguration'
9
+ import { Runtime } from 'aws-sdk/clients/lambda'
10
+ import {
11
+ getCodeRoot ,
12
+ getHandlerName ,
13
+ getTemplateResource ,
14
+ NodejsDebugConfiguration ,
15
+ PythonDebugConfiguration ,
16
+ } from '../../../lambda/local/debugConfiguration'
10
17
import { getDefaultRuntime , getFamily , getRuntimeFamily , RuntimeFamily } from '../../../lambda/models/samLambdaRuntime'
11
18
import { CloudFormationTemplateRegistry , getResourcesFromTemplateDatum } from '../../cloudformation/templateRegistry'
19
+ import { Timeout } from '../../utilities/timeoutUtils'
20
+ import { ChannelLogger } from '../../utilities/vsCodeUtils'
12
21
import * as csharpDebug from './csharpSamDebug'
13
22
import * as pythonDebug from './pythonSamDebug'
14
23
import * as tsDebug from './typescriptSamDebug'
@@ -28,11 +37,88 @@ import {
28
37
AwsSamDebugConfigurationValidator ,
29
38
DefaultAwsSamDebugConfigurationValidator ,
30
39
} from './awsSamDebugConfigurationValidator'
31
- import { SamLaunchRequestArgs } from './samDebugSession'
32
40
import { makeConfig } from '../localLambdaRunner'
41
+ import { SamLocalInvokeCommand } from '../cli/samCliLocalInvoke'
33
42
34
43
const localize = nls . loadMessageBundle ( )
35
44
45
+ /**
46
+ * SAM-specific launch attributes (which are not part of the DAP).
47
+ *
48
+ * Schema for these attributes lives in package.json
49
+ * ("configurationAttributes").
50
+ *
51
+ * @see AwsSamDebuggerConfiguration
52
+ * @see AwsSamDebugConfigurationProvider.resolveDebugConfiguration
53
+ */
54
+ export interface SamLaunchRequestArgs extends AwsSamDebuggerConfiguration {
55
+ // readonly type: 'node' | 'python' | 'coreclr' | 'aws-sam'
56
+ readonly request : 'attach' | 'launch' | 'direct-invoke'
57
+
58
+ /** Runtime id-name passed to vscode to select a debugger/launcher. */
59
+ runtime : Runtime
60
+ runtimeFamily : RuntimeFamily
61
+ /** Resolved (potentinally generated) handler name. */
62
+ handlerName : string
63
+ workspaceFolder : vscode . WorkspaceFolder
64
+
65
+ /**
66
+ * Absolute path to the SAM project root, calculated from any of:
67
+ * - `codeUri` in `template.yaml`
68
+ * - `projectRoot` for the case of `target=code`
69
+ * - provider-specific heuristic (last resort)
70
+ */
71
+ codeRoot : string
72
+ outFilePath ?: string
73
+
74
+ /** Path to (generated) directory used as a working/staging area for SAM. */
75
+ baseBuildDir ?: string
76
+
77
+ /**
78
+ * URI of the current editor document.
79
+ * Used as a last resort for deciding `codeRoot` (when there is no `launch.json` nor `template.yaml`)
80
+ */
81
+ documentUri : vscode . Uri
82
+
83
+ /**
84
+ * SAM/CFN template absolute path used for SAM CLI invoke.
85
+ * - For `target=code` this is the _generated_ template path.
86
+ * - For `target=template` this is the _generated_ template path (TODO: in
87
+ * the future we may change this to be the template found in the workspace.
88
+ */
89
+ samTemplatePath : string
90
+
91
+ /**
92
+ * Path to the (generated) `event.json` file placed in `baseBuildDir` for SAM to discover.
93
+ *
94
+ * The file contains the event payload JSON to be consumed by SAM.
95
+ */
96
+ eventPayloadFile : string
97
+
98
+ /**
99
+ * Path to the (generated) `env-vars.json` file placed in `baseBuildDir` for SAM to discover.
100
+ *
101
+ * The file contains a JSON map of environment variables to be consumed by
102
+ * SAM, resolved from `template.yaml` and/or `lambda.environmentVariables`.
103
+ */
104
+ envFile : string
105
+
106
+ //
107
+ // Debug properties (when user runs with debugging enabled).
108
+ //
109
+ /** vscode implicit field, set if user invokes "Run (Start Without Debugging)". */
110
+ noDebug ?: boolean
111
+ debuggerPath ?: string
112
+ debugPort ?: number
113
+
114
+ //
115
+ // Invocation properties (for "execute" phase, after "config" phase).
116
+ // Non-serializable...
117
+ //
118
+ samLocalInvokeCommand ?: SamLocalInvokeCommand
119
+ onWillAttachDebugger ?( debugPort : number , timeout : Timeout , channelLogger : ChannelLogger ) : Promise < void >
120
+ }
121
+
36
122
/**
37
123
* `DebugConfigurationProvider` dynamically defines these aspects of a VSCode debugger:
38
124
* - Initial debug configurations (for newly-created launch.json)
@@ -89,11 +175,10 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
89
175
}
90
176
91
177
/**
92
- * Generates a launch-config from a user-provided debug-config, then launches it.
178
+ * Generates a full run-config from a user-provided config, then
179
+ * runs/debugs it (essentially `sam build` + `sam local invoke`).
93
180
*
94
- * - "Launch" means `sam build` followed by `sam local invoke`.
95
- * - If launch.json is missing, this function attempts to generate a
96
- * debug-config dynamically.
181
+ * If `launch.json` is missing, attempts to generate a config dynamically.
97
182
*
98
183
* @param folder Workspace folder
99
184
* @param config User-provided config (from launch.json)
@@ -103,6 +188,30 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
103
188
folder : vscode . WorkspaceFolder | undefined ,
104
189
config : AwsSamDebuggerConfiguration ,
105
190
token ?: vscode . CancellationToken
191
+ ) : Promise < SamLaunchRequestArgs | undefined > {
192
+ const resolvedConfig = await this . makeConfig ( folder , config , token )
193
+ if ( ! resolvedConfig ) {
194
+ return undefined
195
+ }
196
+ await this . invokeConfig ( resolvedConfig )
197
+ // TODO: return config here, and remove use of `startDebugging()` in `localLambdaRunner.ts`.
198
+ return undefined
199
+ }
200
+
201
+ /**
202
+ * Performs the CONFIG phase of SAM run/debug:
203
+ * - gathers info from `launch.json`, project workspace, OS
204
+ * - creates runtime-specific files
205
+ * - creates `input-template.yaml`, `env-vars.json`, `event.json` files
206
+ * - creates a config object to handoff to VSCode
207
+ *
208
+ * @returns Config to handoff to VSCode or nodejs/python/dotnet plugin (can
209
+ * also be used in `vscode.debug.startDebugging`)
210
+ */
211
+ public async makeConfig (
212
+ folder : vscode . WorkspaceFolder | undefined ,
213
+ config : AwsSamDebuggerConfiguration ,
214
+ token ?: vscode . CancellationToken
106
215
) : Promise < SamLaunchRequestArgs | undefined > {
107
216
if ( token ?. isCancellationRequested ) {
108
217
return undefined
@@ -229,7 +338,7 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
229
338
// 3. do `sam local invoke`
230
339
//
231
340
await makeConfig ( launchConfig )
232
- switch ( runtimeFamily ) {
341
+ switch ( launchConfig . runtimeFamily ) {
233
342
case RuntimeFamily . NodeJS : {
234
343
// Make a NodeJS launch-config from the generic config.
235
344
launchConfig = await tsDebug . makeTypescriptConfig ( launchConfig )
@@ -272,4 +381,28 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
272
381
273
382
return launchConfig
274
383
}
384
+
385
+ /**
386
+ * Performs the EXECUTE phase of SAM run/debug.
387
+ */
388
+ public async invokeConfig ( config : SamLaunchRequestArgs ) : Promise < SamLaunchRequestArgs > {
389
+ switch ( config . runtimeFamily ) {
390
+ case RuntimeFamily . NodeJS : {
391
+ config . type = 'node'
392
+ const c = await tsDebug . invokeTypescriptLambda ( this . ctx , config as NodejsDebugConfiguration )
393
+ return c
394
+ }
395
+ case RuntimeFamily . Python : {
396
+ config . type = 'python'
397
+ return await pythonDebug . invokePythonLambda ( this . ctx , config as PythonDebugConfiguration )
398
+ }
399
+ case RuntimeFamily . DotNetCore : {
400
+ config . type = 'coreclr'
401
+ return await csharpDebug . invokeCsharpLambda ( this . ctx , config )
402
+ }
403
+ default : {
404
+ throw Error ( `unknown runtimeFamily: ${ config . runtimeFamily } ` )
405
+ }
406
+ }
407
+ }
275
408
}
0 commit comments