|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * cloud#1013 — resolving a host-app package from the CLI. |
| 5 | + * |
| 6 | + * The defect: `serve` loaded `@objectstack/organizations` with a BARE |
| 7 | + * `import()`. Node ESM resolves that against the importer's own realpath — the |
| 8 | + * CLI's, inside the framework workspace — while the package is cloud-private |
| 9 | + * and only ever exists in the served app's `node_modules`. It could therefore |
| 10 | + * never resolve, and every walled tenancy posture died on the ADR-0093 D5 |
| 11 | + * fail-fast. |
| 12 | + * |
| 13 | + * These cases run against a REAL fixture app on disk (a real `node_modules`, |
| 14 | + * real resolution, nothing mocked): the first two are the issue's own repro, |
| 15 | + * one half per case — the CLI's resolution cannot see the package, the host |
| 16 | + * app's can. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 20 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 21 | +import { tmpdir } from 'node:os'; |
| 22 | +import { dirname, join, resolve } from 'node:path'; |
| 23 | +import { fileURLToPath } from 'node:url'; |
| 24 | +import { createHostImporter, createHostRequire } from './import-from-host.js'; |
| 25 | + |
| 26 | +/** The cloud-private package at the heart of cloud#1013. */ |
| 27 | +const ORGANIZATIONS = '@objectstack/organizations'; |
| 28 | +/** A package that fails while it EVALUATES — not while it resolves. */ |
| 29 | +const BROKEN = '@fixture/throws-on-load'; |
| 30 | + |
| 31 | +/** `packages/cli` — what a bare `import()` inside the CLI resolves against. */ |
| 32 | +const CLI_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); |
| 33 | + |
| 34 | +let hostRoot: string; |
| 35 | + |
| 36 | +function writeFixturePackage(root: string, name: string, indexJs: string): void { |
| 37 | + const dir = join(root, 'node_modules', ...name.split('/')); |
| 38 | + mkdirSync(dir, { recursive: true }); |
| 39 | + writeFileSync( |
| 40 | + join(dir, 'package.json'), |
| 41 | + JSON.stringify({ name, version: '0.0.0-fixture', type: 'module', main: 'index.js' }), |
| 42 | + 'utf8', |
| 43 | + ); |
| 44 | + writeFileSync(join(dir, 'index.js'), indexJs, 'utf8'); |
| 45 | +} |
| 46 | + |
| 47 | +beforeAll(() => { |
| 48 | + // A host app exactly as the fix expects one: it DECLARES the enterprise |
| 49 | + // package and has it installed in its own node_modules. The framework |
| 50 | + // workspace the CLI lives in has neither. |
| 51 | + hostRoot = mkdtempSync(join(tmpdir(), 'os-import-from-host-')); |
| 52 | + writeFileSync( |
| 53 | + join(hostRoot, 'package.json'), |
| 54 | + JSON.stringify({ |
| 55 | + name: 'host-app-fixture', |
| 56 | + type: 'module', |
| 57 | + dependencies: { [ORGANIZATIONS]: '*' }, |
| 58 | + }), |
| 59 | + 'utf8', |
| 60 | + ); |
| 61 | + writeFixturePackage( |
| 62 | + hostRoot, |
| 63 | + ORGANIZATIONS, |
| 64 | + 'export class OrganizationsPlugin { name = "com.objectstack.organizations"; }\n', |
| 65 | + ); |
| 66 | + writeFixturePackage(hostRoot, BROKEN, 'throw new Error("fixture package exploded on import");\n'); |
| 67 | +}); |
| 68 | + |
| 69 | +afterAll(() => { |
| 70 | + if (hostRoot) rmSync(hostRoot, { recursive: true, force: true }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('host-app package resolution (cloud#1013)', () => { |
| 74 | + it('the CLI\'s own resolution cannot see a host-only package — the defect', () => { |
| 75 | + // Literally the issue's repro, from `packages/cli`: |
| 76 | + // node -e "require.resolve('@objectstack/organizations')" -> MODULE_NOT_FOUND |
| 77 | + // A bare `import()` in serve.ts resolved from exactly here, which is why |
| 78 | + // declaring the dependency in the app changed nothing. |
| 79 | + expect(() => createHostRequire(CLI_ROOT).resolve(ORGANIZATIONS)).toThrow( |
| 80 | + /Cannot find module/, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('resolves a package that exists ONLY in the host app', async () => { |
| 85 | + const importFromHost = createHostImporter(createHostRequire(hostRoot)); |
| 86 | + const mod = await importFromHost(ORGANIZATIONS); |
| 87 | + // The export `serve` constructs: `new mod.OrganizationsPlugin()`. |
| 88 | + expect(typeof mod.OrganizationsPlugin).toBe('function'); |
| 89 | + expect(new mod.OrganizationsPlugin().name).toBe('com.objectstack.organizations'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('falls back to the CLI\'s own resolution when the host cannot resolve', async () => { |
| 93 | + // Whatever the host app cannot see must still load from the CLI's own |
| 94 | + // dependencies — that fallback is what keeps every framework-owned load in |
| 95 | + // `serve` (plugin-auth, plugin-security, service-i18n, …) working exactly |
| 96 | + // as before. Modelled with a host `require` that resolves nothing, because |
| 97 | + // a real one cannot: vitest exports NODE_PATH into the test process, so |
| 98 | + // every package in the workspace store resolves from any directory. |
| 99 | + const blindHostRequire = { |
| 100 | + resolve(pkg: string): string { |
| 101 | + throw Object.assign(new Error(`Cannot find module '${pkg}'`), { code: 'MODULE_NOT_FOUND' }); |
| 102 | + }, |
| 103 | + } as unknown as NodeRequire; |
| 104 | + const mod = await createHostImporter(blindHostRequire)('chalk'); |
| 105 | + expect(typeof mod.default.green).toBe('function'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('reports a package that neither can resolve as module-not-found', async () => { |
| 109 | + const importFromHost = createHostImporter(createHostRequire(hostRoot)); |
| 110 | + // Callers classify "missing vs crashed" off this error (Serve. |
| 111 | + // isModuleNotFoundError), so the absent case must stay recognisable. |
| 112 | + await expect(importFromHost('@fixture/nowhere-at-all')).rejects.toThrow( |
| 113 | + /Cannot find (module|package)|Failed to (load|resolve)/, |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('propagates an evaluation crash instead of masking it as module-not-found', async () => { |
| 118 | + // A host-resolved package that THROWS while loading is a broken package, |
| 119 | + // not a missing one. Re-importing it bare (the shape this helper replaced) |
| 120 | + // would swap the real cause for a MODULE_NOT_FOUND, which every caller |
| 121 | + // reads as "not installed" — a crash silently downgraded to a skip, or a |
| 122 | + // fatal telling the operator to install what is already installed. |
| 123 | + const importFromHost = createHostImporter(createHostRequire(hostRoot)); |
| 124 | + await expect(importFromHost(BROKEN)).rejects.toThrow(/fixture package exploded on import/); |
| 125 | + }); |
| 126 | +}); |
0 commit comments