Skip to content

Expose WASI preopens through DefaultRubyVM - #658

Merged
kateinoigakukun merged 1 commit into
ruby:mainfrom
ledsun:experiment/wasi-preopen-require-local
Aug 15, 2026
Merged

Expose WASI preopens through DefaultRubyVM#658
kateinoigakukun merged 1 commit into
ruby:mainfrom
ledsun:experiment/wasi-preopen-require-local

Conversation

@ledsun

@ledsun ledsun commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Motivation

As we develop the JavaScript APIs for running Ruby, Node.js is a convenient environment for quickly verifying behavior. However, DefaultRubyVM did not expose the WASI preopens option, so loading an application made up of multiple local Ruby files was unnecessarily difficult.

The goal of this PR is to make local Ruby application code easy to load in Node.js while keeping the normal Ruby loading behavior.

Why WASI preopens

One possible approach is a loader similar to RequireRemote: read source code through JavaScript and evaluate it in Ruby. For local files in Node.js, WASI preopens provide a better fit:

  • Ruby can use its native require and require_relative implementations.
  • Ruby itself manages path resolution, $LOADED_FEATURES, duplicate requires, and load errors.
  • Only explicitly preopened host directories are visible inside the WASI guest namespace; this does not expose unrestricted Node.js filesystem access to Ruby.
  • It avoids introducing and maintaining another Ruby/JavaScript loader and filesystem bridge.

RequireRemote remains useful in browser environments where source files must be fetched over a URL. This PR uses the WASI filesystem capability already available in Node.js for the local-file case.

Changes

  • expose Node.js WASI preopens through DefaultRubyVM
  • add a Node.js example that preopens a directory and loads local Ruby files using require and require_relative
  • verify guest paths in __FILE__ and $LOADED_FEATURES, duplicate-require behavior, and that the original host path is not visible to Ruby

Example

const { vm } = await DefaultRubyVM(module, {
  preopens: {
    "/app": "./require_relative",
  },
});

vm.eval(`require "/app/main"`);

Verification

  • bundle exec rake npm:ruby-wasm-wasi:build
  • env RUBY_NPM_PACKAGE_ROOT=packages/npm-packages/ruby-head-wasm-wasi npm run test:vitest --workspace=@ruby/wasm-wasi -- --run (44 tests)
  • node packages/npm-packages/ruby-wasm-wasi/example/preopens.node.js
  • git diff --check origin/main...HEAD

@ledsun
ledsun marked this pull request as ready for review August 15, 2026 14:47
@kateinoigakukun
kateinoigakukun merged commit 1ae40fc into ruby:main Aug 15, 2026
39 checks passed
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.

2 participants