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

Merged
merged 12 commits into from
Oct 22, 2024

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.

@filip131311 filip131311 requested a review from kmagiera October 16, 2024 20:11
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 22, 2024 9:51am

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

packages/vscode-extension/src/debugging/DebugAdapter.ts Outdated Show resolved Hide resolved
@filip131311
Copy link
Collaborator Author

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

@filip131311 filip131311 requested a review from kmagiera October 17, 2024 10:21
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"

packages/vscode-extension/src/debugging/DebugAdapter.ts Outdated Show resolved Hide resolved
@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

packages/vscode-extension/src/debugging/DebugAdapter.ts Outdated Show resolved Hide resolved
@@ -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?

packages/vscode-extension/src/debugging/DebugAdapter.ts Outdated Show resolved Hide resolved
@kmagiera kmagiera merged commit 1023dc7 into main Oct 22, 2024
3 checks passed
@kmagiera kmagiera deleted the @Filip131311/FixDebuggerMissalinedLinesOnExpoProjects branch October 22, 2024 09:51
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