Skip to content

feat: support plugins that import WASI - #21

Merged
dsherret merged 4 commits into
mainfrom
wasi
Oct 10, 2026
Merged

dsherret merged 4 commits into
mainfrom
wasi

Conversation

@dsherret

@dsherret dsherret commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Allows plugins compiled with a toolchain that needs a WASI libc (ex. .NET, Go) to be used, such as the Wasm build of dprint-plugin-roslyn. Before this, creating a formatter for one failed with Import "wasi_snapshot_preview1": module is not an object or function.

What's provided

wasi.ts is a port of the sandboxed subset of WASI preview 1 that the dprint CLI provides (dprint/dprint#1303), with the same behaviour:

  • The realtime and monotonic clocks, rounded to a millisecond.
  • Sleeping through poll_oneoff.
  • Random bytes.
  • Writing text to stdout/stderr, which is written to stderr as before. A single write is limited to 64 KiB.
  • stdin that is always at the end of the file.
  • No arguments, environment variables, or preopened directories, so every file system function fails with "bad file descriptor".
  • proc_exit throws instead of exiting.
  • Any other WASI function a module imports is defined to fail with "not implemented", so a runtime that imports functions it never calls can still be instantiated.

Pointers from the plugin are bounds checked and fail with "bad address" rather than throwing.

Plugins that export _initialize (WASI reactors) are initialized before use, and a plugin that only exports _start fails with the same error as the CLI.

Capturing plugin output

createContext, createFromBuffer, createFromWasmModule, and createStreaming take an optional onOutput function, which is called with the text a plugin writes to stdout or stderr. This allows capturing or ignoring it. Without it, the text is written to stderr as before.

Behaviour changes for existing plugins

  • Plugin output is now written with process.stderr.write in both Node.js and Deno. It was Deno.stderr.writeSync in Deno and process.stderr.writeSync in Node.js, which doesn't exist, so the output of a plugin was silently dropped in Node.js.

  • The existing env.fd_write import now shares the WASI implementation, so control characters other than a newline or tab in a plugin's output are replaced with U+FFFD. This stops a plugin from sending escape sequences to the terminal and matches the CLI.

  • Host.createImportObject takes an optional module, which is used to find the unsupported WASI functions to define.

Differences from the CLI

  • Sleeping blocks with Atomics.wait. Where that isn't allowed (ex. the main thread of a browser), the poll returns right away as though the time elapsed.
  • The CLI skips _initialize for old versions of dprint-plugin-gofumpt that run it as their start function (dprint 0.61.0 no longer loads dprint-plugin-gofumpt dprint#1306). That isn't done here, so I'd expect those versions to fail, though I didn't try one.

Other

  • Regenerated deno.lock, which was stale.

Testing

  • wasi_test.ts ports the CLI's tests of these functions. They run against test/test_wasi.wasm, a small module that imports the WASI functions and exports a wrapper for each, built by scripts/build_test_wasm.ts.
  • deno test -P: 29 pass. deno task build:npm passes, which runs the same tests in Node.
  • Formatted C# and Visual Basic with the Wasm build of dprint-plugin-roslyn in both Deno and Node (the npm build). That plugin isn't released as Wasm yet, so it isn't part of the tests.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant