Skip to content

Commit

Permalink
move to passing informatoion througsh metro
Browse files Browse the repository at this point in the history
  • Loading branch information
filip131311 committed Oct 18, 2024
1 parent 4f37c39 commit e590d62
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 5 additions & 7 deletions packages/vscode-extension/lib/metro_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ function adaptMetroConfig(config) {
}
} else if (module.path === "__env__") {
// 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
// the code below exposes 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 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};`;
}
// transformation getting shifted by the number of lines in the expo generated prelude.
process.stdout.write(JSON.stringify({ type: "RNIDE_expo_env_prelude_lines", lineOffset: module.output[0].data.lineCount }));
process.stdout.write("\n");
}
return origProcessModuleFilter(module);
};
Expand Down
19 changes: 11 additions & 8 deletions packages/vscode-extension/src/debugging/DebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ 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 @@ -97,6 +98,7 @@ 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 @@ -152,28 +154,29 @@ 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 is a heuristic that checks if the source map should contain __env__
// module that is added by expo, but not reported in the source map
const isFileWithOffset = sourceMap.sources.includes("__prelude__");

// 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
// https://github.com/facebook/metro/pull/1284 we should monitor the situation
// in upcoming versions and if the changes are still not added bump the version below.
const shouldApplyOffset = semver.lte(getReactNativeVersion(), "0.76.0");
if (lineOffset && shouldApplyOffset) {
const shouldApplyOffset =
semver.lte(getReactNativeVersion(), "0.76.0") && isFileWithOffset;
if (this.lineOffset !== 0 && shouldApplyOffset) {
Logger.debug(
"Expo prelude lines were detected and an offset was set to:",
lineOffset
this.lineOffset
);
}

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

constructor(private readonly devtools: Devtools, private readonly delegate: MetroDelegate) {}

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

public get expoPreludeLineOffset() {
return this._expoPreludeLineOffset;
}

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

switch (event.type) {
case "RNIDE_expo_env_prelude_lines":
this._expoPreludeLineOffset = event.lineOffset;
Logger.debug("Expo prelude line offset was set to: ", this._expoPreludeLineOffset);
break;
case "RNIDE_initialize_done":
this._port = event.port;
Logger.info(`Metro started on port ${this._port}`);
Expand Down

0 comments on commit e590d62

Please sign in to comment.