From db8730ebcd192fe72e044c2286859d892cce2225 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:45:39 -0600 Subject: [PATCH] fix(codemode): say why an import can't be resolved (#64587) --- .../src/language/runtime/interpreter.ts | 17 ++++++++++------- .../codemode/src/language/runtime/modules.ts | 14 ++++++++++++++ packages/codemode/test/extensions.test.ts | 12 ++++++++++-- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/codemode/src/language/runtime/interpreter.ts b/packages/codemode/src/language/runtime/interpreter.ts index 191b076f3d2..89f6c4021f6 100644 --- a/packages/codemode/src/language/runtime/interpreter.ts +++ b/packages/codemode/src/language/runtime/interpreter.ts @@ -4,7 +4,7 @@ import { globals } from "./globals.js" import type { Builtins } from "./intrinsics.js" import { IntrinsicNames } from "./intrinsic-names.js" import { type AstNode, type Binding, PendingThrow } from "./model.js" -import { moduleName, resolveModule } from "./modules.js" +import { isRuntimeModule, moduleName, resolveModule } from "./modules.js" import { define, entries, type HostNamespace, makingIntrinsics, nativeRule, Obj, type Value } from "./objects.js" import type { HostResult } from "../host.js" import { Prelude } from "../prelude/prelude.js" @@ -88,12 +88,7 @@ export class Interpreter { const cached = this.modules.get(name) if (cached !== undefined) return cached const module = resolveModule(name, (global) => this.globalScope.get(global)?.value, this.moduleGlobals) - if (module === undefined) - throw new PendingThrow( - "Error", - `Cannot import "${specifier}": it isn't available here, where packages and files can't be loaded. Use the provided globals.`, - site, - ) + if (module === undefined) throw new PendingThrow("Error", cannotImport(specifier), site) this.modules.set(name, module) return module } @@ -135,3 +130,11 @@ export class Interpreter { return this.sources.get(id.source)?.node(id) } } + +/** Why `specifier` can't be loaded, split so the error names the kind of import that failed. */ +function cannotImport(specifier: string): string { + if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("file:")) + return `Cannot import "${specifier}": local files can't be imported.` + if (isRuntimeModule(specifier)) return `Cannot import "${specifier}": this runtime doesn't provide it.` + return `Cannot import "${specifier}": packages can't be installed here.` +} diff --git a/packages/codemode/src/language/runtime/modules.ts b/packages/codemode/src/language/runtime/modules.ts index 1d9699c91c8..a89fc75e774 100644 --- a/packages/codemode/src/language/runtime/modules.ts +++ b/packages/codemode/src/language/runtime/modules.ts @@ -18,6 +18,20 @@ const aliases: Record Value) => Value> = { /** `node:fs` names the same module as `fs`. */ export const moduleName = (specifier: string) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier) +// Node's core modules, fixed here so every host gives the same answer (Bun's `isBuiltin` also claims `ws` and `undici`). +const core = new Set( + "assert async_hooks buffer child_process cluster console constants crypto dgram diagnostics_channel dns domain events fs http http2 https inspector module net os path perf_hooks process punycode querystring readline repl stream string_decoder sys timers tls trace_events tty url util v8 vm wasi worker_threads zlib".split( + " ", + ), +) + +/** Whether a specifier names a module a runtime provides, as Node's and Bun's own modules are, rather than a package. */ +export const isRuntimeModule = (specifier: string) => + specifier.startsWith("node:") || + specifier === "bun" || + specifier.startsWith("bun:") || + core.has(specifier.split("/")[0]!) + /** `hosted` names the global a host's own module names load (`{ bun: "Bun" }`). */ export const resolveModule = ( specifier: string, diff --git a/packages/codemode/test/extensions.test.ts b/packages/codemode/test/extensions.test.ts index c6d263af960..a2694bf5415 100644 --- a/packages/codemode/test/extensions.test.ts +++ b/packages/codemode/test/extensions.test.ts @@ -630,7 +630,13 @@ describe("imports", () => { test("a module that isn't provided, or a missing export, fails at the import", async () => { const target = CodeMode.make({ extensions: [files] }) expect((await failure(`import yaml from "yaml"`, target)).message).toStartWith( - `Error: Cannot import "yaml": it isn't available here, where packages and files can't be loaded.`, + `Error: Cannot import "yaml": packages can't be installed here.`, + ) + expect((await failure(`import ws from "ws"`, target)).message).toStartWith( + `Error: Cannot import "ws": packages can't be installed here.`, + ) + expect((await failure(`import x from "./local.js"`, target)).message).toStartWith( + `Error: Cannot import "./local.js": local files can't be imported.`, ) expect(await value(`try { await import("child_process") } catch (e) { return e.message }`, target)).toStartWith( `Cannot import "child_process"`, @@ -638,7 +644,9 @@ describe("imports", () => { expect((await failure(`import { writeFile } from "fs"`, target)).message).toStartWith( `SyntaxError: "fs" has no export named "writeFile".`, ) - expect((await failure(`require("fs")`)).message).toStartWith(`Error: Cannot import "fs"`) + expect((await failure(`require("fs")`)).message).toStartWith( + `Error: Cannot import "fs": this runtime doesn't provide it.`, + ) }) })