Skip to content

Commit db8730e

Browse files
authored
fix(codemode): say why an import can't be resolved (anomalyco#64587)
1 parent a5533f7 commit db8730e

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎packages/codemode/src/language/runtime/interpreter.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { globals } from "./globals.js"
44
import type { Builtins } from "./intrinsics.js"
55
import { IntrinsicNames } from "./intrinsic-names.js"
66
import { type AstNode, type Binding, PendingThrow } from "./model.js"
7-
import { moduleName, resolveModule } from "./modules.js"
7+
import { isRuntimeModule, moduleName, resolveModule } from "./modules.js"
88
import { define, entries, type HostNamespace, makingIntrinsics, nativeRule, Obj, type Value } from "./objects.js"
99
import type { HostResult } from "../host.js"
1010
import { Prelude } from "../prelude/prelude.js"
@@ -88,12 +88,7 @@ export class Interpreter<R> {
8888
const cached = this.modules.get(name)
8989
if (cached !== undefined) return cached
9090
const module = resolveModule(name, (global) => this.globalScope.get(global)?.value, this.moduleGlobals)
91-
if (module === undefined)
92-
throw new PendingThrow(
93-
"Error",
94-
`Cannot import "${specifier}": it isn't available here, where packages and files can't be loaded. Use the provided globals.`,
95-
site,
96-
)
91+
if (module === undefined) throw new PendingThrow("Error", cannotImport(specifier), site)
9792
this.modules.set(name, module)
9893
return module
9994
}
@@ -135,3 +130,11 @@ export class Interpreter<R> {
135130
return this.sources.get(id.source)?.node(id)
136131
}
137132
}
133+
134+
/** Why `specifier` can't be loaded, split so the error names the kind of import that failed. */
135+
function cannotImport(specifier: string): string {
136+
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("file:"))
137+
return `Cannot import "${specifier}": local files can't be imported.`
138+
if (isRuntimeModule(specifier)) return `Cannot import "${specifier}": this runtime doesn't provide it.`
139+
return `Cannot import "${specifier}": packages can't be installed here.`
140+
}

‎packages/codemode/src/language/runtime/modules.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ const aliases: Record<string, (global: (name: string) => Value) => Value> = {
1818
/** `node:fs` names the same module as `fs`. */
1919
export const moduleName = (specifier: string) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier)
2020

21+
// Node's core modules, fixed here so every host gives the same answer (Bun's `isBuiltin` also claims `ws` and `undici`).
22+
const core = new Set(
23+
"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(
24+
" ",
25+
),
26+
)
27+
28+
/** Whether a specifier names a module a runtime provides, as Node's and Bun's own modules are, rather than a package. */
29+
export const isRuntimeModule = (specifier: string) =>
30+
specifier.startsWith("node:") ||
31+
specifier === "bun" ||
32+
specifier.startsWith("bun:") ||
33+
core.has(specifier.split("/")[0]!)
34+
2135
/** `hosted` names the global a host's own module names load (`{ bun: "Bun" }`). */
2236
export const resolveModule = (
2337
specifier: string,

‎packages/codemode/test/extensions.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,15 +630,23 @@ describe("imports", () => {
630630
test("a module that isn't provided, or a missing export, fails at the import", async () => {
631631
const target = CodeMode.make({ extensions: [files] })
632632
expect((await failure(`import yaml from "yaml"`, target)).message).toStartWith(
633-
`Error: Cannot import "yaml": it isn't available here, where packages and files can't be loaded.`,
633+
`Error: Cannot import "yaml": packages can't be installed here.`,
634+
)
635+
expect((await failure(`import ws from "ws"`, target)).message).toStartWith(
636+
`Error: Cannot import "ws": packages can't be installed here.`,
637+
)
638+
expect((await failure(`import x from "./local.js"`, target)).message).toStartWith(
639+
`Error: Cannot import "./local.js": local files can't be imported.`,
634640
)
635641
expect(await value(`try { await import("child_process") } catch (e) { return e.message }`, target)).toStartWith(
636642
`Cannot import "child_process"`,
637643
)
638644
expect((await failure(`import { writeFile } from "fs"`, target)).message).toStartWith(
639645
`SyntaxError: "fs" has no export named "writeFile".`,
640646
)
641-
expect((await failure(`require("fs")`)).message).toStartWith(`Error: Cannot import "fs"`)
647+
expect((await failure(`require("fs")`)).message).toStartWith(
648+
`Error: Cannot import "fs": this runtime doesn't provide it.`,
649+
)
642650
})
643651
})
644652

0 commit comments

Comments
 (0)