spago icon indicating copy to clipboard operation
spago copied to clipboard

Sourcemaps don't work

Open andys8 opened this issue 3 years ago • 12 comments
trafficstars

Hi, this is more a question than an issue:

The docs say to build with --purs-args "-g sourcemaps" (which didn't work for me): https://github.com/purescript/spago#get-source-maps-for-my-project

But there is also a flag --source-maps provided by spago.

Is there a difference? Are the docs outdated? When to use what?

Maybe it's possible to clarify this in the docs :)

andys8 avatar Apr 29 '22 16:04 andys8

Good question - does the --source-maps flag work for you?

f-f avatar Apr 29 '22 17:04 f-f

The difference is that the purs flag fails to build. The spago flag builds fine, but I'm not sure if it makes a difference.

I'm not sure to what degree I can expect the result containing information, if it depends on other build settings or if the user needs to configure browser settings... But for now I think it doesn't seem to be working in chrome dev tools. That's how I started searching for PureScript specific instructions.

andys8 avatar Apr 30 '22 08:04 andys8

Right, I think the docs are outdated here and the purs flag just doesn't work anymore.

As of the latest version (0.20.8) when you bundle with the --source-maps flag in Spago, that gets passed to esbuild: https://github.com/purescript/spago/blob/744bdbbefc538750d4c36888ee53dc5d336314c4/src/Spago/Build.hs#L426 ..so the relevant esbuild documentation applies, see here.

As for having them work, once you get the .js.map file then Chrome should be able to pick it up once the JS maps flag is enabled.

Maybe there are ES-modules specific things that apply here? @sigma-andex any ideas about this?

f-f avatar Apr 30 '22 10:04 f-f

so the spago —source-map flag is the one that gets passed to esbuild to generate the source map for the bundle. I guess this should then be passed to the purs build actually so one doesnt have to provide the purs-args. Regarding the purs build failing: afaik the source-maps are currently largely broken right now and @j-nava is working on a fix: https://github.com/purescript/purescript/pull/4273. Maybe @j-nava can shed some light on how the purs sourcemaps should work together with the esbuild generated sourcemap.

sigma-andex avatar Apr 30 '22 10:04 sigma-andex

I've tested a few things and I think this is actually an issue.

When I run spago bundle-app --source-maps, it looks like esbuild bundles an index.js.map file based on whatever .map files it can find, and if there's not one for a particular file, it will generate a "placeholder" for it, mapping javascript to javascript. I speculate it does that in case only some of the sources to be bundled have source maps.

If I build first with --purs-args "-g sourcemaps", the .map files will be there and esbuild will correctly include them.

I think it's reasonable to expect that if spago bundle-app natively supports a --source-maps option and compiles the files for me, it would compile them with -g sourcemaps.

So, in my opinion, either removing the --source-maps option from bundle-app, or making the build phase of bundle-app call purs compile with -g sourcemaps in case the --source-maps option is selected would be fine.

On a side note, it would be great to see a --source-maps option for spago build too! 😄

j-nava avatar May 01 '22 20:05 j-nava

So, in my opinion, either removing the --source-maps option from bundle-app, or making the build phase of bundle-app call purs compile with -g sourcemaps in case the --source-maps option is selected would be fine. On a side note, it would be great to see a --source-maps option for spago build too!

I think both of these sound great!

f-f avatar May 02 '22 10:05 f-f

I'll mark this as a bug since it's functionality that we advertise having but it's currently broken

f-f avatar May 02 '22 10:05 f-f

I agree, I think the --source-maps option should pass the argument to purs so the user doesn't have to care about it, and it should be available for spago build.

sigma-andex avatar May 02 '22 10:05 sigma-andex

If I understand correctly than one issue is that arguments could be improved so that the user doesn't need to run spago build --source-maps --purs-args "-g sourcemaps", right?

But is the actual issue - not related to spago - "afaik the source-maps are currently largely broken right now" likely the reason to end up with: image

Is it expected before https://github.com/purescript/purescript/pull/4273 or worth looking into if the current state of tooling could produce usable source maps? :)

andys8 avatar May 09 '22 23:05 andys8

Unfortunately, I think that is expected. The PR you mentioned also fixes https://github.com/purescript/purescript/issues/4034, which is what's causing the above issue.

JordanMartinez avatar May 10 '22 00:05 JordanMartinez

Thanks for letting me know and pointing me to the issue about negative lines 🙂

andys8 avatar May 10 '22 00:05 andys8

Just FYI. The negative line/column issue was fixed in purescript/purescript#4343 and was published as 0.15.3-5. There's a few other changes in this pre-release, but you can try it out via npm i [email protected].

JordanMartinez avatar May 30 '22 16:05 JordanMartinez

In the meantime I forgot everything I knew about sourcemaps, and the new Spago does not implement anything about them, but it's something that we should support.

I'd love if someone in this thread could help with a summary of what we need to do to properly support sourcemaps end-to-end - as in: one can pass the --sourcemaps flag to spago and expect to have it working right away

f-f avatar Sep 29 '23 21:09 f-f

I reread this thread a few times, I think it's fair to summarize as follows:

Problem Statement

(1) Old spago docs recommended manually passing extra --purs-args "-g sourcemaps". This may have been necessary at some point. (2) If spago builds without sourcemaps, then the bundling step produces unusable source maps. (3) If purs creates invalid source maps, then the bundling step produces unusable sourcemaps.

Resolution

(1) Is no longer part of the spago readme (2) New Spago builds with the source map codegen target automatically enabled (3) The reported issue with purs source maps has been fixed: https://github.com/purescript/purescript/issues/4034

I experimented with simply passing --sourcemap to esbuild, and no other changes were required to get end to end source maps in my hobby project.

So with that mind I have a PR here to re-introduce source map support for spago bundle: #1030

CharlesTaylor7 avatar Sep 30 '23 03:09 CharlesTaylor7