diff --git a/.changeset/22225-console-not-built-loud.md b/.changeset/22225-console-not-built-loud.md new file mode 100644 index 00000000000..6d935c4d1a0 --- /dev/null +++ b/.changeset/22225-console-not-built-loud.md @@ -0,0 +1,16 @@ +--- +'@objectstack/cli': patch +--- + +fix(cli): with no built Console, `/_console/` says the Console is not built and names the remedy, instead of answering a bare 404 (#22225) + +Clause-②: no + +When the Console package `os serve` resolves has no built `dist/`, the server printed the "Console dist not found" boot warning and mounted nothing. `GET /_console/` then answered the router's `ENDPOINT_NOT_FOUND` and `GET /` an empty 404, which look exactly like a wrong URL. + +Now, on that boot, the server mounts the Console's not-built answer on the routes the Console would own: + +- **`GET /_console/` and every path under it** answer `503` with a short plain-text body. It says the Console is not built, then gives the remedy the boot warning names: `pnpm objectui:build` in a framework checkout, or reinstalling `@objectstack/cli`'s `@objectstack/console` dependency elsewhere. The same function picks the remedy for both. The HTTP form leaves out the absolute paths on the host, which only the terminal shows. +- **`GET /` and `GET /_console`** redirect to `/_console/`, as they do when the Console is built. + +A built Console is served exactly as before, and `--no-ui` / `--no-console` still mount nothing. The `--ui` flag's help text and the README row now name `@objectstack/console`, the package the resolver reads, instead of `@object-ui/console`. diff --git a/packages/cli/README.md b/packages/cli/README.md index b2f2536adf1..11d4423881c 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -241,7 +241,7 @@ There are no short forms: `os -h` and `os -v` exit 2 with `command -h not found` - `-p, --port ` — Server port. Resolution: `--port` › `$OS_PORT` › `$PORT` › `3000`. With `--dev` a busy port auto-hops to the next free one; in production mode it's a hard error (never silently drifts). - `--dev` — Run in development mode (load devPlugins, pretty logging) -- `--ui` — Enable the bundled Console portal at `/_console/` when `@object-ui/console` is installed (default: true) +- `--ui` — Enable the bundled Console portal at `/_console/`, served from `@objectstack/console` (default: true). With no built Console, `/_console/` answers `503` naming how to build or reinstall it. - `--no-server` — Skip starting HTTP server plugin ### `os generate` diff --git a/packages/cli/src/commands/serve.ts b/packages/cli/src/commands/serve.ts index 8c8420b0604..4aa02f5a3a5 100644 --- a/packages/cli/src/commands/serve.ts +++ b/packages/cli/src/commands/serve.ts @@ -157,6 +157,7 @@ import { formatConsoleShaDriftRefusal, formatConsoleDistMissingWarning, createConsoleStaticPlugin, + createConsoleNotBuiltPlugin, createRuntimeAssetsPlugin, type ConsoleShaDrift, } from '../utils/console.js'; @@ -1197,7 +1198,7 @@ export default class Serve extends Command { static override flags = { port: Flags.string({ char: 'p', description: 'Server port', default: readEnvWithDeprecation('OS_PORT', 'PORT', { silent: true }) ?? '3000' }), dev: Flags.boolean({ description: 'Run in development mode (load devPlugins)' }), - ui: Flags.boolean({ description: 'Enable the bundled Console portal at /_console/ when @object-ui/console is installed (default: true)', default: true, allowNo: true }), + ui: Flags.boolean({ description: 'Enable the bundled Console portal at /_console/, served from @objectstack/console (default: true)', default: true, allowNo: true }), console: Flags.boolean({ description: 'Mount the Console UI at /_console/ when the package is installed (default: true).', default: true, @@ -5088,12 +5089,16 @@ export default class Serve extends Command { }); // ── Console portal ────────────────────────────────────────── - // The opinionated, fork-ready runtime console (`@object-ui/console`, - // published from the objectstack-ai/objectui monorepo) mounts under - // `/_console/`. When present, it owns the root `/` redirect - // (preferred default UI). It is optional — we only mount it when - // the package resolves and a pre-built `dist/` is present, and — in - // dev — only when that build matches the repo's objectui pin (#7752). + // The opinionated, fork-ready runtime console (`@objectstack/console`, + // the build of objectstack-ai/objectui's console app this framework + // vendors at its `.objectui-sha` pin) mounts under `/_console/`. When + // present, it owns the root `/` redirect (preferred default UI). It is + // optional — we only mount it when the package resolves and a + // pre-built `dist/` is present, and — in dev — only when that build + // matches the repo's objectui pin (#7752). A package that resolves + // with no `dist/` gets the not-built answer on those same routes, so + // `/_console/` names the remedy the boot warning prints instead of + // answering a bare 404. if (consolePath) { if (consoleWillMount) { if (consoleDrift) { @@ -5106,6 +5111,8 @@ export default class Serve extends Command { console.error(chalk.red(formatConsoleShaDriftRefusal(consoleDrift))); } else { console.warn(chalk.yellow(formatConsoleDistMissingWarning(consolePath))); + await kernel.use(createConsoleNotBuiltPlugin(consolePath)); + trackPlugin('ConsoleNotBuilt'); } } } diff --git a/packages/cli/src/utils/console.not-built.test.ts b/packages/cli/src/utils/console.not-built.test.ts new file mode 100644 index 00000000000..9fca308ec75 --- /dev/null +++ b/packages/cli/src/utils/console.not-built.test.ts @@ -0,0 +1,203 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * What a server with no built Console answers at `/_console/` and at `/`. + * + * Before the not-built answer existed, `os serve` mounted nothing when the + * console package resolved without a `dist/`: `GET /_console/` got the + * router's bare not-found and `GET /` an empty 404, the same answers a wrong + * URL gets, and only the boot warning said why. `os serve` now mounts + * `createConsoleNotBuiltPlugin` on that boot, and these tests pin what it + * answers: + * + * - `GET /_console/` (and every path under it) answers `503` with the remedy + * the boot warning names — the same sentence from the same picker, with + * the host's absolute paths left out; + * - `GET /` and `GET /_console` redirect to `/_console/`, exactly as they do + * once the Console is built (the control below), so neither route changes + * owner with the build. + * + * These tests run the REAL plugins on a REAL Hono app (the `HonoHttpServer` + * that production resolves as `http.server`). + */ + +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; + +import { + createConsoleNotBuiltPlugin, + createConsoleStaticPlugin, + formatConsoleDistMissingWarning, +} from './console.js'; + +const ORIGIN = 'http://console.example.test'; + +/** The first line of the not-built answer, which says what is wrong before saying how to fix it. */ +const NOT_BUILT_HEADLINE = 'The ObjectStack Console is not built, so this server has no Console to serve at /_console/.'; + +/** The framework repo's remedy. */ +const BUILD_REMEDY = 'pnpm objectui:build'; + +/** The prefix the terminal warning carries and the HTTP answer does not. */ +const WARNING_PREFIX = ' ⚠ '; + +/** Requests the not-built answer must cover: the shell, a client-side route, and a hashed asset. */ +const CONSOLE_REQUESTS = ['/_console/', '/_console/apps/crm/records/42', '/_console/assets/index-ocmkyCt6.js']; + +type Request = (p: string, init?: RequestInit) => Promise; + +let scratch: string; + +beforeAll(() => { + scratch = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'os-test-console-not-built-'))); +}); + +afterAll(() => { + fs.rmSync(scratch, { recursive: true, force: true }); +}); + +function writeJson(file: string, value: unknown): void { + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync(file, JSON.stringify(value)); +} + +/** A console package with a manifest and no `dist/` — what the boot warning is printed for. */ +function writeUnbuiltConsole(dir: string): string { + writeJson(path.join(dir, 'package.json'), { name: '@objectstack/console', version: '1.0.0' }); + return dir; +} + +/** The framework repo's shape: the root manifest declares the console build script. */ +function frameworkTree(): { root: string; consoleDir: string } { + const root = path.join(scratch, 'framework'); + writeJson(path.join(root, 'package.json'), { + name: 'framework-root', + private: true, + scripts: { build: 'turbo run build', 'objectui:build': 'bash scripts/build-console.sh' }, + }); + return { root, consoleDir: writeUnbuiltConsole(path.join(root, 'packages', 'console')) }; +} + +/** A project that installed the CLI, whose `@objectstack/console` arrived without its `dist/`. */ +function installTree(): { project: string; consoleDir: string } { + const project = path.join(scratch, 'app'); + writeJson(path.join(project, 'package.json'), { name: 'consumer-app', scripts: { dev: 'objectstack dev' } }); + return { + project, + consoleDir: writeUnbuiltConsole(path.join(project, 'node_modules', '@objectstack', 'console')), + }; +} + +async function serve(plugin: { start: (ctx: any) => Promise }): Promise { + const server = new HonoHttpServer(0); + await plugin.start({ getServiceAsync: async () => server, logger: { warn: () => {} } }); + const app = server.getRawApp(); + return async (p, init) => app.request(`${ORIGIN}${p}`, init); +} + +/** `/` and `/_console` redirect to the Console's shell, built or not. */ +async function expectRedirectsToConsole(request: Request): Promise { + for (const p of ['/', '/_console']) { + const res = await request(p); + expect(res.status, `GET ${p}`).toBe(302); + expect(res.headers.get('location'), `GET ${p}`).toBe('/_console/'); + } +} + +describe('a framework checkout with no built Console', () => { + let request: Request; + let root: string; + let consoleDir: string; + beforeAll(async () => { + ({ root, consoleDir } = frameworkTree()); + request = await serve(createConsoleNotBuiltPlugin(consoleDir)); + }); + + it.each(CONSOLE_REQUESTS)('GET %s answers 503 naming `pnpm objectui:build`', async (p) => { + const res = await request(p); + expect(res.status).toBe(503); + expect(res.headers.get('content-type')).toMatch(/^text\/plain/); + expect(res.headers.get('cache-control')).toBe('no-store'); + const body = await res.text(); + expect(body.split('\n')[0]).toBe(NOT_BUILT_HEADLINE); + expect(body).toContain(BUILD_REMEDY); + }); + + it('is the boot warning\'s sentence, with only the absolute build root left out', async () => { + const warning = formatConsoleDistMissingWarning(consoleDir); + expect(warning.startsWith(WARNING_PREFIX)).toBe(true); + // The warning names the root to run the script in; the HTTP answer says + // where in words. Everything either side of that path is the same text. + const [before, after] = warning.slice(WARNING_PREFIX.length).split(` in ${root} `); + expect(after, 'the warning no longer names the build root this test splits on').toBeDefined(); + + const body = await (await request('/_console/')).text(); + expect(body).toContain(before); + expect(body).toContain(after); + expect(body).toContain(path.join('packages', 'console', 'dist')); + expect(body).not.toContain(root); + }); + + it('redirects `/` and `/_console` to `/_console/`, as a built Console does', async () => { + await expectRedirectsToConsole(request); + }); +}); + +describe('an installed CLI whose `@objectstack/console` has no `dist/`', () => { + let request: Request; + let project: string; + let consoleDir: string; + beforeAll(async () => { + ({ project, consoleDir } = installTree()); + request = await serve(createConsoleNotBuiltPlugin(consoleDir)); + }); + + it('answers 503 with the reinstall remedy, the boot warning\'s sentence without the dist path', async () => { + const warning = formatConsoleDistMissingWarning(consoleDir); + const dist = path.join(consoleDir, 'dist'); + const [before, after] = warning.slice(WARNING_PREFIX.length).split(` at ${dist}`); + expect(after, 'the warning no longer names the dist path this test splits on').toBeDefined(); + + const res = await request('/_console/'); + expect(res.status).toBe(503); + const body = await res.text(); + expect(body.split('\n')[0]).toBe(NOT_BUILT_HEADLINE); + expect(body).toContain(`${before}${after}`); + expect(body).toContain('@objectstack/console'); + expect(body).not.toContain(BUILD_REMEDY); + expect(body).not.toContain(project); + }); + + it('redirects `/` and `/_console` to `/_console/`', async () => { + await expectRedirectsToConsole(request); + }); +}); + +describe('controls', () => { + it('a BUILT Console serves its SPA shell at `/_console/` and the same redirects', async () => { + const dist = path.join(scratch, 'built', 'dist'); + fs.mkdirSync(dist, { recursive: true }); + fs.writeFileSync(path.join(dist, 'index.html'), 'console shell'); + const request = await serve(createConsoleStaticPlugin(dist)); + + const res = await request('/_console/'); + expect(res.status).toBe(200); + expect(res.headers.get('content-type')).toMatch(/^text\/html/); + expect(await res.text()).toContain('console shell'); + await expectRedirectsToConsole(request); + }); + + it('with no Console plugin mounted, both paths are the bare 404 the not-built answer replaces', async () => { + // What `--no-ui` composes, and what a not-built boot composed before: the + // answers above come from the plugin, not from the server. + const request = await serve({ start: async () => {} }); + for (const p of ['/', '/_console/']) { + const res = await request(p); + expect(res.status, `GET ${p}`).toBe(404); + expect(await res.text(), `GET ${p}`).not.toContain(NOT_BUILT_HEADLINE); + } + }); +}); diff --git a/packages/cli/src/utils/console.ts b/packages/cli/src/utils/console.ts index 66279f318c2..0a8b6a55bd4 100644 --- a/packages/cli/src/utils/console.ts +++ b/packages/cli/src/utils/console.ts @@ -334,7 +334,10 @@ function findConsoleBuildRoot(consolePath: string): string | null { } /** - * The boot warning for a resolved console package with no built `dist/`. + * What is missing and what fixes it, for a resolved console package with no + * built `dist/` — the ONE remedy picker, which both the boot warning and the + * HTTP answer render ({@link formatConsoleDistMissingWarning}, + * {@link createConsoleNotBuiltPlugin}). * * Two places reach it, and each gets the remedy that works there: * @@ -347,23 +350,35 @@ function findConsoleBuildRoot(consolePath: string): string | null { * remedy is that package install. (An installed `@object-ui/console`, * which this text used to name, is never consulted: that name is matched * only as the workspace package of a sibling `../objectui` checkout.) + * + * `hostPaths` decides whether the text names absolute paths on this host. The + * terminal's reader is the operator, who is told exactly where to look. The + * HTTP answer's reader is whoever requests `/_console/`, with no credential, so + * that form names no absolute path: the remedy (the script or the package to + * reinstall) is the same sentence either way, and the repository-relative + * `packages/console/dist` stays because it is the repository's public layout. */ -export function formatConsoleDistMissingWarning(consolePath: string): string { +function describeConsoleDistMissing(consolePath: string, hostPaths: boolean): string { const dist = path.join(consolePath, 'dist'); const buildRoot = findConsoleBuildRoot(consolePath); if (buildRoot) { return ( - ` ⚠ Console dist not found at ${path.relative(buildRoot, dist)} — \`pnpm build\` does not produce it. ` + - `Build it with \`pnpm ${CONSOLE_BUILD_SCRIPT}\` in ${buildRoot} ` + + `Console dist not found at ${path.relative(buildRoot, dist)} — \`pnpm build\` does not produce it. ` + + `Build it with \`pnpm ${CONSOLE_BUILD_SCRIPT}\` in ${hostPaths ? buildRoot : 'the root of the ObjectStack checkout this server runs from'} ` + `(uses a ../objectui checkout if present, else clones objectui at the pinned commit — needs network).` ); } return ( - ` ⚠ Console dist not found at ${dist} — the Console ships prebuilt in \`${CONSOLE_PACKAGE}\`, ` + + `Console dist not found${hostPaths ? ` at ${dist}` : ''} — the Console ships prebuilt in \`${CONSOLE_PACKAGE}\`, ` + `a dependency of \`@objectstack/cli\`; reinstall it at the same version as the CLI.` ); } +/** The boot warning for a resolved console package with no built `dist/`. */ +export function formatConsoleDistMissingWarning(consolePath: string): string { + return ` ⚠ ${describeConsoleDistMissing(consolePath, true)}`; +} + // ─── objectui-SHA Drift Guard (dev monorepo only) ─────────────────── /** @@ -792,6 +807,56 @@ export function createConsoleStaticPlugin(distPath: string, options?: { isDev?: }; } +/** + * What `/_console/` answers when the console package resolved but has no built + * `dist/` — mounted by `os serve` in place of {@link createConsoleStaticPlugin}, + * on the boot that prints {@link formatConsoleDistMissingWarning}. + * + * Without it nothing was mounted, so `GET /_console/` got the router's bare + * `ENDPOINT_NOT_FOUND` and `GET /` an empty 404: a missing build read exactly + * like a wrong URL, and only the terminal said why (AGENTS.md, Route & surface + * ownership rule 3, "Absence must be loud"). It mounts the same three routes the + * static plugin would, so who owns each path does not change with the build: + * + * - `GET /` and `GET /_console` redirect to `/_console/`, as they do once the + * Console is built — so `/` keeps one owner and one answer, the Console's; + * - `GET /_console/*` answers `503` with a short `text/plain` body saying the + * Console is not built, then the remedy {@link describeConsoleDistMissing} + * picked for the warning, rendered without host paths. + * + * `503`, not `404`: the address is right and the server is missing a build it + * needs, which the operator fixes and a caller cannot — and a caller reading + * only the status still tells it apart from a wrong URL. Plain text rather than + * a JSON envelope: these are the Console's static-asset routes, not API + * surface (`console-route-ledger.ts`), read by a browser or by `curl`. + * `no-store`, so a browser never keeps the answer past the restart that serves + * the real Console. + */ +export function createConsoleNotBuiltPlugin(consolePath: string) { + const answer = + `The ObjectStack Console is not built, so this server has no Console to serve at ${CONSOLE_PATH}/.\n\n` + + `${describeConsoleDistMissing(consolePath, false)}\n\n` + + 'Then restart the server. The API is served either way.\n'; + return { + name: 'com.objectstack.console-not-built', + + init: async () => {}, + + start: async (ctx: any) => { + const httpServer = await resolveHttpServer(ctx); + if (!httpServer?.getRawApp) { + ctx.logger?.warn?.('Console not-built answer: http.server service not found — skipping'); + return; + } + + const app = httpServer.getRawApp(); + app.get('/', (c: any) => c.redirect(`${CONSOLE_PATH}/`)); + app.get(CONSOLE_PATH, (c: any) => c.redirect(`${CONSOLE_PATH}/`)); + app.get(`${CONSOLE_PATH}/*`, (c: any) => c.text(answer, 503, { 'cache-control': 'no-store' })); + }, + }; +} + // ─── Runtime Assets Plugin ────────────────────────────────────────── /** The URL prefix the runtime assets route is mounted under. */ diff --git a/packages/cli/test/published-subpath-console.pin.test.ts b/packages/cli/test/published-subpath-console.pin.test.ts index fe242864b14..b36ca275d6d 100644 --- a/packages/cli/test/published-subpath-console.pin.test.ts +++ b/packages/cli/test/published-subpath-console.pin.test.ts @@ -175,7 +175,7 @@ const RETIRED_RUNTIME_NAMES = [ * them off the published subpath. Publishing one is the same deliberate act as * re-admitting a retired name. */ -const INTERNAL_SINCE_RETIREMENT = ['formatConsoleDistMissingWarning']; +const INTERNAL_SINCE_RETIREMENT = ['createConsoleNotBuiltPlugin', 'formatConsoleDistMissingWarning']; /** * Every subpath the published package resolves, in full. A subpath removed here