emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.88k stars 3.32k forks source link

Embed source files in source map file #22189

Open verhovsky opened 4 months ago

verhovsky commented 4 months ago

I want to generate a source map when compiling my C code that includes all the source files in the source map file itself instead of having to serve all the source code files alongside the source map file. This can be done by adding a sourcesContent key to the source map file. Otherwise I would have to upload 93 C files to npm with my JavaScript library.

wasm-sourcemap.py includes support for doing this already with the --sources argument

https://github.com/emscripten-core/emscripten/blob/b3c25673f53974ff1f30df43701dc63af7cbbdc4/tools/wasm-sourcemap.py#L37

but there's no way to pass that argument from the emcc command

https://github.com/emscripten-core/emscripten/blob/b3c25673f53974ff1f30df43701dc63af7cbbdc4/tools/building.py#L1107-L1111

This could be a -ginline-source-map option or -gsource-map -s INLINE_SOURCE_MAP, which would match TypeScript's --inlineSourceMap. Though I expected this to be the default behavior. Serving C files over HTTP is weird.

My use case is that I have an npm package compiled with Emscripten and my source map requires files from ../../../../../../../opt/homebrew/Cellar/emscripten/3.1.61/libexec/system/lib/ and I have no idea how I could put that path in my npm module https://github.com/tree-sitter/tree-sitter/pull/3381

sbc100 commented 4 months ago

I think the normal way that folks deal with this issue it to use path substitution and server the sources form localhost: https://developer.chrome.com/blog/wasm-debugging-2020.

Embedding the all of the source files in the source map seems like it could lead to absolutely enourmous source maps so I'm not sure we would want to make that the default.

@dschuff @pfaffe @RReverser WDYT?

verhovsky commented 4 months ago

I am compiling a JavaScript library for npm. I want the source map to also work for downstream users when someone develops a website using the library. Users of the library probably won't bother to copy files out of node_modules/ to serve them for their source maps and they definitely won't bother with writing paths out in Chrome settings. They'll just use the library without source maps, so the choice is really between absolutely enormous source maps or no source maps.

As a datapoint, the sourcesContent for me would be 800KB, less than the 1MB wasm file.

$ cat $(jq -r '.sources[]' tree-sitter.wasm.map) | wc -c
847334
$ wc -c tree-sitter.wasm
1055249 tree-sitter.wasm
sbc100 commented 4 months ago

(Sorry, the wasm-debugging link I sent above was for DWARF debugging. It probably not relevant to source map debugging)

pfaffe commented 4 months ago

The request makes sense in general, but don't make it the default. As default, it could cause quite the surprise for any user who doesn't want to ship their sources.

verhovsky commented 4 months ago

I meant that putting all the sources in sourcesContent should be the default when using -gsource-map. It shouldn't be surprising that a source map includes the source, at a minimum it already contains line/character numbers which gives you some hint about the source.

dschuff commented 3 months ago

I think it's a good idea to support this, but I also think it's best not to have it on by default. Not just for the surprise in possibly exposing the sources, but also that it would make the source map much bigger, and many common use cases (e.g. stack trace symbolization) don't require the sources.