Skip to content

Commit

Permalink
Small adjustments of the code and conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera committed Oct 22, 2024
1 parent fb61f3c commit d28b796
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
7 changes: 6 additions & 1 deletion packages/vscode-extension/lib/metro_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ function adaptMetroConfig(config) {
// 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 expo generated prelude.
process.stdout.write(JSON.stringify({ type: "RNIDE_expo_env_prelude_lines", lineOffset: module.output[0].data.lineCount }));
process.stdout.write(
JSON.stringify({
type: "RNIDE_expo_env_prelude_lines",
lineCount: module.output[0].data.lineCount,
})
);
process.stdout.write("\n");
}
return origProcessModuleFilter(module);
Expand Down
31 changes: 15 additions & 16 deletions packages/vscode-extension/src/debugging/DebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +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 expoPreludeLineCount: number;
private linesStartAt1 = true;
private columnsStartAt1 = true;

Expand All @@ -98,7 +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.expoPreludeLineCount = configuration.expoPreludeLineCount;

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

// We detect when a source map for the entire bundle is loaded by checking
// if __env__ module is present in the sources.
const isFileWithOffset = sourceMap.sources.includes("__prelude__");

// When using expo <${PUT_VERSION_HERE}, source maps skip the prelude module which is
// included in the main bundle at the start. As a result, all lines in the main bundle are shifted by
// the amount of lines the prelude file adds. To offset this, we use the lineOffset parameter that we pass
// to the source maps list that is used later on to correct line numbers when translating from generated
// to the original positions.
const shouldApplyOffset =
semver.lte(getReactNativeVersion(), "0.76.0") && isFileWithOffset;
if (this.lineOffset !== 0 && shouldApplyOffset) {
// We detect when a source map for the entire bundle is loaded by checking if __prelude__ module is present in the sources.
const isMainBundle = sourceMap.sources.includes("__prelude__");

// Expo env plugin has a bug that causes the bundle to include so-called expo prelude module named __env__
// which is not present in the source map. As a result, the line numbers are shifted by the amount of lines
// the __env__ module adds. If we detect that main bundle is loaded, but __env__ is not there, we use the provided
// expoPreludeLineCount which reflects the number of lines in __env__ module to offset the line numbers in the source map.
const bundleContainsExpoPrelude = sourceMap.sources.includes("__env__");
let lineOffset = 0;
if (isMainBundle && !bundleContainsExpoPrelude) {
Logger.debug(
"Expo prelude lines were detected and an offset was set to:",
this.lineOffset
this.expoPreludeLineCount
);
lineOffset = this.expoPreludeLineCount;
}

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

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

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

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

public dispose() {
Expand Down Expand Up @@ -214,8 +214,8 @@ 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);
this._expoPreludeLineCount = event.lineCount;
Logger.debug("Expo prelude line offset was set to: ", this._expoPreludeLineCount);
break;
case "RNIDE_initialize_done":
this._port = event.port;
Expand Down

0 comments on commit d28b796

Please sign in to comment.