Skip to content

Commit

Permalink
changes after CR
Browse files Browse the repository at this point in the history
  • Loading branch information
filip131311 committed Oct 17, 2024
1 parent e2cbc1d commit 900205f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
15 changes: 9 additions & 6 deletions packages/vscode-extension/lib/metro_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ function adaptMetroConfig(config) {
module.output[0].data.code = `${preludeCode};var __REACT_DEVTOOLS_PORT__=${process.env.RCT_DEVTOOLS_PORT};`;
}
} else if (module.path === "__env__") {
// this handles @expo/env plugin, which is used to expose the number of lines in the prelude.
// This is used to calculate the line number offset when reporting line numbers for
// console.logs, breakpoints and uncaught exceptions. The reason why this is needed, is that
// this handles @expo/env plugin, which is used to inject environment variables
// the code below instantiates a global variable __EXPO_ENV_PRELUDE_LINES__ that stores
// the number of lines in the prelude. This is used to calculate the line number offset
// when reporting line numbers from the JS runtime, breakpoints
// and uncaught exceptions. The reason why this is needed, is that
// metro doesn't include __env__ prelude in the source map resulting in the source map
// transformation getting shifted by the number of lines in the prelude.
const expoEnvPreludeLines = module.output[0].data.lineCount;
process.stdout.write(JSON.stringify({ type: "RNIDE_Expo_Env_Prelude_Lines", expoEnvPreludeLines }));
process.stdout.write("\n");
const expoEnvCode = module.output[0].data.code;
if (!expoEnvCode.includes("__EXPO_ENV_PRELUDE_LINES__")) {
module.output[0].data.code = `${expoEnvCode};var __EXPO_ENV_PRELUDE_LINES__=${module.output[0].data.lineCount};`;
}
}
return origProcessModuleFilter(module);
};
Expand Down
11 changes: 7 additions & 4 deletions packages/vscode-extension/src/debugging/DebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ export class DebugAdapter extends DebugSession {
private projectPathAlias?: string;
private threads: Array<Thread> = [];
private sourceMaps: Array<[string, string, SourceMapConsumer, number]> = [];
private lineOffset: number;

private linesStartAt1 = true;
private columnsStartAt1 = true;

Expand All @@ -99,7 +97,6 @@ export class DebugAdapter extends DebugSession {
this.absoluteProjectPath = configuration.absoluteProjectPath;
this.projectPathAlias = configuration.projectPathAlias;
this.connection = new WebSocket(configuration.websocketAddress);
this.lineOffset = configuration.lineOffset;

this.connection.on("open", () => {
// the below catch handler is used to ignore errors coming from non critical CDP messages we
Expand Down Expand Up @@ -155,6 +152,10 @@ export class DebugAdapter extends DebugSession {
const sourceMap = JSON.parse(decodedData);
const consumer = await new SourceMapConsumer(sourceMap);

const sourceFile = await (await fetch(message.params.url)).text();
const lineOffsetRegex = /__EXPO_ENV_PRELUDE_LINES__=(\d+);/;
const match = sourceFile.match(lineOffsetRegex);
const lineOffset = Number(match ? match[1] : null);
// This line is here because of a problem with sourcemaps for expo projects,
// that was addressed in this PR https://github.com/expo/expo/pull/29463,
// unfortunately it still requires changes to metro that were attempted here
Expand All @@ -164,11 +165,13 @@ export class DebugAdapter extends DebugSession {
// (generated during reload) do not contain the prelude causing the issue
const shouldApplyOffset =
semver.lte(getReactNativeVersion(), "0.76.0") && this.sourceMaps.length === 0;
Logger.debug("Expo prelude lines were detected and an offset was set to:", lineOffset);

this.sourceMaps.push([
message.params.url,
message.params.scriptId,
consumer,
shouldApplyOffset ? this.lineOffset : 0,
shouldApplyOffset ? lineOffset : 0,
]);
this.updateBreakpointsInSource(message.params.url, consumer);
}
Expand Down
1 change: 0 additions & 1 deletion packages/vscode-extension/src/debugging/DebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class DebugSession implements Disposable {
websocketAddress: websocketAddress,
absoluteProjectPath: getAppRootFolder(),
projectPathAlias: this.metro.isUsingNewDebugger ? "/[metro-project]" : undefined,
lineOffset: this.metro.initialBundleLineOffset,
},
{
suppressDebugStatusbar: true,
Expand Down
15 changes: 0 additions & 15 deletions packages/vscode-extension/src/project/metro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ type MetroEvent =
transformedFileCount: number;
totalFileCount: number;
}
| {
type: "RNIDE_Expo_Env_Prelude_Lines";
expoEnvPreludeLines: number;
}
| {
type: "RNIDE_initialize_done";
port: number;
Expand All @@ -68,7 +64,6 @@ type MetroEvent =
export class Metro implements Disposable {
private subprocess?: ChildProcess;
private _port = 0;
private _initialBundleLineOffset = 0;
private startPromise: Promise<void> | undefined;
private usesNewDebugger?: Boolean;

Expand All @@ -85,10 +80,6 @@ export class Metro implements Disposable {
return this._port;
}

public get initialBundleLineOffset() {
return this._initialBundleLineOffset;
}

public dispose() {
this.subprocess?.kill(9);
}
Expand Down Expand Up @@ -216,12 +207,6 @@ export class Metro implements Disposable {
}

switch (event.type) {
case "RNIDE_Expo_Env_Prelude_Lines":
this._initialBundleLineOffset = event.expoEnvPreludeLines;
Logger.debug(
`Metro initial bundle offset is set to ${this._initialBundleLineOffset}`
);
break;
case "RNIDE_initialize_done":
this._port = event.port;
Logger.info(`Metro started on port ${this._port}`);
Expand Down

0 comments on commit 900205f

Please sign in to comment.