Skip to content

TypeError: Invalid byte sequence in service_ts getter during code generation #109

@q-uint

Description

@q-uint

Bug

Starting the Vite dev server fails with a TypeError: Invalid byte sequence thrown from the WASM-generated TextDecoder when reading the service_ts output of GenerateResult.

TypeError: Invalid byte sequence
    at decode (unknown)
    at service_ts (.../node_modules/@icp-sdk/bindgen/dist/esm/core/generate/rs/dist/icp-js-bindgen.js:251:14)
    at writeBindings (.../node_modules/@icp-sdk/bindgen/dist/esm/core/generate/index.js:60:36)
    at async generate (.../node_modules/@icp-sdk/bindgen/dist/esm/core/generate/index.js:35:9)
    at async run (.../node_modules/@icp-sdk/bindgen/dist/esm/plugins/vite.js:30:9)
    at async buildStart (.../node_modules/@icp-sdk/bindgen/dist/esm/plugins/vite.js:9:13)

Root cause

The GenerateResult struct exposes service_ts via wasm_bindgen(getter_with_clone). The generated JS getter reads a pointer/length pair from WASM memory and decodes it via TextDecoder("utf-8", { fatal: true }):

get service_ts() {
    const ret = wasm.__wbg_get_generateresult_service_ts(this.__wbg_ptr);
    return getStringFromWasm0(ret[0], ret[1]); // throws TypeError
}

Note: This getter is a wasm-bindgen build artifact in the published package (dist/esm/core/generate/rs/dist/icp-js-bindgen.js), not in the source repo.

The bytes at that location are not valid UTF-8, so the fatal decoder throws instead of silently replacing with U+FFFD. This means either:

  • The Rust code generator is producing non-UTF-8 output for certain .did inputs, or
  • The returned pointer/length pair is incorrect (e.g. a memory corruption or use-after-free in the WASM allocator)

Environment

  • @icp-sdk/bindgen: 0.2.1
  • Vite 6.x
  • Node.js 22.x

Metadata

Metadata

Assignees

No one assigned

    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