forked from denoland/vscode_deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_config_provider.ts
More file actions
129 lines (119 loc) · 3.98 KB
/
debug_config_provider.ts
File metadata and controls
129 lines (119 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as vscode from "vscode";
import type { DenoExtensionContext } from "./types";
import { getDenoCommandName, getInspectArg } from "./util";
import { EXTENSION_NS } from "./constants";
export class DenoDebugConfigurationProvider
implements vscode.DebugConfigurationProvider {
#extensionContext: DenoExtensionContext;
#getEnv() {
const settings = this.#extensionContext.clientOptions
.initializationOptions();
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const env: Record<string, string> = {};
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
if (settings.cache) {
env["DENO_DIR"] = settings.cache;
}
if (settings.future) {
env["DENO_FUTURE"] = "1";
}
return env;
}
#getAdditionalRuntimeArgs() {
const args: string[] = [];
const settings = this.#extensionContext.clientOptions
.initializationOptions();
if (settings.unstable) {
args.push("--unstable");
}
if (settings.importMap) {
args.push("--import-map");
args.push(settings.importMap.trim());
}
if (settings.config) {
args.push("--config");
args.push(settings.config.trim());
}
return args;
}
#getInspectArg() {
return getInspectArg(this.#extensionContext.serverInfo?.version);
}
constructor(extensionContext: DenoExtensionContext) {
this.#extensionContext = extensionContext;
}
async provideDebugConfigurations(): Promise<vscode.DebugConfiguration[]> {
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const debugConfig: vscode.DebugConfiguration = {
request: "launch",
name: "Launch Program",
type: "node",
program: "${workspaceFolder}/main.ts",
cwd: "${workspaceFolder}",
env: this.#getEnv(),
runtimeExecutable: await getDenoCommandName(this.#extensionContext.approvedPaths),
runtimeArgs: [
"run",
...this.#getAdditionalRuntimeArgs(),
this.#getInspectArg(),
"--allow-all",
],
attachSimplePort: 9229,
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
debugConfig.envFile = denoEnvFile;
}
return [debugConfig];
}
async resolveDebugConfiguration(
workspace: vscode.WorkspaceFolder | undefined,
config: vscode.DebugConfiguration,
): Promise<vscode.DebugConfiguration | null | undefined> {
// if launch.json is missing or empty
if (!config.type && !config.request && !config.name) {
const editor = vscode.window.activeTextEditor;
const langId = editor?.document.languageId;
if (
editor &&
(langId === "typescript" || langId === "javascript" ||
langId === "typescriptreact" || langId === "javascriptreact")
) {
// Bypass the bug of the vscode 1.49.0
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const debugConfig: vscode.DebugConfiguration = {
request: "launch",
name: "Launch Program",
type: "node",
program: "${file}",
env: this.#getEnv(),
runtimeExecutable: await getDenoCommandName(this.#extensionContext.approvedPaths),
runtimeArgs: [
"run",
...this.#getAdditionalRuntimeArgs(),
this.#getInspectArg(),
"--allow-all",
],
attachSimplePort: 9229,
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
debugConfig.envFile = denoEnvFile;
}
// https://github.com/microsoft/vscode/issues/106703#issuecomment-694595773
vscode.debug.startDebugging(workspace, debugConfig);
return undefined;
}
return null;
}
if (!config.program) {
await vscode.window.showErrorMessage("Cannot resolve a program to debug");
return undefined;
}
return config;
}
}