Spago doesn't use source maps to refer to the source code location of an error in the stack trace.
Here is my Main.purs:
f :: Unit -> Unit
f _ = unsafeCrashWith "error"
main :: Effect Unit
main = do
pure $ f unit
I ran these commands to build and run the program:
spago build --purs-args "-g source maps"
spago run
In the output, I get references to lines in index.js files, e.g.
<my-project>/output/Partial/foreign.js:6
throw new Error(msg);
^
Error: error
at exports._crashWith (<my-project>/output/Partial/foreign.js:6:9)
at <my-project>/output/Partial.Unsafe/index.js:8:35
at exports._unsafePartial (<my-project>/output/Partial.Unsafe/foreign.js:6:10)
at Object.unsafeCrashWith (<my-project>/output/Partial.Unsafe/index.js:7:12)
at f (<my-project>/output/Main/index.js:14:27)
at Object.<anonymous> (<my-project>/output/Main/index.js:16:63)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
What I want is for this stack trace to use the source maps to refer to the actual purescript source code locations.
Ok, there's quite a lot here. Some are your mistakes, some are undocumented gotchas.
So first of all, if you run
purs compile --help, you should see the following section in there:This gives us two pieces of information:
sourcemaps(no spaces), notsource maps. So your parameter should be--purs-args "-g sourcemaps"sourcemaps, you will get only source maps, no actual JS output. So your parameter should be--purs-args "-g sourcemaps,js"But besides these two, there is an undocumented gotcha: there should be no space after
-g(surprise!)Well, to be fair, it's not that wild, it's a somewhat accepted pattern. And it goes like this:
--purs-args "-gsourcemaps,js"--purs-args "--codegen=sourcemaps,js"Ok, so now, if you run
spago build --purs-args "-gsourcemaps,js", you should seeindex.js.mapfiles generated next to theindex.jsfiles. So far so good.But - oh no! When you
spago run, you still see JS callstack only. What's going on?Well, turns out, even though Node does support source maps starting with v12.12, they are disabled by default. You have to enable them explicitly by passing the
--enable-source-mapsparameter to Node itself. And in order to pass extra parameters to Node, Spago offers the handy-b|--node-argsoption.So, putting together all of the above, here's your final solution:
In conclusion, I'd like to note that Spago itself has a parameter
-x|--source-maps, which is for some reason allowed for bothrunandbuildcommands but in reality doesn't actually apply to them: it only works forbundle-appandbundle-module.