|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { |
| 6 | + classifyRequiredCapability, |
| 7 | + type CapabilityClassification, |
| 8 | +} from '@objectstack/spec/kernel'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Installable-provider preflight (framework#3366). |
| 12 | + * |
| 13 | + * A capability listed in `requires: [...]` is fail-fast at `serve` time when its |
| 14 | + * provider package is missing — but the generic "not installed, add it to your |
| 15 | + * dependencies" advice is un-followable when the provider has **no installable |
| 16 | + * version in the current edition** (e.g. `ai` → `@objectstack/service-ai`, which |
| 17 | + * went cloud-only in 11.3.0 / ADR-0025). Neither `os validate` nor `os build` |
| 18 | + * caught it, because neither resolves providers or boots the runtime. |
| 19 | + * |
| 20 | + * This module resolves each declared capability's provider the same way `serve` |
| 21 | + * loads it and classifies the result via the spec-owned |
| 22 | + * {@link classifyRequiredCapability}, so a shift-left gate (`os build` / |
| 23 | + * `os validate`) and the `serve` boot error read identically. |
| 24 | + */ |
| 25 | + |
| 26 | +// ── Provider resolution ───────────────────────────────────────────────────── |
| 27 | + |
| 28 | +/** True when `err` is a "module not exported / not found" resolution failure. */ |
| 29 | +function isResolveMiss(err: unknown): boolean { |
| 30 | + const code = (err as NodeJS.ErrnoException | undefined)?.code; |
| 31 | + // A package that IS installed but whose `exports` map doesn't expose the bare |
| 32 | + // entry under the `require` condition (import-only) still counts as installed. |
| 33 | + return code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Build an `isInstalled(pkg)` predicate that resolves a provider package the way |
| 38 | + * `os serve` actually loads it (`importFromHost`): first from the HOST APP's |
| 39 | + * dir — a locally-linked service or an enterprise plugin the app installed — then |
| 40 | + * from the CLI's own module graph, where the framework `@objectstack/*` providers |
| 41 | + * live as dependencies (serve's bare-import fallback). Resolution never |
| 42 | + * imports/executes the package, so it is cheap and side-effect-free. |
| 43 | + */ |
| 44 | +export function makeProviderResolver(projectDir: string): (pkg: string) => boolean { |
| 45 | + // Anchoring on `<dir>/package.json` only sets the resolution base — the file |
| 46 | + // itself need not exist (Node walks up node_modules from that directory). |
| 47 | + const hostRequire = createRequire(path.join(projectDir, 'package.json')); |
| 48 | + const cliRequire = createRequire(import.meta.url); |
| 49 | + const resolvableFrom = (req: NodeRequire, pkg: string): boolean => { |
| 50 | + try { |
| 51 | + req.resolve(pkg); |
| 52 | + return true; |
| 53 | + } catch (err) { |
| 54 | + return !isResolveMiss(err); |
| 55 | + } |
| 56 | + }; |
| 57 | + return (pkg: string) => resolvableFrom(hostRequire, pkg) || resolvableFrom(cliRequire, pkg); |
| 58 | +} |
| 59 | + |
| 60 | +// ── Message rendering ──────────────────────────────────────────────────────── |
| 61 | + |
| 62 | +/** |
| 63 | + * The one-line, actionable message for a classified capability. Shared by the |
| 64 | + * build/validate preflight and the serve boot error so both read identically. |
| 65 | + * Only `installable` / `unavailable` / `unknown` produce a message; `ok` is |
| 66 | + * satisfied and never surfaced. |
| 67 | + */ |
| 68 | +export function renderCapabilityMessage(c: CapabilityClassification): string { |
| 69 | + const p = c.provider; |
| 70 | + switch (c.status) { |
| 71 | + case 'installable': { |
| 72 | + const pkg = p!.package!; |
| 73 | + if (p!.edition === 'enterprise') { |
| 74 | + const note = p!.note ? ` (${p!.note})` : ''; |
| 75 | + return ( |
| 76 | + `Capability "${c.token}" is provided by ${pkg}${note}. ` + |
| 77 | + `Run \`pnpm add ${pkg}\` and add it to \`plugins[]\`, or remove "${c.token}" from \`requires\`.` |
| 78 | + ); |
| 79 | + } |
| 80 | + return ( |
| 81 | + `Capability "${c.token}" requires ${pkg}, which is not installed. ` + |
| 82 | + `Run \`pnpm add ${pkg}\`, or remove "${c.token}" from \`requires\`.` |
| 83 | + ); |
| 84 | + } |
| 85 | + case 'unavailable': { |
| 86 | + const detail = p?.note ? ` (${p.note})` : ''; |
| 87 | + const lead = p?.package |
| 88 | + ? `Capability "${c.token}" resolves to ${p.package}, which is not available in the open edition${detail}.` |
| 89 | + : `Capability "${c.token}" is provided only by a cloud runtime and has no open-edition provider${detail}.`; |
| 90 | + return ( |
| 91 | + `${lead} ` + |
| 92 | + `Remove "${c.token}" from \`requires\`, or run under a cloud runtime that provides the "${c.token}" tier.` |
| 93 | + ); |
| 94 | + } |
| 95 | + case 'unknown': |
| 96 | + return `requires: "${c.token}" is not a known platform capability — check for a typo.`; |
| 97 | + case 'ok': |
| 98 | + default: |
| 99 | + return `Capability "${c.token}" is satisfied.`; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// ── Preflight (build / validate) ───────────────────────────────────────────── |
| 104 | + |
| 105 | +export interface CapabilityPreflightResult { |
| 106 | + /** |
| 107 | + * FATAL findings (`status: 'unavailable'`) — a declared capability whose |
| 108 | + * provider has no installable version in the active edition. Fails the build. |
| 109 | + */ |
| 110 | + readonly errors: CapabilityClassification[]; |
| 111 | + /** |
| 112 | + * Advisory findings — `installable` (absent but addable → `pnpm add` hint) and |
| 113 | + * `unknown` (a typo). Never fatal. |
| 114 | + */ |
| 115 | + readonly warnings: CapabilityClassification[]; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Classify every DECLARED `requires` token (deduped) against the resolvable |
| 120 | + * providers. Only explicit declarations are checked — the platform's own |
| 121 | + * auto-injected convenience defaults (`ALWAYS_ON`, `mcp`, …) carry no "required" |
| 122 | + * intent, exactly as `serve` treats them. |
| 123 | + */ |
| 124 | +export function preflightRequiredCapabilities(opts: { |
| 125 | + requires: readonly unknown[]; |
| 126 | + projectDir: string; |
| 127 | + /** Injectable for tests; defaults to on-disk `require.resolve` resolution. */ |
| 128 | + isInstalled?: (pkg: string) => boolean; |
| 129 | +}): CapabilityPreflightResult { |
| 130 | + const isInstalled = opts.isInstalled ?? makeProviderResolver(opts.projectDir); |
| 131 | + const errors: CapabilityClassification[] = []; |
| 132 | + const warnings: CapabilityClassification[] = []; |
| 133 | + const seen = new Set<string>(); |
| 134 | + for (const token of opts.requires) { |
| 135 | + if (typeof token !== 'string' || seen.has(token)) continue; |
| 136 | + seen.add(token); |
| 137 | + const c = classifyRequiredCapability(token, isInstalled); |
| 138 | + if (c.status === 'unavailable') errors.push(c); |
| 139 | + else if (c.status === 'installable' || c.status === 'unknown') warnings.push(c); |
| 140 | + } |
| 141 | + return { errors, warnings }; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * The fatal one-line message `os serve` throws when a DECLARED capability's |
| 146 | + * provider import fails as module-not-found. The package is already confirmed |
| 147 | + * absent at the throw site, so classification runs against a `false` resolver — |
| 148 | + * yielding the same `installable` / `unavailable` wording the build preflight |
| 149 | + * prints, so boot and preflight read identically (framework#3366). |
| 150 | + */ |
| 151 | +export function missingProviderMessage(token: string): string { |
| 152 | + return renderCapabilityMessage(classifyRequiredCapability(token, () => false)); |
| 153 | +} |
0 commit comments