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

[babel-generator] Generated code doesn’t match with source with input source-maps #5408

Closed
alloy opened this issue Mar 3, 2017 · 16 comments · Fixed by #15022
Closed

[babel-generator] Generated code doesn’t match with source with input source-maps #5408

alloy opened this issue Mar 3, 2017 · 16 comments · Fixed by #15022
Labels
area: sourcemaps i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator

Comments

@alloy
Copy link

alloy commented Mar 3, 2017

Input Code

I’ve put up a repo here

The relevant code is here

Expected Behavior

I would have expected the source-map from babel-generator to map from the original TypeScript source to the generated JS.

Current Behavior

The source-map doesn’t appear to have any relation to the original code.

Here are some examples of the code looked-up through the source-maps:

 Original: import * as React fr
Transform: var React=require("
 Generate: "use strict";

 Original: function MyComponent
Transform: function MyComponen
 Generate:  require("react");

As you can see, the transform step does a good job of mapping to the original code, but the generate step just produces something completely different.

Context

I’m trying to use TypeScript for our React Native project and have inline source-maps propagate properly through Babel. I would prefer to not write the intermediate JS (produced by tsc) to disk.

I’m not sure if I’m doing something wrong, if this has to do with how React Native uses transform and the generator, or if there’s a bug with babel-generator.

Your Environment

software version
Babel 6.23.1
node 6.2.1
npm 4.1.1
Operating System macOS 10.12.2
@babel-bot
Copy link
Collaborator

Hey @alloy! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@loganfsmyth
Copy link
Member

Assuming I'm understanding fully, I think this may be the expected, if frustrating, behavior. babel-generator outputs a sourcemap that maps to the sourcecode that was parsed to create the AST and does not take input sourcemaps into account. Currently they are merged inside babel-core in https://github.com/babel/babel/blob/7.0/packages/babel-core/src/transformation/file/index.js#L364, so if you want to generate the code from the AST yourself, you'd have to reimplement that merging logic.

I could see the benefit of moving this logic, so I'm happy to have this as a feature request if you'd like. Not sure it would be a high priority for the core team to implement though.

alloy added a commit to alloy/test-source-maps-with-typescript-through-babel that referenced this issue Mar 3, 2017
@alloy
Copy link
Author

alloy commented Mar 3, 2017

Ah yes! This works: alloy/test-source-maps-with-typescript-through-babel@a2982c8

I was stepping through all the code looking for when the input and output maps of the generated code got merged and could only find it indeed in the transform step, glad I decided to ask.

So if I understand you correctly you’re suggesting to extract mergeSourceMap into a standalone utility function? If so, could you just point me to where you would want to have it? I’ll try to make a PR for it.

@loganfsmyth
Copy link
Member

I think it would be reasonable to move mergeSourceMap into babel-generator so people could more easily do what you are trying to do.

@loganfsmyth
Copy link
Member

A similar issue was just raised in aspnet/JavaScriptServices#739 (comment). In that usecase, people are expecting the AST locations to take the original sourcemap locations into account.

Given that, an alternative would be to actually delete mergeSourceMap entirely and have either babylon or babel-core ensure that the AST locations map to the original file content, so that the final map generated by babel-generator, and Babel's output AST, would have the correct position data already.

I'm fine with that as a solution as long as there's no loss of information.

@dan-j
Copy link

dan-j commented Jul 21, 2017

I think I came across a similar issue while trying to write a jest preprocessor for TS -> Babel and getting code coverage right.

I ended up not passing inputSourceMap into babel, and using source-map-merge to merge the source maps outputted by the TS compiler and babel respectively.

Perhaps babel should use this module for merging input source maps rather than it's own merge code? From debugging the issue before I resorted to source-map-merge the merge code inside babel is here:

https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/file/index.js#L368

@DrMiaow
Copy link

DrMiaow commented Nov 9, 2017

+1 for resolving this please.

My issue is that Babel is not merging with source-maps from previous steps in my transformation chain, it appears to simply generate its own source-map from the input, which seems to be the problem everyone else is having.

My workaround, for now, is to set retainLines: true, sourceMap: false and after transforming using Babel I copy the *.map files from the previous step to feed into the next step.

Not ideal, but does the job.

@PabloZaiden
Copy link

Any news on this?

@fbartho
Copy link

fbartho commented Jul 25, 2019

We're super invested in this, because it means that our bugsnags don't map back to the original TypeScript lines in our react-native source code :(

@fbartho
Copy link

fbartho commented Feb 7, 2020

Is there any way to solve this? A work around?

@dlebedynskyi
Copy link

Any update on this issue?

@dan-j
Copy link

dan-j commented Aug 31, 2021

Any update on this issue?

I doubt there is, I've no need for Babel in my current role and won't find the time to contribute (I just got the notification because I've previously commented).

If nothing much has changed in 4 years, my comment is my suggestion on fixing the issue: #5408 (comment), however I have no real idea whether or not that comment is applicable anymore.

If a maintainer can comment and approve of the idea then it may be worth you implementing the fix and contributing it back?

@shaunlebron
Copy link

Hello, my employer has asked me to help solve this, if possible.

We require this feature downstream in Metro (parallel issue here).

Can a maintainer answer some questions?

  1. Does this issue imply that inputSourceMap: true isn’t actually working?
  2. Is PR #12471 intended to fix this?
  3. Is it possible for an outsider to help with this, or to catch up on domain knowledge to help?

Thank you. Hoping we can help solve this five-year-old issue!

@liuxingbaoyu
Copy link
Member

liuxingbaoyu commented Oct 6, 2022

Yes, inputSourceMap is currently invalid in babel-generator.
#12471 I'm not sure if that solves your problem.
Exactly this is in my plan, but I can't promise.

In addition, in theory babel-core can complete source map merging very well, can you use that?

ref: #15022 (comment)

@shaunlebron
Copy link

Thank you @liuxingbaoyu! Are you saying babel-core already does correct source map merging with inputSourceMap: true? (I actually don’t see Metro passing this option to babel-core in their repo…)

@liuxingbaoyu
Copy link
Member

Yes, as in the OP's reproduced code. babel/core is merging source maps. So this is just an enhancement and shouldn't be blocking.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 21, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: sourcemaps i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator
Projects
None yet
Development

Successfully merging a pull request may close this issue.