Skip to content

Exceptions throw panic - Option::unwrap() on a None value #134

@johanjanssens

Description

@johanjanssens

Running into a similar issue as described here: #111

thread '<unnamed>' panicked at crates/core/src/lib.rs:21:53:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: wasm error: unreachable
wasm stack trace:
    .$1356(i32,i32,i32,i32,i32)
    .$1491(i32,i32)
    .$1482(i32,i32,i32)
    .$1508(i32)
    .$209(i32,i32) i32
    .$1559(i32) i32
    .$1566() i32
returned non-zero exit code: 1

Seems that https://github.com/extism/js-pdk/blob/main/crates/core/src/lib.rs#L21 isn't able to handle the exception, trying to unwrap something that isn't there.

Original report on discord: https://discord.com/channels/1011124058408112148/1062468347851178165/1372870993915150407

To replicate

The following code should be run async, as createHighlighter returns a promise, compiler doesn't error on it, but when running you get the following:

import { createHighlighter } from 'shiki'

export function format() {

    const code = 'const a = 1' // input code
    const highlighter = createHighlighter({
        themes: ['nord'],
        langs: ['javascript'],
    })

    const html = highlighter.codeToHtml(code, {
        lang: 'javascript',
        theme: 'nord'
    })

    Host.outputString(html);
}
extism call shikijs.wasm format --wasi --log-level debug
2025/05/16 23:35:01 Calling function : format

thread '<unnamed>' panicked at crates/core/src/lib.rs:21:53:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: wasm error: unreachable
wasm stack trace:
	.$1356(i32,i32,i32,i32,i32)
	.$1491(i32,i32)
	.$1482(i32,i32,i32)
	.$1508(i32)
	.$209(i32,i32) i32
	.$1559(i32) i32
	.$1566() i32
returned non-zero exit code: 1

If in a try / catch the Error is catched properly and there is no panic.

export function format() {

    try {
        const code = 'const a = 1' // input code
        const highlighter = createHighlighter({
            themes: ['nord'],
            langs: ['javascript'],
        })

        const html = highlighter.codeToHtml(code, {
            lang: 'javascript',
            theme: 'nord'
        })

        Host.outputString(html);

    } catch (err) {
        console.error(err.message);
    }

    Host.outputString('done');
}
extism call shikijs.wasm format --wasi --log-level debug
2025/05/16 23:42:35 Calling function : format
2025/05/16 23:42:35 not a function
done

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions