From b1737254776296b4e832b99fb4e04ce2faffef25 Mon Sep 17 00:00:00 2001 From: Chris Gaafary <11409288+cgaaf@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:29:27 -0500 Subject: [PATCH] vite: do not reuse a failed build A failed build stamps its entry and the files its diagnostics name. MO-6 names the importer, not the module that is missing or lacks the export, so restoring that module changed no stamp and the failed build was served until the entry or the importer was touched, or the dev server restarted (#84). Only a build that compiled is reused now; a program that does not compile is built again on each request. compiler/test/node.mjs: removes a dependency, sees MODULE_NOT_FOUND through the front end, puts the file back with its mtime unchanged, and compiles the entry and the dependency again, warm and cold. Co-Authored-By: Claude Fable 5.2 --- compiler/test/node.mjs | 30 +++++++++++++++++++++++++++--- compiler/vite.mjs | 3 ++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/compiler/test/node.mjs b/compiler/test/node.mjs index 15984a05..da2c0070 100644 --- a/compiler/test/node.mjs +++ b/compiler/test/node.mjs @@ -1,9 +1,10 @@ // The Node-API build loads on this platform and compiles a program for every target: what CI checks on each binary -// before it is published. Usage: node test/node.mjs (after `npm run build`) +// before it is published. Then the Vite front end's cache over it (vite.mjs). +// Usage: node test/node.mjs (after `npm run build`) import { createRequire } from "node:module"; -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; import path from "node:path"; -import { mkdtempSync, writeFileSync } from "node:fs"; +import { mkdtempSync, renameSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; const { app } = createRequire(import.meta.url)("../index.js"); @@ -36,4 +37,27 @@ const code = Object.values(rel.modules ?? {}).map(m => m.code).join("\n"); const relOk = !rel.error && !rel.diagnostics && /from "\.\/util\.js"/.test(code) && !/from "\.\/api"/.test(code); if (!relOk) fail++; console.log(`${relOk ? "✓" : "✗"} a relative JS import stays an ES import${relOk ? "" : `: ${JSON.stringify(rel).slice(0, 300)}`}`); +// the Vite front end (vite.mjs) does not keep a failed build: a program that failed on an import compiles once the import +// is back (#84). The diagnostic names the importer and a rename keeps the file's mtime, so nothing it stamped changes +const { default: rx } = await import(pathToFileURL(fileURLToPath(new URL("../vite.mjs", import.meta.url))).href); +const root = mkdtempSync(path.join(tmpdir(), "rx-recover-")); +const main = path.join(root, "main.rx"), part = path.join(root, "part.rx"), away = path.join(root, "part.away"); +writeFileSync(main, 'import { Part } from "./part.rx"\n\nexport default component App() {\n \n}\n'); +writeFileSync(part, 'export component Part(props: { label: string }) {\n {props.label}\n}\n'); +const ctx = (id, moduleId) => ({ id, moduleId, root, solid: { generate: "dom" }, addWatchFile() {} }); +const entry = front => front.compile("", ctx(main, main)).code, dep = front => front.compile("", ctx(part, `${part}?rx-dep`)).code; +const lost = compile => { try { compile(); } catch (e) { return /main\.rx:\d+(:\d+)?: error MODULE_NOT_FOUND/.test(e.message); } return false; }; +let recovered; +try { + const warm = rx(), want = { entry: entry(warm), dep: dep(warm) }; + renameSync(part, away); const failed = lost(() => entry(warm)); renameSync(away, part); + if (!failed) throw new Error("a missing import is not MODULE_NOT_FOUND at its importer"); + if (entry(warm) !== want.entry || dep(warm) !== want.dep) throw new Error("the program compiled after its import came back differs from the first"); + // and a program whose first compile failed, then asked for by its dependency (the cold path) + const cold = rx(); + renameSync(part, away); lost(() => entry(cold)); renameSync(away, part); + if (dep(cold) !== want.dep) throw new Error("the dependency of a program that had failed differs from the first"); +} catch (e) { recovered = e.message.split("\n")[0]; } +if (recovered) fail++; +console.log(`${recovered ? "✗" : "✓"} the Vite front end compiles a program again once its missing import is back${recovered ? `: ${recovered}` : ""}`); process.exit(fail ? 1 : 0); diff --git a/compiler/vite.mjs b/compiler/vite.mjs index ed6f0275..423da87a 100644 --- a/compiler/vite.mjs +++ b/compiler/vite.mjs @@ -65,7 +65,8 @@ export default function rx() { function build(entry, target, moduleName, spa) { const key = `${entry}\0${target}\0${moduleName}\0${spa}`; const prior = builds.get(key); - if (prior && fresh(prior)) return prior; + // a failed build's stamps miss what it failed to find (MO-6 names the importer), so it is never reused + if (prior?.out.modules && fresh(prior)) return prior; const out = JSON.parse(app(entry, target, moduleName, spa)); if (out.modules) out.modules = Object.fromEntries(Object.entries(out.modules).map(([p, m]) => [path.resolve(p), m])); const files = out.modules ? Object.keys(out.modules) : out.diagnostics ? [...new Set([entry, ...out.diagnostics.map(d => d.file)])] : [entry];