Skip to content

P2 fileysstem write fails with utf8Encode error #1078

@vados-cosmonic

Description

@vados-cosmonic

The following component code does not seem to work in a browser context (and may not in a NodeJS context either) when transpiled:

import { getDirectories } from "wasi:filesystem/[email protected]";

export const test = {
    run() {
        const preopens = getDirectories();
        if (preopens.length === 0) { throw "ERROR: no preopens"; }

        const dirDescriptor = preopens[0][0];
        const newFileRes = dirDescriptor.openAt(
            {symlinkFollow: false},
            "output.txt",
            { create: true },
            { write: true },
        );
        if (newFileRes.tag === "err") {
            throw "ERROR: failed to create new file: " + newFileRes.val;
        }
        const newFileDescriptor = newFileRes.val;

        newFileDescriptor.write(new TextEncoder().encode("hello world!"), 0);

        const syncRes = newFileDescriptor.sync();
        if (syncRes.tag !== "ok") {
            throw "ERROR: failed to sync new file to disk: " + syncRes.val;
        }

        return "SUCCESS: wrote file";
    }
}

The error that is returned is the following (output from a local test):

[browser] log: expected a string
[browser] log: Stack:
[browser] log:   utf8Encode@/tmp/67e786128b1f/sources/initializer.js:66:36
  export_test$run@/tmp/67e786128b1f/sources/initializer.js:6288:28
[browser] log:
[browser] log: Redirecting call to abort() to mozalloc_abort

RuntimeError: unreachable
RuntimeError: unreachable
    at http://127.0.0.1:34397/transpiled/component.core.wasm:wasm-function[12408]:0x88502e
    at http://127.0.0.1:34397/transpiled/component.core.wasm:wasm-function[4500]:0x1aaaac
    at tests:p2-shim/test#run (http://127.0.0.1:34397/transpiled/component.core.wasm:wasm-function[12536]:0x89b54f)
    at run (http://127.0.0.1:34397/transpiled/component.js:13246:15)
    at runTranspiledModuleTest (http://127.0.0.1:34397/index.html:100:27)
    at async http://127.0.0.1:34397/index.html:74:4

Ideally after this is fixed, the preview2-shim browser test should be updated to do more than just open a file and should write contents (and maybe read them back) as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions