-
Notifications
You must be signed in to change notification settings - Fork 11
/
expoDebuggers.ts
301 lines (257 loc) · 9.81 KB
/
expoDebuggers.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import fs from 'fs';
import vscode from 'vscode';
import {
fetchDevicesToInspect,
askDeviceByName,
inferDevicePlatform,
fetchDevicesToInspectFromUnknownWorkflow,
} from './expo/bundler';
import {
ExpoProjectCache,
ExpoProject,
findProjectFromWorkspaces,
findProjectFromWorkspace,
} from './expo/project';
import { debug } from './utils/debug';
import { featureTelemetry } from './utils/telemetry';
const log = debug.extend('expo-debuggers');
const DEBUG_TYPE = 'expo';
const DEBUG_COMMAND = 'expo.debug.start';
const DEBUG_USER_AGENT = `vscode/${vscode.version} ${process.env.EXTENSION_NAME}/${process.env.EXTENSION_VERSION}`;
export interface ExpoDebugConfig extends vscode.DebugConfiguration {
projectRoot: string;
bundlerHost?: string;
bundlerPort?: string;
// Inherited config from `pwa-node`
trace?: boolean;
restart?: boolean;
enableTurboSourcemaps?: boolean;
}
export class ExpoDebuggersProvider implements vscode.DebugConfigurationProvider {
constructor(
extension: vscode.ExtensionContext,
protected projects: ExpoProjectCache
) {
extension.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(
DEBUG_TYPE,
this,
vscode.DebugConfigurationProviderTriggerKind.Dynamic
)
);
extension.subscriptions.push(
vscode.commands.registerCommand(DEBUG_COMMAND, () => this.onDebugCommand())
);
}
async onDebugCommand() {
let project = await findProjectFromWorkspaces(this.projects);
let projectRelativePath: string | undefined = '';
if (!project) {
projectRelativePath = await vscode.window.showInputBox({
title: 'Expo Debugger',
prompt: 'Enter the path to the Expo project',
value: './',
});
// Abort silently if nothing was entered
if (!projectRelativePath) {
featureTelemetry('command', `${DEBUG_COMMAND}/aborted`, { reason: 'no-path' });
return log('No relative project path entered, aborting...');
}
project = await findProjectFromWorkspaces(this.projects, projectRelativePath);
}
if (!project) {
featureTelemetry('command', `${DEBUG_COMMAND}/aborted`, { reason: 'no-project' });
return vscode.window.showErrorMessage(
projectRelativePath
? `Could not find any Expo projects in: ${projectRelativePath}`
: `Could not find any Expo projects in the current workspaces`
);
}
log('Resolved dynamic project configuration for:', project.root.fsPath);
featureTelemetry('command', DEBUG_COMMAND, {
path: projectRelativePath ? 'nested' : 'workspace',
});
return vscode.debug.startDebugging(undefined, {
type: DEBUG_TYPE,
request: 'attach',
name: 'Inspect Expo app',
projectRoot: project.root.fsPath,
});
}
async provideDebugConfigurations(
workspace?: vscode.WorkspaceFolder,
token?: vscode.CancellationToken
) {
const project = workspace
? await findProjectFromWorkspace(this.projects, workspace)
: await findProjectFromWorkspaces(this.projects);
return [
{
type: DEBUG_TYPE,
request: 'attach',
name: 'Inspect Expo app',
projectRoot: project?.root ?? '${workspaceFolder}',
bundlerHost: '127.0.0.1',
bundlerPort: undefined,
},
];
}
async resolveDebugConfiguration(
workspace: vscode.WorkspaceFolder | undefined,
config: ExpoDebugConfig,
token?: vscode.CancellationToken
) {
if (config.request === 'launch') {
featureTelemetry('debugger', `${DEBUG_TYPE}/aborted`, { reason: 'launch' });
throw new Error(
'Expo debugger does not support launch mode yet. Start the app manually, and connect through `attach`.'
);
}
return {
type: DEBUG_TYPE,
request: 'attach',
name: config.name ?? 'Debug Expo app',
// Pass the user-provided configuration
projectRoot: config.projectRoot ?? '${workspaceFolder}',
bundlerHost: config.bundlerHost,
bundlerPort: config.bundlerPort,
// Enable sourcemaps
sourceMap: true,
pauseForSourceMap: true,
// Enable source-loading for `node_modules`, when using `expo/AppEntry.js`
outFiles: [],
// But disable certain attempts to resolve non-existing source code
resolveSourceMapLocations: [
'!**/__prelude__/**',
'!webpack:**',
'!**/node_modules/react-devtools-core/**',
],
// Disable some internal webpack source-mapping, mostly for React DevTools Backend
sourceMapPathOverrides: {},
// Enable the CDP tracer
trace: config.trace ?? false,
// Attach to whatever processes is running in Hermes
attachExistingChildren: true,
// When Hermes/app or the inspector unexpectedly disconnects, close the debug session
restart: config.restart ?? false,
// Speed up the sourcemap loading, it's kind of experimental in `vscode-js-debug`, but does work fine eiher way
enableTurboSourcemaps: config.enableTurboSourcemaps ?? true,
};
}
async resolveDebugConfigurationWithSubstitutedVariables(
workspace: vscode.WorkspaceFolder | undefined,
config: ExpoDebugConfig,
token?: vscode.CancellationToken
) {
const project = await this.projects.fromRoot(vscode.Uri.file(config.projectRoot));
if (!project) {
featureTelemetry('debugger', `${DEBUG_TYPE}/aborted`, { reason: 'no-project' });
throw new Error('Could not resolve Expo project: ' + config.projectRoot);
}
// Reuse the pwa-node debug adapter from vscode.js-debug
config.type = 'pwa-node';
// Reuse the validated project root as cwd
config.cwd = config.projectRoot;
// Infer the bundler address from project workflow
config.bundlerHost = config.bundlerHost ?? '127.0.0.1';
config.bundlerPort = config.bundlerPort ?? undefined;
// Workaround for Window's drive letter case mismatch with source maps
if (process.platform === 'win32') {
const projectRoot = await resolveWindowsProjectRoot(config.projectRoot);
// Replace the project root for all source maps related paths.
config.cwd = projectRoot;
config.projectRoot = projectRoot;
config.rootPath = projectRoot;
}
// Resolve the target device config to inspect
const { platform, deviceName, ...deviceConfig } = await resolveDeviceConfig(config, project);
featureTelemetry('debugger', `${DEBUG_TYPE}`, {
platform,
deviceName,
expoVersion: project.expoVersion,
});
return { ...config, ...deviceConfig };
}
}
async function resolveDeviceConfig(config: ExpoDebugConfig, project: ExpoProject) {
const device = await waitForDevice(config);
if (!device) {
featureTelemetry('debugger', `${DEBUG_TYPE}/aborted`, { reason: 'device-canceled' });
throw new Error('Expo debug session aborted.');
}
const platform = inferDevicePlatform(device);
return {
platform: platform ?? 'unknown',
deviceName: platform ? undefined : device.deviceName ?? 'unknown',
// The address of the device to connect to
websocketAddress:
device.webSocketDebuggerUrl +
'&type=vscode' + // Adding the "classic" `type=vscode` query parameter (SDK <=49)
`&userAgent=${encodeURIComponent(DEBUG_USER_AGENT)}`, // Adding the modern "userAgent" query parameter (SDK >=50)
// Define the required root paths to resolve source maps
localRoot: project.root,
remoteRoot: `http://${config.bundlerHost}:${config.bundlerPort ?? 8081}`,
};
}
async function waitForDevice(config: ExpoDebugConfig) {
return await vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification, cancellable: true, title: 'Expo debug' },
async (progress, token) => {
progress.report({ message: 'connecting to bundler...' });
while (!token.isCancellationRequested) {
try {
return await pickDevice(config);
} catch (error) {
progress.report({ message: error.message });
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
);
}
async function pickDevice(config: ExpoDebugConfig) {
const bundlerHost = config.bundlerHost ?? '127.0.0.1';
// Either fetch from user-specified port, or try both `19000` and `8081`.
const devices = config.bundlerPort
? await fetchDevicesToInspect({ host: bundlerHost, port: config.bundlerPort }).catch(() => {
throw new Error(`waiting for bundler on ${config.bundlerHost}:${config.bundlerPort}...`);
})
: await fetchDevicesToInspectFromUnknownWorkflow({ host: bundlerHost }).catch(() => {
throw new Error(`waiting for bundler on ${config.bundlerHost}...`);
});
if (devices.length === 1) {
log('Picking only device available:', devices[0].deviceName);
return devices[0];
}
if (devices.length > 1) {
log('Asking user to pick device by name...');
return await askDeviceByName(devices);
}
throw new Error('waiting for device to connect...');
}
/**
* Resolve the "real path" of the project root.
* This workaround is for Window's drive letter casing mismatch within the source maps.
* VS Code seems to prefer lower case drive letters, while Metro use upper case drive letters.
*/
async function resolveWindowsProjectRoot(projectRoot: string) {
if (process.platform !== 'win32') {
return projectRoot;
}
return await new Promise<string>((resolve) => {
fs.realpath.native(projectRoot, (error, realProjectRoot) => {
if (error) {
console.warn(
`Failed to resolve the real path of the project root, this may break the breakpoints functionality.`
);
console.warn(`Reason: ${error.message} (${error.code})`);
console.warn(
`If you run into this issue often, please report it at: https://github.com/expo/vscode-expo/issues/new/choose`
);
resolve(projectRoot);
} else {
resolve(realProjectRoot);
}
});
});
}