Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misaligned lines in debugger #634

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

filip131311
Copy link
Collaborator

@filip131311 filip131311 commented Oct 16, 2024

This PR fixes a problem with debugger on expo projects, caused by prelude lines added to the entry bundle file added by expo. The solution is t0 add the required offset to the prelude causing the problem and scanning for it when receiving source map in the debuger.

It is possible that the changes will no longer be needed in future versions of react native as the expo team tries to correct source map generation to include extra lines, for more information read those PRs:

This is why we add version check before adding lineOffset to initial source map.

How Has This Been Tested:

  • open expo project and set a breakpoint, before the changes it would trigger 2 lines above the place it's been set.
  • save any change in the file containing breakpoint and make sure it still works.
  • check if links to files generated next to console logs are pointing in to the correct place, again both before and after making changes to the file.
  • check if error position indicator (when uncaught exception is raised) points to the correct position.

Additional changes:

This PR also fixes a minor mistake with getReactNativeVersion utility that does not need to be async and as it is used, we make it synchronous as part of this PR.

Copy link

vercel bot commented Oct 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
radon-ide ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2024 11:16am

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the coupling between console calls that are also meant to provide the offset and the source map parser that seems to expect the console call prior to the "source maps ready" call.

I think we should instead restructure it in the following way:

  1. Remove __EXPO_ENV_PRELUDE_LINES__ entirely
  2. Instead of it, we should log a command from metro process that will be captured by line reader. Similarily to how we do with RNIDE_initialize_done, but it will also contain the offset number
  3. Read the offset in metro.ts using line reader and expose it such that we can provide it as a parameter to the debug session

@filip131311
Copy link
Collaborator Author

@kmagiera you were right, I changed it according to your suggestions.

module.output[0].data.code = `${expoEnvCode};var __EXPO_ENV_PRELUDE_LINES__=${module.output[0].data.lineCount};`;
}
const expoEnvPreludeLines = module.output[0].data.lineCount;
process.stdout.write(JSON.stringify({ type: "RNIDE_Expo_Env_Prelude_Lines", expoEnvPreludeLines }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with RNIDE_initialize_done I wouldn't use capitalized words here

@@ -64,6 +68,7 @@ type MetroEvent =
export class Metro implements Disposable {
private subprocess?: ChildProcess;
private _port = 0;
private _initialBundleLineOffset = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like "initial" word here as it isn't accurate. I think the offset applies for the whole bundle not just for the first bundle that gets loaded as it is currently implemented.

We should rename "mainBundleLineOffset"

@kmagiera
Copy link
Member

I'm now thinking that it may actually be the easiest solution to still include the offset in the bundle, and when we get source map we'd check if the offset variable exists there and read its value. If this would work, that'd be the best solution in my option. The only reason why it may not work is that I'm not sure if the preamble actually gets included in the source map at all. Tthe reason we're adding this offset was that there was an issue with that. But, perhaps, the issue was only because it is considered to have 0 length rather than just being completely omitted. You'll need to check the bundle for that to tell

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few more comments inline, but since only related to variable/parameter names and comments I'm accepting, as otherwise the code looks good.
The only comment that may require more attention is about whether we should test agains expo or react native version when determining when to use offset parameter and when not


// 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__");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename isMainBundle or isFullBundle

Comment on lines +157 to +158
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
// We detect when a source map for the entire bundle is loaded by checking
// if __env__ module is present in the sources.

@@ -96,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter should really be called expoPreludeExtraLines or something along these lines instead of being so generic

// 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") && isFileWithOffset;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check for expo version here and not react native?

Comment on lines +161 to +165
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.
// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants