Skip to content

WebContainers / StackBlitz crash in CLI shim due to enableCompileCache() #1

Description

@jonathanpv

Summary

ripgrep([...]) already works in StackBlitz WebContainers, which suggests the wasm32-wasip1 build itself is fine there. The failure happens in the CLI launcher path and in host selection around node:wasi, not in the ripgrep WASM payload.

In WebContainers (observed with Node.js v22.22.0), the package crashes when invoking the CLI shim or anything that indirectly touches rgPath / lib/rg.mjs.

Reproduction

In a StackBlitz WebContainer project:

import { ripgrep, rgPath } from "ripgrep";

const { code } = await ripgrep(["--json", "Vite", "src"]);

import { spawn } from "node:child_process";
spawn(rgPath, ["Vite", "src"], { stdio: "inherit" });

And from the shell:

npx ripgrep vite src/

Observed error:

TypeError: Cannot read properties of undefined (reading '0')
    at Module.enableCompileCache (node:internal/modules/helpers:177:2181)
    at eval (file:///home/abundant-ui/node_modules/.pnpm/ripgrep@0.1.0/node_modules/ripgrep/lib/rg.mjs:13:41)
    at ModuleJob.run (node:internal/modules/esm/module_job:170:4963)

Root cause

The immediate crash is caused by lib/rg.mjs doing this eagerly:

import module from "node:module";
module.enableCompileCache?.();

In WebContainers, enough of Node is implemented to run the package, but this compile-cache path is not safe and throws before ripgrep starts.

Separately, lib/index.mjs prefers Node's built-in node:wasi by default on Node-like hosts. In browser-hosted Node runtimes like StackBlitz WebContainers, node:wasi may exist but still be unreliable depending on the host/runtime behavior. The package already has a portable custom WASI shim, so a fallback path helps.

Working fix

I tested a fork with two changes, and it works in StackBlitz WebContainers both programmatically and via the CLI shim:

  1. Make enableCompileCache() best-effort in lib/rg.mjs.

    • Dynamically import node:module
    • Call enableCompileCache?.()
    • Swallow failures so unsupported hosts do not crash
  2. In lib/index.mjs, if createNodeWasi() fails, automatically fall back to the existing custom JS WASI shim.

Reference commit with the working patch:

jonathanpv/ripgrep@3f75c80

Verification

After applying the patch in the fork, this worked successfully inside a StackBlitz WebContainer:

node node_modules/@abundantui/ripgrep/lib/rg.mjs --json App ./src

And programmatic usage also worked.

Why this seems worth merging

This looks like a good portability improvement rather than a StackBlitz-only workaround:

  • the WASM build already works in WebContainers
  • the crash is in optional host-specific startup optimization (enableCompileCache)
  • falling back from node:wasi to the existing custom shim preserves compatibility in browser-hosted Node environments without affecting normal Node users

If useful, I can open a PR based on the tested patch linked above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions