Wasm-pack, stack traces, file name + line number

  1. I recently switched from (stdweb + cargo web start) to (web_sys + wasm-pack).

  2. In (stdweb + cargo web start), on panics, I could get file names + line numbers.

  3. In (websys + wasm-pack), all I am getting are function names; similar to:
    https://github.com/rustwasm/console_error_panic_hook/blob/master/with_panic_hook.png

  4. I am a big fan of line numbers and would like them back.

  5. I am building my code via wasm-pack build --target web --dev

  6. In cargo.toml, I have:

[profile.dev]
debug=true

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
debug=true

  1. The entry point to my code is:
pub fn set_panic_hook() {
    console_error_panic_hook::set_once();
}

#[wasm_bindgen]
pub fn main() {
    set_panic_hook();
    x_client_dom::main();
}

Question: is there anything else I can do to get more debug info / get line numbers ? Function name is nice, but line numbers would be ideal.

You also need to catch the error in the script

<script type=module>
      import main from './pkg/<project>.js';
      main().catch(console.error);
</script>

I don't understand what this changes. Before adding the catch(console.error);, I got something that looked like https://github.com/rustwasm/console_error_panic_hook/blob/master/with_panic_hook.png
after adding that line, it still looks like https://github.com/rustwasm/console_error_panic_hook/blob/master/with_panic_hook.png

I.e. before & after, it's both

stack frame = function name + ptr to *.wasm, but not line number in original *.rs file

That means your code is trying to access a Vec with an index that is higher than the length of the Vec. Unlike unwrap, rust doesn't display the line before the call was made. In this case, you'd have to put log::trace! in parts of your code where you suspect the access to index happens, and try to log the vec and the index. You may have to put a lot log::trace!s everywhere, before you can find the problem.

You'd have to use log and console_log crate. Don't forget to initialize the logging crates at the entry point.

This looks like a lot of work, but this is how I debug at the moment, I haven't found a better way to do this yet.

#[wasm_bindgen(start)]
pub fn main() {
    console_log::init_with_level(log::Level::Trace).unwrap();
    console_error_panic_hook::set_once();
   x_client_dom::main();