From 1d72d6701ad13c458610c8c8c51401ff684e6d70 Mon Sep 17 00:00:00 2001 From: filip131311 Date: Wed, 16 Oct 2024 21:51:44 +0200 Subject: [PATCH] init --- packages/vscode-extension/lib/runtime.js | 2 +- .../src/builders/buildAndroid.ts | 2 +- .../src/debugging/DebugAdapter.ts | 42 ++++++++++++++----- .../src/utilities/reactNative.ts | 2 +- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/vscode-extension/lib/runtime.js b/packages/vscode-extension/lib/runtime.js index cfee23dd5..d749f1719 100644 --- a/packages/vscode-extension/lib/runtime.js +++ b/packages/vscode-extension/lib/runtime.js @@ -26,7 +26,7 @@ function wrapConsole(consoleFunc) { const expoLogIndex = stack.findIndex((frame) => frame.methodName === "__expoConsoleLog"); const location = expoLogIndex > 0 ? stack[expoLogIndex + 1] : stack[1]; const lineOffset = global.__EXPO_ENV_PRELUDE_LINES__ || 0; - args.push(location.file, location.lineNumber - lineOffset, location.column); + args.push(lineOffset, location.file, location.lineNumber, location.column); return consoleFunc.apply(console, args); }; } diff --git a/packages/vscode-extension/src/builders/buildAndroid.ts b/packages/vscode-extension/src/builders/buildAndroid.ts index 4a0af62f2..6f8e49b86 100644 --- a/packages/vscode-extension/src/builders/buildAndroid.ts +++ b/packages/vscode-extension/src/builders/buildAndroid.ts @@ -143,7 +143,7 @@ export async function buildAndroid( ), ]; // configureReactNativeOverrides init script is only necessary for RN versions older then 0.74.0 see comments in configureReactNativeOverrides.gradle for more details - if (semver.lt(await getReactNativeVersion(), "0.74.0")) { + if (semver.lt(getReactNativeVersion(), "0.74.0")) { gradleArgs.push( "--init-script", // configureReactNativeOverrides init script is used to patch React Android project, see comments in configureReactNativeOverrides.gradle for more details path.join( diff --git a/packages/vscode-extension/src/debugging/DebugAdapter.ts b/packages/vscode-extension/src/debugging/DebugAdapter.ts index abbc2838b..3820764f2 100644 --- a/packages/vscode-extension/src/debugging/DebugAdapter.ts +++ b/packages/vscode-extension/src/debugging/DebugAdapter.ts @@ -14,6 +14,8 @@ import { Source, StackFrame, } from "@vscode/debugadapter"; +import { getReactNativeVersion } from "../utilities/reactNative"; +import semver from "semver"; import { DebugProtocol } from "@vscode/debugprotocol"; import WebSocket from "ws"; import { NullablePosition, SourceMapConsumer } from "source-map"; @@ -83,7 +85,8 @@ export class DebugAdapter extends DebugSession { private absoluteProjectPath: string; private projectPathAlias?: string; private threads: Array = []; - private sourceMaps: Array<[string, string, SourceMapConsumer]> = []; + private sourceMaps: Array<[string, string, SourceMapConsumer, number]> = []; + private lineOffset: number = 0; private linesStartAt1 = true; private columnsStartAt1 = true; @@ -150,7 +153,21 @@ export class DebugAdapter extends DebugSession { const decodedData = Buffer.from(base64Data, "base64").toString("utf-8"); const sourceMap = JSON.parse(decodedData); const consumer = await new SourceMapConsumer(sourceMap); - this.sourceMaps.push([message.params.url, message.params.scriptId, consumer]); + + // 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. + // more over offset should only be applied to the main bundle sourcemap as other files (generated during reload) + // do not contain the prelude causing the issue + const shouldApplyOffset = + semver.lte(getReactNativeVersion(), "0.76.0") && this.sourceMaps.length === 0; + this.sourceMaps.push([ + message.params.url, + message.params.scriptId, + consumer, + shouldApplyOffset ? this.lineOffset : 0, + ]); this.updateBreakpointsInSource(message.params.url, consumer); } @@ -186,9 +203,12 @@ export class DebugAdapter extends DebugSession { if (argsLen > 3 && message.params.args[argsLen - 1].type === "number") { // Since console.log stack is extracted from Error, unlike other messages sent over CDP // the line and column numbers are 1-based - const [scriptURL, generatedLineNumber1Based, generatedColumn1Based] = message.params.args - .slice(-3) - .map((v: any) => v.value); + const [lineOffset, scriptURL, generatedLineNumber1Based, generatedColumn1Based] = + message.params.args.slice(-4).map((v: any) => v.value); + + if (this.lineOffset !== lineOffset) { + this.lineOffset = lineOffset; + } const { lineNumber1Based, columnNumber0Based, sourceURL } = this.findOriginalPosition( scriptURL, @@ -196,10 +216,10 @@ export class DebugAdapter extends DebugSession { generatedColumn1Based - 1 ); - const variablesRefDapID = this.createVariableForOutputEvent(message.params.args.slice(0, -3)); + const variablesRefDapID = this.createVariableForOutputEvent(message.params.args.slice(0, -4)); output = new OutputEvent( - (await formatMessage(message.params.args.slice(0, -3))) + "\n", + (await formatMessage(message.params.args.slice(0, -4))) + "\n", typeToCategory(message.params.type) ); output.body = { @@ -264,7 +284,7 @@ export class DebugAdapter extends DebugSession { let sourceLine1Based = lineNumber1Based; let sourceColumn0Based = columnNumber0Based; - this.sourceMaps.forEach(([url, id, consumer]) => { + this.sourceMaps.forEach(([url, id, consumer, lineOffset]) => { // when we identify script by its URL we need to deal with a situation when the URL is sent with a different // hostname and port than the one we have registered in the source maps. The reason for that is that the request // that populates the source map (scriptParsed) is sent by metro, while the requests from breakpoints or logs @@ -273,7 +293,7 @@ export class DebugAdapter extends DebugSession { if (id === scriptIdOrURL || compareIgnoringHost(url, scriptIdOrURL)) { scriptURL = url; const pos = consumer.originalPositionFor({ - line: lineNumber1Based, + line: lineNumber1Based - lineOffset, column: columnNumber0Based, }); if (pos.source != null) { @@ -440,7 +460,7 @@ export class DebugAdapter extends DebugSession { } let position: NullablePosition = { line: null, column: null, lastColumn: null }; let originalSourceURL: string = ""; - this.sourceMaps.forEach(([sourceURL, scriptId, consumer]) => { + this.sourceMaps.forEach(([sourceURL, scriptId, consumer, lineOffset]) => { const sources = []; consumer.eachMapping((mapping) => { sources.push(mapping.source); @@ -453,7 +473,7 @@ export class DebugAdapter extends DebugSession { }); if (pos.line != null) { originalSourceURL = sourceURL; - position = pos; + position = { ...pos, line: pos.line + lineOffset }; } }); if (position.line === null) { diff --git a/packages/vscode-extension/src/utilities/reactNative.ts b/packages/vscode-extension/src/utilities/reactNative.ts index 5e4e75d80..c2c64cf80 100644 --- a/packages/vscode-extension/src/utilities/reactNative.ts +++ b/packages/vscode-extension/src/utilities/reactNative.ts @@ -1,7 +1,7 @@ import path from "path"; import { getAppRootFolder } from "./extensionContext"; -export async function getReactNativeVersion() { +export function getReactNativeVersion() { const workspacePath = getAppRootFolder(); const reactNativeRoot = path.dirname(require.resolve("react-native", { paths: [workspacePath] })); const packageJsonPath = path.join(reactNativeRoot, "package.json");