Skip to content

Commit b84470b

Browse files
authored
Fix attach request for different metro ports (#2007)
* Fix packager port issue * Fix attach request for different metro ports * Fix issues * Update * Update
1 parent 50a7f6e commit b84470b

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

.vscode/launch.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"runtimeExecutable": "${execPath}",
1919
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
2020
"sourceMaps": true,
21-
"outFiles": ["${workspaceRoot}/src/**/*.js"],
22-
"preLaunchTask": "gulp: quick-build"
21+
"outFiles": ["${workspaceRoot}/src/**/*.js"]
2322
},
2423
{
2524
"name": "Launch Extension tests",

src/cdp-proxy/debuggerEndpointHelper.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@ export class DebuggerEndpointHelper {
3636
attemptNumber: number,
3737
cancellationToken: CancellationToken,
3838
isHermes: boolean = false,
39+
settingsPort?: number,
3940
): Promise<string> {
4041
while (true) {
4142
try {
42-
return await this.getWSEndpoint(browserURL, isHermes);
43+
let url = "";
44+
if (settingsPort) {
45+
url = `http://localhost:${settingsPort}`;
46+
try {
47+
return await this.getWSEndpoint(browserURL, isHermes);
48+
} catch {
49+
return await this.getWSEndpoint(url, isHermes);
50+
}
51+
} else {
52+
return await this.getWSEndpoint(browserURL, isHermes);
53+
}
4354
} catch (err) {
4455
if (attemptNumber < 1 || cancellationToken.isCancellationRequested) {
4556
const internalError = ErrorHelper.getInternalError(
@@ -85,6 +96,15 @@ export class DebuggerEndpointHelper {
8596
? this.tryToGetHermesImprovedChromeReloadsWebSocketDebuggerUrl(jsonList)
8697
: jsonList[0].webSocketDebuggerUrl;
8798
}
99+
// Try to get websocket endpoint from default metro bundler
100+
const defaultJsonList = await this.fetchJson<DebuggableEndpointData[]>(
101+
"http://localhost:8081/json/list",
102+
);
103+
if (defaultJsonList.length) {
104+
return isHermes
105+
? this.tryToGetHermesImprovedChromeReloadsWebSocketDebuggerUrl(defaultJsonList)
106+
: defaultJsonList[0].webSocketDebuggerUrl;
107+
}
88108

89109
throw new Error("Could not find any debuggable target");
90110
}

src/common/packager.ts

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export class Packager {
100100
return this.packagerPort || SettingsHelper.getPackagerPort(this.workspacePath);
101101
}
102102

103+
public resetToSettingsPort(): void {
104+
this.packagerPort = SettingsHelper.getPackagerPort(this.workspacePath);
105+
}
106+
103107
public setRunOptions(runOptions: IRunOptions): void {
104108
this.runOptions = runOptions;
105109
}
@@ -111,6 +115,7 @@ export class Packager {
111115
public getStatusIndicator(): PackagerStatusIndicator {
112116
return this.packagerStatusIndicator;
113117
}
118+
114119
public getHost(): string {
115120
return Packager.getHostForPort(this.getPort());
116121
}

src/debugger/debugSessionBase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ export abstract class DebugSessionBase extends LoggingDebugSession {
185185
this.vsCodeDebugSession.workspaceFolder.uri.fsPath,
186186
);
187187
}
188+
const settingsPort = this.appLauncher.getPackagerPort(projectRootPath);
189+
if (this.appLauncher.getPackager().getPort() != settingsPort) {
190+
this.appLauncher.getPackager().resetToSettingsPort();
191+
}
188192
}
189193
}
190194

src/debugger/direct/directDebugSession.ts

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCD
2424
import { TipNotificationService } from "../../extension/services/tipsNotificationsService/tipsNotificationService";
2525
import { RNSession } from "../debugSessionWrapper";
2626
import { IWDPHelper } from "./IWDPHelper";
27+
import { SettingsHelper } from "../../extension/settingsHelper";
2728

2829
nls.config({
2930
messageFormat: nls.MessageFormat.bundle,
@@ -243,11 +244,13 @@ export class DirectDebugSession extends DebugSessionBase {
243244
}
244245
});
245246

247+
const settingsPorts = SettingsHelper.getPackagerPort(attachArgs.cwd);
246248
const browserInspectUri = await this.debuggerEndpointHelper.retryGetWSEndpoint(
247249
`http://localhost:${attachArgs.port}`,
248250
90,
249251
this.cancellationTokenSource.token,
250252
attachArgs.useHermesEngine,
253+
settingsPorts,
251254
);
252255
this.appLauncher.getRnCdpProxy().setBrowserInspectUri(browserInspectUri);
253256
await this.establishDebugSession(attachArgs);

0 commit comments

Comments
 (0)