Skip to content

WasmFS Documentation #15949

Open
Open

Description

@tlively

WasmFS should be well documented, with docs covering this non-exhaustive list of topics:

  • How to build with WasmFS
  • How to configure various backends and reason about when caches, etc. would be helpful.
  • How to write your own backend.

Activity

  1. moved this to Todo in WasmFSon Jan 11, 2022
  2. added this to the WasmFS new features milestone on Jan 11, 2022
  3. curiousdannii commented on Jan 15, 2022

    @curiousdannii
    Contributor

    My project has two things that I'd like to use WasmFS for, so it would be great if the docs explained both of these:

    1. A custom async FS. I have a custom FS currently, but it's hampered by not being async
    2. Async stdin, which I currently implement by wrapping getc with a function that calls (if it's getting stdin) an EM_ASYNC_JS function. I'm lucky that the project I'm porting only accesses stdin via getc, as it would be messy to have to wrap lots of other functions too.

    Also, enabling and mounting FSes used to happen in JS code. Will that still be the case? Or will we be able to put this code into the wasm. My port doesn't modify main, so it would be nice if this doesn't require that either...

    this.EFS = new EmglkenFS(this)
    Module.FS.mkdir('/emglken')
    Module.FS.mount(this.EFS, {}, '/emglken')
    Module.FS.chdir('/emglken')
    
  4. tlively commented on Jan 18, 2022

    @tlively
    MemberAuthor

    re: mounting, I expect we will have both C and JS APIs for that. The story for Async APIs hasn't quite been figured out, but that's tracked in #15964.

  5. curiousdannii commented on Jan 18, 2022

    @curiousdannii
    Contributor

    @tlively #15964 is about a new async API, right? I was expecting that it would possible to make the standard posix FS API async with Asyncify.

  6. tlively commented on Jan 18, 2022

    @tlively
    MemberAuthor

    Oh gotcha. Yes, we might have to do something to get Asyncify working as expected as well. (Or maybe it will Just Work, not sure). I filed #16053 to track that separately.

  7. nachoab commented on Dec 30, 2022

    @nachoab

    Is there some example of compiling including OPFS? Setting WASMFS=1 doesn't seem to include it, even though it looks like it is. Also, using current FS you have to use the mount method to specify a different FS provider, does WasmFS use this same API?

  8. mere-human commented on Dec 31, 2022

    @mere-human
    Contributor

    Is there some example of compiling including OPFS?

    @nachoab I think the best example would be tests, for instance https://github.com/emscripten-core/emscripten/blob/main/test/wasmfs/wasmfs_opfs.c

  9. curiousdannii commented on Mar 5, 2024

    @curiousdannii
    Contributor

    It's taken a long while, but I'm ready to start working on my own WasmFS backend.

    How "ready" is WasmFS now? I know some people are using it, but I also see lots of open issues and tickets in the project/milestone. It seems like there's still not really any docs, we just have to learn from the other implementations?

    I suspect it might not be that difficult to use WasmFS with Asyncify, because you can put anything through Asyncify. The question is more about what's efficient and ergonomic.

    An alternative idea I had was to somehow replace stdio.h with a custom implementation, but that would probably be much more complex.

  10. tlively commented on Mar 5, 2024

    @tlively
    MemberAuthor

    We have some very large applications using WasmFS, especially with the OPFS backend, so it's "ready" for production use in the sense that it is stable and correct enough. We haven't been able to implement as many new features as we wanted, though, such as other concrete backends (for IndexedDB especially) and virtual backends for caching and layering, etc. We also haven't been able to finish all the compatibility work that we want to have done to allow us to turn on WasmFS by default.

  11. art926 commented on Apr 30, 2024

    @art926
  12. adamziel commented on Aug 2, 2024

    @adamziel

    We have some very large applications using WasmFS, especially with the OPFS backend, so it's "ready" for production use in the sense that it is stable and correct enough.

    @tlively I would love to explore using the OPFS backend in WordPress Playground, but I'm not sure how to do it. Would that work without pthreads support? A perfect option for Playground would be an asynchronous filesystem backend as we don't support pthreads. We're using MEMFS in web browsers and MEMFS+NODEFS in Node.js. Would you be able to point me to any resources, or write up a guide in a comment in this issue?

  13. jozefchutka commented on Sep 3, 2024

    @jozefchutka

    I also came here to explore options for OPFS backend. Preferably would like to mount FileSystemDirectoryHandle as mentioned in #15964 (comment) but have no idea how/where to start. Will OPFS backend be provided by emscripten or something a dev would implement himself following some documentation?

  14. adamziel commented on Sep 16, 2024

    @adamziel

    Looping in @ThomasTheDane @tomayac – an OPFS backend would be a highly useful addition.

  15. kripken commented on Sep 20, 2024

    @kripken
    Member

    I would love to explore using the OPFS backend in WordPress Playground, but I'm not sure how to do it. Would that work without pthreads support?

    I believe the OPFS backend requires threads or JS Promise Integration (JSPI). One of the two is needed so that we can handle the async behavior of the JS APIs there. Here are the tests that we run:

    @no_firefox('no OPFS support yet')
    @no_wasm64()
    @parameterized({
    '': (['-pthread', '-sPROXY_TO_PTHREAD'],),
    'jspi': (['-Wno-experimental', '-sASYNCIFY=2'],),
    'jspi_wasm_bigint': (['-Wno-experimental', '-sASYNCIFY=2', '-sWASM_BIGINT'],),
    })
    def test_wasmfs_opfs(self, args):
    if '-sASYNCIFY=2' in args:
    self.require_jspi()
    test = test_file('wasmfs/wasmfs_opfs.c')
    args = ['-sWASMFS', '-O3'] + args
    self.btest_exit(test, args=args + ['-DWASMFS_SETUP'])
    self.btest_exit(test, args=args + ['-DWASMFS_RESUME'])
    @no_firefox('no OPFS support yet')
    def test_wasmfs_opfs_errors(self):
    test = test_file('wasmfs/wasmfs_opfs_errors.c')
    postjs = test_file('wasmfs/wasmfs_opfs_errors_post.js')
    args = ['-sWASMFS', '-pthread', '-sPROXY_TO_PTHREAD', '--post-js', postjs]
    self.btest(test, args=args, expected='0')

    The code in those tests could be an easy way to get started with the OPFS backend.

    Separately, it would be good to update the docs on the website.

  16. adamziel commented on Oct 9, 2024

    @adamziel

    Thanks @kripken! I started exploring and got some initial successes, I'll report back when I have something workable. It will probably take a few months of taking a few moments here and there to get this right – just like supporting JSPI in Playground took 1,5 years. It wasn't an easy process.

    It would be much easier to use this API if there was a high quality documentation page. Not blog posts scattered on v8.dev and brief descriptions on https://emscripten.org/docs/, but an actual solid learning resource with detailed, step by step guides, code examples, descriptions of different wasmfs backends, specific terminal commands to run, outcomes to expect, mental models explained, and live demos of the built binaries. Also, a transition guide from the JavaScript FS to WASMFS would help a lot.

    Right now we, the developers trying to use WebAssembly, are in a tough spot. It's very unclear what's in progress, what's finished, what's the browser support, how to use what, and which APIs are going to change in two versions.

  17. kripken commented on Oct 9, 2024

    @kripken
    Member

    @adamziel I agree, this could be much improved. There is also the remaining work to get WasmFS enabled by default, and I was thinking that might happen before, but perhaps it doesn't need to be in that order. I'm not sure when I'll have time myself to get to either, but I'd like to (and others might get to it before).

    fyi @tomayac , this could be a promising area for docs or a blogpost or both.

  18. tomayac commented on Oct 10, 2024

    @tomayac
    Collaborator

    I agree that there's a need to document this properly. Meanwhile, this repository courtesy of @milky2028 might help people bootstrap their apps. May I also recommend people working on this install my OPFS Explorer Chrome DevTools extension? The files that you open with this demo are persisted across reloads, as you can verify with the extension.

    Screenshot 2024-10-10 at 11 02 26

  19. adamziel commented on Apr 25, 2025

    @adamziel
  20. nexryai commented on Jan 7, 2026

    @nexryai

    I'm having trouble getting WASMFS (OPFS) to work with JSPI.
    I've tried many build options, including JSPI_EXPORTS, but OPFS initialization fails with the following error:

    wasmfs_create_directory("/opfs", 0755, opfs);
    SuspendError: trying to suspend without WebAssembly.promising
    

    The offending WASM binary code, as shown in the stack trace, is as follows:

    call $env._wasmfs_opfs_init_root_directory
    

    After searching Google and GitHub for a solution, I found that @adamziel also appears to be encountering the same issue.
    https://github.com/adamziel/emscripten-wasmfs-demo/tree/b66cb7e0c8b8413d9e4a95483f623f134e504b32?tab=readme-ov-file#running-the-demo

    Is this an Emscripten issue, or am I missing something?
    Due to technical constraints, I can't use ptread, so JSPI is my only option.

    My linkopts and copts are as follows.
    I actually set similar options in copts, but I've omitted them because they're redundant.

    cc_binary(
        name = "ikaria",
        linkopts = [
            "-O3",
            "--bind",
            "-sINITIAL_MEMORY=399966208",
            "-sEXPORT_ES6=1",
            "-sWASMFS",
            "-sASYNCIFY=2",
            "-sENVIRONMENT=web,worker",
            "-sJSPI_EXPORTS=['_wasmfs_opfs_init_root_directory']",
            "-sASYNCIFY_IGNORE_INDIRECT=1",
            "-sUSE_PTHREADS=0",
            "-sASSERTIONS",
            "-o ikaria.js",
        ],
        deps = [":ikaria_lib"],
    )

    This appears to be covered by the tests.

    emscripten_console_log("starting main");
    backend_t opfs = wasmfs_create_opfs_backend();
    emscripten_console_log("created OPFS backend");
    err = wasmfs_create_directory("/opfs", 0777, opfs);
    assert(err == 0);
    emscripten_console_log("mounted OPFS root directory");

    @no_firefox('no OPFS support yet')
    @no_wasm64()
    @parameterized({
    '': (['-pthread', '-sPROXY_TO_PTHREAD'],),
    'jspi': (['-Wno-experimental', '-sASYNCIFY=2'],),
    'jspi_wasm_bigint': (['-Wno-experimental', '-sASYNCIFY=2', '-sWASM_BIGINT'],),
    })
    @no_safari('TODO: Fails with abort:Assertion failed: err == 0') # Fails in Safari 17.6 (17618.3.11.11.7, 17618), Safari 26.0.1 (21622.1.22.11.15)
    def test_wasmfs_opfs(self, args):
    if '-sASYNCIFY=2' in args:
    self.require_jspi()
    test = test_file('wasmfs/wasmfs_opfs.c')
    args = ['-sWASMFS', '-O3'] + args
    self.btest_exit(test, cflags=args + ['-DWASMFS_SETUP'])
    self.btest_exit(test, cflags=args + ['-DWASMFS_RESUME'])

    I'm completely stuck, and any information would be a great help.

  21. nexryai commented on Jan 8, 2026

    @nexryai

    After some trial and error, I finally found the cause.
    It seems that an error occurs when using OPFS in a function bound with EMSCRIPTEN_BINDINGS rather than main().
    When I changed wasmfs_create_directory to run in the main function, the error disappeared.
    If I still try to write to an OPFS-mounted directory in a function bound with EMSCRIPTEN_BINDINGS, I get the following error:

    SuspendError: trying to suspend without WebAssembly.promising
    at opfs.wasm:0x453c0c
    
    0x453c0c call $env._wasmfs_opfs_get_child
    

    Looking at the JavaScript that Emscripten is generating, I think this is because Emscripten doesn't recognize the function I bound as asynchronous.

            instrumentWasmExports(e) {
                var t = /^(main|__main_argc_argv)$/;
                pr.asyncExports = new Set;
                var r = {};
                for (let[a,i] of Object.entries(e))
                    if ("function" == typeof i) {
                        t.test(a) && (pr.asyncExports.add(i),
                        i = pr.makeAsyncFunction(i));
                        var n = pr.instrumentFunction(i);
                        r[a] = n
                    } else
                        r[a] = i;
                return r
            },
            asyncExports: null,
            isAsyncExport: e => pr.asyncExports?.has(e),
            handleAsync: async e => {
                try {
                    return await e()
                } finally {}
            }
            ,
            handleSleep: e => pr.handleAsync( () => new Promise(e)),
            makeAsyncFunction: e => WebAssembly.promising(e)
        };

    instrumentWasmExports(exports) {
    #if EMBIND_GEN_MODE
    // Instrumenting is not needed when generating code.
    return exports;
    #endif
    #if ASYNCIFY_DEBUG
    dbg('asyncify instrumenting exports');
    #endif
    #if ASYNCIFY == 2
    var exportPattern = {{{ new RegExp(`^(${ASYNCIFY_EXPORTS.join('|').replace(/\*/g, '.*')})$`) }}};
    Asyncify.asyncExports = new Set();
    #endif

    I've set the following bindings based on the documentation, but for some reason they're not working.

    using namespace emscripten;
    
    EMSCRIPTEN_BINDINGS(dash_remuxer_module) {
        class_<DashRemuxer>("DashRemuxer")
            .constructor<>()
            .function("process", &DashRemuxer::process, async());
    }

    https://emscripten.org/docs/porting/asyncify.html#usage-with-embind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions