From 85c7588d708cb70891f5863bc31a0903f6b71bc0 Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Mon, 17 Jul 2023 16:41:12 -0400 Subject: [PATCH] fix: resolves nets ts diagnostic error handling --- cli/resolveNets.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/cli/resolveNets.ts b/cli/resolveNets.ts index 1d6b85b09..8174b65c7 100644 --- a/cli/resolveNets.ts +++ b/cli/resolveNets.ts @@ -5,19 +5,48 @@ import { NetSpec } from "../nets/mod.ts" const $nets = $.record($.instance(NetSpec as new() => NetSpec, $.tuple(), (_: NetSpec) => [])) +const $tsDiagnostics = $.field("diagnosticCodes", $.array($.u32)) + export async function resolveNets(maybeNetsPath?: string): Promise> { const resolvedNetsPath = await resolveNetsPath(maybeNetsPath) if (resolvedNetsPath.endsWith(".ts")) { await register() } // shimmed by dnt - let nets = await _import(resolvedNetsPath) + let nets = await importNets(resolvedNetsPath) if ("default" in nets) nets = nets.default $.assert($nets, nets) for (const key in nets) nets[key]!.name = key return nets } +async function importNets(netsPath: string) { + try { + return _import(netsPath) + } catch (err) { + let errorMessage = `Failed to import nets file ${netsPath}` + if ($.is($tsDiagnostics, err)) { + for (const code of new Set(err.diagnosticCodes)) { + errorMessage += `: Typescript compiler error TS${code}` + switch (code) { + case 2305: + errorMessage += ": Did you forget to import from \"capi/nets\"?" + break + case 2307: + errorMessage += + ": Did you set your tsconfig module to \"ESNext\" and moduleResolution to \"node16\"?" + break + default: + break + } + } + } else { + console.error(err) + } + throw new Error(errorMessage) + } +} + async function resolveNetsPath(maybeNetsPath?: string): Promise { if (maybeNetsPath) return path.resolve(maybeNetsPath) for (const p of ["nets.ts", "nets.js"]) {