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

inline sourcemaps and cache API cause incorrect sourcemap output #3465

Closed
jbedard opened this issue Mar 28, 2020 · 20 comments · Fixed by #3476
Closed

inline sourcemaps and cache API cause incorrect sourcemap output #3465

jbedard opened this issue Mar 28, 2020 · 20 comments · Fixed by #3476

Comments

@jbedard
Copy link
Contributor

jbedard commented Mar 28, 2020

  • Rollup Version: 2.2.0, pretty sure it also occured in 1.3
  • Operating System (or Browser): many via CI

Reproduction (can create a git repo if wanted, but this is pretty simple):

Given the following inputs:

index.js

import value from "./other";
console.log(value);

other.js (compiled from a ts file that used const and had x: string)

var x = "foo";
export default x;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3RoZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJvdGhlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFNLENBQUMsR0FBVyxLQUFLLENBQUM7QUFDeEIsZUFBZSxDQUFDLENBQUMifQ==

Then run rollup via the API twice, the second time with the cache of the first. This is essentially the same as https://rollupjs.org/guide/en/#cache

const rollup = require('rollup');

let cache;
let runNum = 1;
function run() {
    return rollup.rollup({cache, input: ["./index.js"]});
}
function write(bundle) {
    cache = bundle.cache;
    return bundle.write({file: `out/run-${runNum++}.js`})
}

Promise.resolve()
    .then(run).then(write)
    .then(run).then(write);

Expected Behavior

The same output each time (a single .js.map file alongside the bundle). Hopefully the cache makes it faster.

Actual Behavior

The //# sourceMappingURL=data: correctly gets merged into the outputted run-1.js.map on the first run. Then it remains within the code (run-2.js) on the second run (and run-2.js.map is a tiny amount larger then the first run, so it's probably still merged into there).

@jbedard
Copy link
Contributor Author

jbedard commented Mar 30, 2020

Here are a few tests reproducing it: jbedard@5765190

@jbedard
Copy link
Contributor Author

jbedard commented Mar 30, 2020

Note this also happens when using --watch, so the CLI usage of this API has the same bug.

@lukastaegert
Copy link
Member

Here are a few tests reproducing it: jbedard/rollup@5765190

Your test does not appear to use the cache API, am I missing something here?

So one thing to note is that Rollup does NOT pick up inlined or external sourcemaps by default to merge them into the final sourcemap. If you want something like this, you may want e.g. https://github.com/maxdavidson/rollup-plugin-sourcemaps

Beyond that it sounds like your actual problem is that the inline sourcemap is not removed and thus causes issues, is that correct?

@lukastaegert
Copy link
Member

But I can definitely confirm that the sourcemap is only removed on the first run and not on the second, so it appears the existing logic to remove sourcemaps is not triggered when using the cache. We definitely need to fix this!

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

Your test does not appear to use the cache API, am I missing something here?

Every test in the sourcemaps examples is run twice using the cache, ironically to make sure you get the same result each time :)

@lukastaegert
Copy link
Member

Ah, nicely spotted!

@lukastaegert
Copy link
Member

lukastaegert commented Mar 31, 2020

Tentative fix at #3476

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

Thanks!

Worth adding jbedard@5765190 as well?

@lukastaegert
Copy link
Member

Sure, will try to merge it

@lukastaegert
Copy link
Member

The test description is wrong, though, as the inline sourcemaps are not combined. They are just removed. See my comment above.

@lukastaegert
Copy link
Member

And we will need to switch to asserts that work in Node 10...

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

The test description is wrong, though, as the inline sourcemaps are not combined

I'm confused... the tests are outputting sourcemaps though (there are .js.map files in the _action output folder). Doesn't the outputOptions.sourcemap = true output sourcemaps?

@lukastaegert
Copy link
Member

lukastaegert commented Mar 31, 2020

Yes it does. But the sourcemaps only take Rollup's transformations + any plugin transformations into account. Pre-existing sourcemaps are ignored by default — unless you add a plugin that changes that. The reasoning is that developers are usually only interested in sourcemaps for their own code, and plugins take care of that. Pre-existing sourcemaps usually come with third-party libraries where you are usually not that interested in detailed original code locations, especially if you do not have this code available anyway. Also, obeying pre-existing sourcemaps comes at a performance cost.

@lukastaegert
Copy link
Member

I removed the test about external sourcemaps because it really does not test anything interesting IMO, and change the description and asserts of the other test.

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

the sourcemaps only take Rollups transformations + any plugin transformations into account. Pre-existing sourcemaps are ignored by default — unless you add a plugin that changes that

I see, thanks!

This look right? jbedard@3e18939

@lukastaegert
Copy link
Member

Yes. But the two tests now basically test the same thing, so I would remove one.

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

I figured it was worth testing inline + external. I had to test both to know if it effected both 🤷‍♂

I can remove one if you prefer that though. Remove the inline one because a giant blob of base64 is ugly..?

@lukastaegert
Copy link
Member

Well, only inline sourcemaps are a problem. External sourcemaps are not touched at all.

@jbedard
Copy link
Contributor Author

jbedard commented Mar 31, 2020

But the sourcemap comment should be removed either way...

@lukastaegert
Copy link
Member

Ah true, there is still a comment. Makes sense.

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

Successfully merging a pull request may close this issue.

2 participants