diff --git a/packages/proton/src/ast/import-resolver.ts b/packages/proton/src/ast/import-resolver.ts index 70207a77..c8c4509c 100644 --- a/packages/proton/src/ast/import-resolver.ts +++ b/packages/proton/src/ast/import-resolver.ts @@ -102,25 +102,99 @@ function extractExportAllSpecifiers(sf: ts.SourceFile): string[] { return result; } -/** Resolve a relative module specifier to an absolute file path. Returns null if not found. */ +// Compiler options used for bare-specifier module resolution. `Bundler` +// matches modern monorepo + vite tooling: it understands `package.json` +// `exports`, `main`, and workspace symlinks without forcing the caller +// to spell out file extensions. We pass these once per process — the +// only consumer here is `ts.resolveModuleName` and it doesn't mutate +// them. +const BARE_SPECIFIER_COMPILER_OPTIONS: ts.CompilerOptions = { + moduleResolution: ts.ModuleResolutionKind.Bundler, + module: ts.ModuleKind.ESNext, + target: ts.ScriptTarget.Latest, + allowImportingTsExtensions: true, +}; + +// Per-process cache for `ts.resolveModuleName`. Hot paths (rebuild on +// HMR) call this hundreds of times — without the cache each call +// re-walks `node_modules` and `package.json` lookups. +let bareModuleResolutionCache: ts.ModuleResolutionCache | null = null; + +function getBareModuleResolutionCache(): ts.ModuleResolutionCache { + if (!bareModuleResolutionCache) { + bareModuleResolutionCache = ts.createModuleResolutionCache( + process.cwd(), + (s) => s, + BARE_SPECIFIER_COMPILER_OPTIONS, + ); + } + return bareModuleResolutionCache; +} + +/** + * Resolve a module specifier (relative OR bare) to an absolute `.ts` + * source-file path. Returns `null` if the resolution lands on something + * proton can't consume (a `.d.ts` declaration, a `.js` build artifact, + * or just not-found). + * + * Two code paths: + * + * - **Relative** (`./foo`, `../bar`): direct file-existence probe. + * Same behavior as before — fast, no TS dependency. Preserved + * verbatim so that the common case (cross-file imports inside a + * single package) is unaffected by the new bare-specifier logic. + * + * - **Bare** (`@scope/pkg`, `pkg-name`, `~alias`): delegate to + * TypeScript's module resolution so monorepo workspaces / npm + * packages / tsconfig paths / package.json `exports` all just work. + * Required to support proto definitions that live in a separate + * workspace package — e.g. moving `bridge/proto/proton/*.ts` into a + * dedicated `@snowluma/proto-defs` package while keeping the call + * sites in `@snowluma/core`. + * + * We deliberately require the resolved file to be `.ts` (not + * `.d.ts`): proton collects `pb<…>` field tags by walking interface + * AST nodes, and a hand-written `.d.ts` *would* work too — but the + * typical `dist/index.d.ts` strips away the `pb<>` type arguments + * proton needs. Asking proto-definition packages to ship source + * `.ts` (the same model `@snowluma/proton` itself uses) avoids that + * footgun entirely. + */ function resolveModulePath(specifier: string, importerPath: string): string | null { - if (!specifier.startsWith('.')) return null; // skip bare/alias specifiers + if (specifier.startsWith('.')) { + // ── Relative path branch (preserved verbatim from the pre-bare- + // specifier implementation; existing callers depend on its + // exact behavior, including the .ts-only filter). ── + const base = resolve(dirname(importerPath), specifier); - const base = resolve(dirname(importerPath), specifier); + // Try exact path (e.g., './types.ts') + if (existsSync(base) && !base.endsWith('.ts') === false) return base; + if (base.endsWith('.ts') && existsSync(base)) return base; - // Try exact path (e.g., './types.ts') - if (existsSync(base) && !base.endsWith('.ts') === false) return base; - if (base.endsWith('.ts') && existsSync(base)) return base; + // Try appending .ts + const withTs = base + '.ts'; + if (existsSync(withTs)) return withTs; - // Try appending .ts - const withTs = base + '.ts'; - if (existsSync(withTs)) return withTs; + // Try as directory with index.ts + const indexTs = resolve(base, 'index.ts'); + if (existsSync(indexTs)) return indexTs; - // Try as directory with index.ts - const indexTs = resolve(base, 'index.ts'); - if (existsSync(indexTs)) return indexTs; + return null; + } - return null; + // ── Bare specifier branch ── + const result = ts.resolveModuleName( + specifier, + importerPath, + BARE_SPECIFIER_COMPILER_OPTIONS, + ts.sys, + getBareModuleResolutionCache(), + ); + const resolved = result.resolvedModule; + if (!resolved) return null; + // Only accept .ts source — see doc comment above for why. + if (resolved.extension !== ts.Extension.Ts) return null; + return resolved.resolvedFileName; } function hasExportModifier(node: ts.Node): boolean { diff --git a/packages/proton/test/__tests__/cross-package.test.ts b/packages/proton/test/__tests__/cross-package.test.ts new file mode 100644 index 00000000..854bb176 --- /dev/null +++ b/packages/proton/test/__tests__/cross-package.test.ts @@ -0,0 +1,176 @@ +// Cross-package bare-specifier resolution. +// +// Companion to `cross-file.test.ts` — that suite covers relative-path +// imports between sibling files in the same package. Here we exercise +// the OTHER branch of `resolveModulePath`: a TypeScript-driven module +// resolution path that handles bare specifiers (`@scope/pkg`, +// `pkg-name`, …) so monorepo workspaces / npm packages can host proto +// definitions used from a different package. +// +// The motivating use case is decoupling `bridge/proto/proton/*.ts` +// from `@snowluma/core` into a standalone `@snowluma/proto-defs` +// package, so SnowLumaExtended (and any other consumer) can reuse the +// same wire definitions without copy-pasting them. +// +// The fixture is built in `os.tmpdir()` per-test-suite rather than +// committed to `test/fixtures/`. The reason: the resolution lookup +// has to find a real `node_modules/@fake/proto-defs/...` layout, and +// the repo-level `.gitignore` rule `**/node_modules/` would clobber a +// committed copy of that layout. A scratch tmpdir sidesteps the issue +// entirely and self-cleans in `afterAll`. + +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import { mkdtempSync, mkdirSync, writeFileSync, rmSync, readFileSync } from 'fs'; +import { join, resolve } from 'path'; +import { tmpdir } from 'os'; +import { analyze, analyzeSource } from '../../src/ast/analyzer'; +import { generateCode } from '../../src/codegen/generator'; +import { applyReplacements } from '../../src/transform/replacer'; +import { resolveImports, type ParsedFileEntry } from '../../src/ast/import-resolver'; +import { execAndGet } from '../helpers'; + +describe('cross-package import resolution (bare specifier)', () => { + let tmpRoot: string; + let consumerPath: string; + + beforeAll(() => { + // Build a tiny fake monorepo layout: + // + // / + // consumer.ts ← uses `import { RemoteMsg } from '@fake/proto-defs'` + // node_modules/ + // @fake/ + // proto-defs/ + // package.json ← exports → ./src/index.ts + // src/ + // index.ts ← exported interfaces + // + // Bundler-mode `ts.resolveModuleName` walks importer → parent + // dirs looking for `node_modules`, so placing the consumer at + // `/consumer.ts` lets `/node_modules/...` satisfy the + // lookup. + tmpRoot = mkdtempSync(join(tmpdir(), 'proton-cross-pkg-')); + + const pkgDir = resolve(tmpRoot, 'node_modules', '@fake', 'proto-defs'); + mkdirSync(resolve(pkgDir, 'src'), { recursive: true }); + + // `exports."."` must point at a `.ts` source file — proton walks + // interface AST nodes to collect `pb<…>` field tags, and a + // generated `.d.ts` typically strips those generic args. Telling + // consumers to ship source `.ts` is the same model + // `@snowluma/proton` itself uses (`"default": "./src/runtime.ts"`). + writeFileSync( + resolve(pkgDir, 'package.json'), + JSON.stringify({ + name: '@fake/proto-defs', + version: '1.0.0', + type: 'module', + main: './src/index.ts', + types: './src/index.ts', + exports: { + '.': { + types: './src/index.ts', + default: './src/index.ts', + }, + }, + }, null, 2), + ); + + writeFileSync( + resolve(pkgDir, 'src', 'index.ts'), + // proton's `collectInterface` recognises `pb` as a marker + // type by NAME — no actual import of `@snowluma/proton` is + // needed for the fixture to parse. Matches the existing + // cross-file fixtures (`fixtures/cross-file/types.ts` does the + // same). + 'export interface RemoteMsg {\n' + + ' id?: pb<1, uint_32>;\n' + + ' name?: pb<2, string>;\n' + + '}\n', + ); + + consumerPath = resolve(tmpRoot, 'consumer.ts'); + writeFileSync( + consumerPath, + 'import type { RemoteMsg } from \'@fake/proto-defs\';\n' + + '\n' + + 'const buf = protobuf_encode({ id: 42, name: \'alice\' });\n' + + 'const decoded = protobuf_decode(buf);\n' + + '\n' + + 'export { buf, decoded };\n', + ); + }); + + afterAll(() => { + if (tmpRoot) rmSync(tmpRoot, { recursive: true, force: true }); + }); + + it('collects interfaces from a `@fake/proto-defs` bare-specifier import', () => { + // Smoke test: the import resolver MUST find the package and pick + // up `RemoteMsg`. If this fails, `ts.resolveModuleName` likely + // can't find the fake node_modules layout (check that the + // BARE_SPECIFIER_COMPILER_OPTIONS in import-resolver.ts still + // sets `moduleResolution: Bundler`). + const code = readFileSync(consumerPath, 'utf-8'); + const cache = new Map(); + const imported = resolveImports(code, consumerPath, cache); + + expect(imported.concrete.some(m => m.name === 'RemoteMsg')).toBe(true); + }); + + it('rewrites encode/decode call sites against the imported type', () => { + const code = readFileSync(consumerPath, 'utf-8'); + const cache = new Map(); + const imported = resolveImports(code, consumerPath, cache); + + const { registry, callSites, sourceFile } = analyze(code, consumerPath, imported); + expect(registry.has('RemoteMsg')).toBe(true); + + const { transformedCode, hasReplacements } = applyReplacements(code, sourceFile, callSites, registry); + expect(hasReplacements).toBe(true); + expect(transformedCode).toContain('protobuf_encode_RemoteMsg'); + expect(transformedCode).toContain('protobuf_decode_RemoteMsg'); + }); + + it('round-trips encode/decode of a bare-specifier-imported message', () => { + // End-to-end: parse → analyze → generate codec → encode → + // decode → assert payload survived. The same shape as + // cross-file.test.ts's "Scenario A round-trip", but with the + // type sourced from a separate package. + const code = readFileSync(consumerPath, 'utf-8'); + const cache = new Map(); + const imported = resolveImports(code, consumerPath, cache); + const registry = analyzeSource(code, consumerPath, imported); + const gen = generateCode(registry); + + const result = execAndGet<{ id: number; name: string }>( + gen + '\n' + + 'const enc = protobuf_encode_RemoteMsg({ id: 42, name: \'alice\' });\n' + + 'globalThis.__r = protobuf_decode_RemoteMsg(enc);\n', + '__r', + ); + expect(result.id).toBe(42); + expect(result.name).toBe('alice'); + }); + + it('returns null for an unresolvable bare specifier (does not throw)', () => { + // Pre-existing relative-path behavior: missing files return + // null, never throw. The bare-specifier branch must match that + // — otherwise a typo in a package name turns into a build crash + // rather than a silent "no codec emitted" warning further down + // the pipeline. + const orphanPath = resolve(tmpRoot, 'orphan.ts'); + writeFileSync( + orphanPath, + 'import type { Whatever } from \'@does-not-exist/nope\';\n' + + 'export type _ = Whatever;\n', + ); + const code = readFileSync(orphanPath, 'utf-8'); + const cache = new Map(); + // resolveImports swallows resolution failures by design — it + // simply produces an empty `concrete` for the unresolvable + // import. The assertion is that we got here without an exception. + const imported = resolveImports(code, orphanPath, cache); + expect(imported.concrete).toEqual([]); + }); +});