Skip to content

Commit 41be120

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(compiler): load the WASI fallback where / is not accessible (Android) (#3758)
The @solidjs/compiler-wasm32-wasi loader is generated by `napi build`, and @napi-rs/cli 3.8.6 emitted a WASI preopen of the host filesystem root. On Android/Termux `/` cannot be opened, so `new WASI(...)` failed with UVWASI_EACCES before the compiler ran. napi-rs fixed the template upstream (napi-rs/napi-rs#3485, in 3.9.1+): on Android the guest root maps to the working directory, and the main thread passes that root to its workers. Bump @napi-rs/cli to 3.10.6 so the published loader carries it. The WASI test run gains a regression test that loads the binding with `node:wasi` rejecting host-root preopens, as Termux does: it fails with the 3.8.6 loader and passes with the regenerated one. Fixes #3748 Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 98d35b9 commit 41be120

5 files changed

Lines changed: 96 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/compiler": patch
3+
---
4+
5+
The `@solidjs/compiler-wasm32-wasi` fallback loads on Android (Termux), where the filesystem root cannot be opened: its generated loader no longer preopens `/` there (it failed with `UVWASI_EACCES, uvwasi_init`). The loader is regenerated by `@napi-rs/cli` 3.10.6, which maps the WASI root to the working directory on Android.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
3+
const { spawnSync } = require("node:child_process");
4+
const path = require("node:path");
5+
6+
// The WASI binding only exists after a wasm32-wasip1-threads build, which
7+
// the WASI test run (SOLID_COMPILER_TEST_WASI) has.
8+
const describeWasi = process.env.SOLID_COMPILER_TEST_WASI ? describe : describe.skip;
9+
10+
const compilerDir = path.resolve(__dirname, "..");
11+
const shim = path.join(__dirname, "wasi-android", "simulate-termux.cjs");
12+
const script = `
13+
const compiler = require(${JSON.stringify(compilerDir)});
14+
const sync = compiler.transform("const el = <div class='a' />;", { filename: "a.jsx" }).code;
15+
compiler
16+
.transformAsync("const el = <span />;", { filename: "b.jsx" })
17+
.then(async => console.log(JSON.stringify({ sync, async: async.code })));
18+
`;
19+
20+
function loadUnderTermux(simulateAndroid) {
21+
return spawnSync(process.execPath, ["--require", shim, "-e", script], {
22+
cwd: compilerDir,
23+
encoding: "utf8",
24+
env: {
25+
...process.env,
26+
NAPI_RS_FORCE_WASI: "error",
27+
SIMULATE_ANDROID: simulateAndroid ? "1" : "0"
28+
},
29+
timeout: 60_000
30+
});
31+
}
32+
33+
describeWasi("WASI binding where the filesystem root is not accessible (#3748)", () => {
34+
test("the simulation rejects a preopen of the host root", () => {
35+
const result = loadUnderTermux(false);
36+
expect(result.status).not.toBe(0);
37+
expect(result.stderr).toContain("UVWASI_EACCES");
38+
});
39+
40+
test("loads and compiles on Android without preopening the host root", () => {
41+
const result = loadUnderTermux(true);
42+
expect(result.stderr).not.toContain("UVWASI_EACCES");
43+
expect(result.status).toBe(0);
44+
const out = JSON.parse(result.stdout.trim().split("\n").pop());
45+
expect(out.sync).toContain("_$template");
46+
expect(out.async).toContain("_$template");
47+
});
48+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
// Preloaded with `--require`. Reproduces Android/Termux, where the
4+
// filesystem root cannot be opened: Node's `uvwasi_init` fails with
5+
// UVWASI_EACCES for a WASI preopen of the host root (#3748).
6+
// SIMULATE_ANDROID=1 also reports the platform as Android, the signal the
7+
// generated loader keys its accessible root on.
8+
9+
const path = require("node:path");
10+
const wasi = require("node:wasi");
11+
12+
if (process.env.SIMULATE_ANDROID === "1") {
13+
Object.defineProperty(process, "platform", { value: "android" });
14+
}
15+
16+
const hostRoot = path.parse(process.cwd()).root;
17+
const NodeWASI = wasi.WASI;
18+
wasi.WASI = class extends NodeWASI {
19+
constructor(options = {}) {
20+
for (const host of Object.values(options.preopens || {})) {
21+
if (path.resolve(host) === hostRoot) {
22+
const error = new Error("UVWASI_EACCES, uvwasi_init");
23+
error.code = "UVWASI_EACCES";
24+
throw error;
25+
}
26+
}
27+
super(options);
28+
}
29+
};

‎packages/compiler/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"devDependencies": {
5858
"@emnapi/core": "^1.11.3",
5959
"@emnapi/runtime": "^1.11.3",
60-
"@napi-rs/cli": "^3.7.2",
60+
"@napi-rs/cli": "^3.10.6",
6161
"@napi-rs/wasm-runtime": "^1.1.6",
6262
"emnapi": "^1.11.3"
6363
},

‎pnpm-lock.yaml‎

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)