Skip to content

Commit d37befe

Browse files
authored
SAM debugconfig: remove DebugAdapter #1141
Use of DebugAdapter and InlineDebugAdapterFactory was an experiment which may be potentially useful in the future. But this commit removes them because: - API is new: requires us to bump our minimum VSCode version - API may not be implemented by all targets yet (e.g. Eclipse Theia) - We don't actually *need* the DebugAdapter, yet
1 parent 1b8bb28 commit d37befe

13 files changed

+232
-469
lines changed

package-lock.json

Lines changed: 0 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "https://github.com/aws/aws-toolkit-vscode"
1111
},
1212
"engines": {
13-
"vscode": "^1.43.0"
13+
"vscode": "^1.42.0"
1414
},
1515
"icon": "resources/aws-icon-256x256.png",
1616
"bugs": {
@@ -886,7 +886,7 @@
886886
"@types/sinon": "^7.0.13",
887887
"@types/tcp-port-used": "^1.0.0",
888888
"@types/uuid": "^3.4.4",
889-
"@types/vscode": "^1.43.0",
889+
"@types/vscode": "^1.42.0",
890890
"@types/xml2js": "^0.4.3",
891891
"@typescript-eslint/eslint-plugin": "^2.27.0",
892892
"@typescript-eslint/eslint-plugin-tslint": "^2.27.0",
@@ -948,8 +948,6 @@
948948
"triple-beam": "^1.3.0",
949949
"typescript": "^3.7.2",
950950
"uuid": "^3.3.2",
951-
"vscode-debugadapter": "1.40.0-pre.2",
952-
"vscode-debugprotocol": "1.40.0-pre.1",
953951
"vscode-languageclient": "^6.1.1",
954952
"vscode-languageserver": "^6.1.1",
955953
"vscode-languageserver-textdocument": "^1.0.1",

src/lambda/local/debugConfiguration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {
1313
CodeTargetProperties,
1414
TemplateTargetProperties,
1515
} from '../../shared/sam/debugger/awsSamDebugConfiguration'
16-
import { SamLaunchRequestArgs } from '../../shared/sam/debugger/samDebugSession'
1716
import * as pathutil from '../../shared/utilities/pathUtils'
1817
import { localize } from '../../shared/utilities/vsCodeUtils'
1918
import { tryGetAbsolutePath } from '../../shared/utilities/workspaceUtils'
2019
import { RuntimeFamily } from '../models/samLambdaRuntime'
20+
import { SamLaunchRequestArgs } from '../../shared/sam/debugger/awsSamDebugger'
2121

2222
export const DOTNET_CORE_DEBUGGER_PATH = '/tmp/lambci_debug_files/vsdbg'
2323

src/shared/sam/activation.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { initialize as initializeSamCliContext } from './cli/samCliContext'
3131
import { detectSamCli } from './cli/samCliDetection'
3232
import { SamDebugConfigProvider } from './debugger/awsSamDebugger'
3333
import { addSamDebugConfiguration } from './debugger/commands/addSamDebugConfiguration'
34-
import { SamDebugSession } from './debugger/samDebugSession'
3534
import { AWS_SAM_DEBUG_TYPE } from './debugger/awsSamDebugConfiguration'
3635

3736
const STATE_NAME_SUPPRESS_YAML_PROMPT = 'aws.sam.suppressYamlPrompt'
@@ -54,17 +53,6 @@ export async function activate(ctx: ExtContext): Promise<void> {
5453
vscode.debug.registerDebugConfigurationProvider(AWS_SAM_DEBUG_TYPE, new SamDebugConfigProvider(ctx))
5554
)
5655

57-
// "Inline" DA type: runs inside the extension and directly talks to it.
58-
//
59-
// Debug adapters can be run in different ways, defined by the type of
60-
// `vscode.DebugAdapterDescriptorFactory` you implement:
61-
// https://code.visualstudio.com/api/extension-guides/debugger-extension#alternative-approach-to-develop-a-debugger-extension
62-
//
63-
// XXX: requires the "debuggers.*.label" attribute in package.json!
64-
ctx.extensionContext.subscriptions.push(
65-
vscode.debug.registerDebugAdapterDescriptorFactory(AWS_SAM_DEBUG_TYPE, new InlineDebugAdapterFactory(ctx))
66-
)
67-
6856
ctx.extensionContext.subscriptions.push(
6957
vscode.languages.registerCompletionItemProvider(
7058
{
@@ -169,30 +157,6 @@ async function activateCodeLensProviders(
169157
return disposables
170158
}
171159

172-
class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
173-
public constructor(readonly ctx: ExtContext) {}
174-
175-
/**
176-
* The inline implementation implements the Debug Adapter Protocol.
177-
* VSCode's extension API has a minimalistic subset of that protocol:
178-
* - vscode.DebugAdapter.handleMessage(): for passing a DAP message to the adapter.
179-
* - vscode.DebugAdapter.onDidSendMessage(): for listening for DAP messages received from the adapter.
180-
*
181-
* - Alternative: import the "vscode-debugprotocol" node module.
182-
* - Alternative (easier): use VSCode's default implementation of a debug
183-
* adapter, available as node module "vscode-debugadapter" in 1.38+ the
184-
* DebugSession (or LoggingDebugSession) is compatible with the
185-
* `vscode.DebugAdapter` interface defined in the extension API.
186-
*
187-
* https://code.visualstudio.com/updates/v1_42#_extension-authoring
188-
*/
189-
public createDebugAdapterDescriptor(
190-
_session: vscode.DebugSession
191-
): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
192-
return new vscode.DebugAdapterInlineImplementation(new SamDebugSession(this.ctx))
193-
}
194-
}
195-
196160
/**
197161
* Creates a prompt (via toast) to guide users to installing the Red Hat YAML extension.
198162
* This is necessary for displaying codelenses on templaye YAML files.

src/shared/sam/debugger/awsSamDebugger.ts

Lines changed: 140 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
import * as vscode from 'vscode'
77
import * as _ from 'lodash'
88
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'
1017
import { getDefaultRuntime, getFamily, getRuntimeFamily, RuntimeFamily } from '../../../lambda/models/samLambdaRuntime'
1118
import { CloudFormationTemplateRegistry, getResourcesFromTemplateDatum } from '../../cloudformation/templateRegistry'
19+
import { Timeout } from '../../utilities/timeoutUtils'
20+
import { ChannelLogger } from '../../utilities/vsCodeUtils'
1221
import * as csharpDebug from './csharpSamDebug'
1322
import * as pythonDebug from './pythonSamDebug'
1423
import * as tsDebug from './typescriptSamDebug'
@@ -28,11 +37,88 @@ import {
2837
AwsSamDebugConfigurationValidator,
2938
DefaultAwsSamDebugConfigurationValidator,
3039
} from './awsSamDebugConfigurationValidator'
31-
import { SamLaunchRequestArgs } from './samDebugSession'
3240
import { makeConfig } from '../localLambdaRunner'
41+
import { SamLocalInvokeCommand } from '../cli/samCliLocalInvoke'
3342

3443
const localize = nls.loadMessageBundle()
3544

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+
36122
/**
37123
* `DebugConfigurationProvider` dynamically defines these aspects of a VSCode debugger:
38124
* - Initial debug configurations (for newly-created launch.json)
@@ -89,11 +175,10 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
89175
}
90176

91177
/**
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`).
93180
*
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.
97182
*
98183
* @param folder Workspace folder
99184
* @param config User-provided config (from launch.json)
@@ -103,6 +188,30 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
103188
folder: vscode.WorkspaceFolder | undefined,
104189
config: AwsSamDebuggerConfiguration,
105190
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
106215
): Promise<SamLaunchRequestArgs | undefined> {
107216
if (token?.isCancellationRequested) {
108217
return undefined
@@ -229,7 +338,7 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
229338
// 3. do `sam local invoke`
230339
//
231340
await makeConfig(launchConfig)
232-
switch (runtimeFamily) {
341+
switch (launchConfig.runtimeFamily) {
233342
case RuntimeFamily.NodeJS: {
234343
// Make a NodeJS launch-config from the generic config.
235344
launchConfig = await tsDebug.makeTypescriptConfig(launchConfig)
@@ -272,4 +381,28 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
272381

273382
return launchConfig
274383
}
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+
}
275408
}

src/shared/sam/debugger/csharpSamDebug.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import { DefaultDockerClient, DockerClient } from '../../clients/dockerClient'
1717
import { ExtContext } from '../../extensions'
1818
import { mkdir } from '../../filesystem'
1919
import { DefaultSamLocalInvokeCommand, WAIT_FOR_DEBUGGER_MESSAGES } from '../../sam/cli/samCliLocalInvoke'
20-
import { SamLaunchRequestArgs } from '../../sam/debugger/samDebugSession'
2120
import { getStartPort } from '../../utilities/debuggerUtils'
2221
import { ChannelLogger, getChannelLogger } from '../../utilities/vsCodeUtils'
2322
import { invokeLambdaFunction, makeInputTemplate, waitForDebugPort } from '../localLambdaRunner'
23+
import { SamLaunchRequestArgs } from './awsSamDebugger'
2424

2525
/**
2626
* Gathers and sets launch-config info by inspecting the workspace and creating
@@ -61,12 +61,12 @@ export async function makeCsharpConfig(config: SamLaunchRequestArgs): Promise<Sa
6161
* Linux, then mount it with the SAM app on run. User's C# workspace dir will
6262
* have a `.vsdbg` dir after the first run.
6363
*/
64-
export async function invokeCsharpLambda(ctx: ExtContext, config: SamLaunchRequestArgs): Promise<void> {
64+
export async function invokeCsharpLambda(ctx: ExtContext, config: SamLaunchRequestArgs): Promise<SamLaunchRequestArgs> {
6565
config.samLocalInvokeCommand = new DefaultSamLocalInvokeCommand(getChannelLogger(ctx.outputChannel), [
6666
WAIT_FOR_DEBUGGER_MESSAGES.DOTNET,
6767
])
6868
config.onWillAttachDebugger = waitForDebugPort
69-
await invokeLambdaFunction(ctx, config, async () => {
69+
return await invokeLambdaFunction(ctx, config, async () => {
7070
if (!config.noDebug) {
7171
await _installDebugger(
7272
{

src/shared/sam/debugger/pythonSamDebug.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Timeout } from '../../utilities/timeoutUtils'
2020
import { ChannelLogger } from '../../utilities/vsCodeUtils'
2121
import { DefaultSamLocalInvokeCommand, WAIT_FOR_DEBUGGER_MESSAGES } from '../cli/samCliLocalInvoke'
2222
import { invokeLambdaFunction, makeInputTemplate } from '../localLambdaRunner'
23-
import { SamLaunchRequestArgs } from './samDebugSession'
23+
import { SamLaunchRequestArgs } from './awsSamDebugger'
2424

2525
const PYTHON_DEBUG_ADAPTER_RETRY_DELAY_MS = 1000
2626

@@ -182,10 +182,14 @@ export async function makePythonDebugConfig(config: SamLaunchRequestArgs): Promi
182182
/**
183183
* Launches and attaches debugger to a SAM Python project.
184184
*/
185-
export async function invokePythonLambda(ctx: ExtContext, config: PythonDebugConfiguration) {
185+
export async function invokePythonLambda(
186+
ctx: ExtContext,
187+
config: PythonDebugConfiguration
188+
): Promise<PythonDebugConfiguration> {
186189
config.samLocalInvokeCommand = new DefaultSamLocalInvokeCommand(ctx.chanLogger, [WAIT_FOR_DEBUGGER_MESSAGES.PYTHON])
187190
config.onWillAttachDebugger = waitForPythonDebugAdapter
188-
await invokeLambdaFunction(ctx, config, async () => {})
191+
const c = (await invokeLambdaFunction(ctx, config, async () => {})) as PythonDebugConfiguration
192+
return c
189193
}
190194

191195
export async function waitForPythonDebugAdapter(debugPort: number, timeout: Timeout, channelLogger: ChannelLogger) {

0 commit comments

Comments
 (0)