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:

  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:


# Tell `rustc` to optimize for small code size.
opt-level = "s"

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

pub fn 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';

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.

pub fn main() {

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.