fix(codemode): say why an import can't be resolved (#64587)

This commit is contained in:
Aiden Cline
2026-11-17 22:45:39 -06:00
committed by GitHub
Unverified
parent a5533f7bdf
commit db8730ebcd
3 changed files with 34 additions and 9 deletions
@@ -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<R> {
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<R> {
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.`
}
@@ -18,6 +18,20 @@ const aliases: Record<string, (global: (name: string) => 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,
+10 -2
View File
@@ -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.`,
)
})
})