Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(cli): unify virtual TypeScript compiler host resolution (#3182)
Kshitiz446c6f65bf41
diff --git a/.changeset/virtual-compiler-host-resolution.md b/.changeset/virtual-compiler-host-resolution.mdnew file mode 100644index 000000000..b03d90e7d--- /dev/null+++ b/.changeset/virtual-compiler-host-resolution.md@@ -0,0 +1,5 @@+---+'@composio/cli': patch+---++Fix virtual TypeScript file resolution used by CLI type generation so in-memory imports resolve consistently during transpilation and validation.diff --git a/mise.lock b/mise.lockindex 8edf0fd9c..b879527ea 100644--- a/mise.lock+++ b/mise.lock@@ -89,23 +89,23 @@ version = "3.12.13"backend = "core:python"[tools.python."platforms.linux-arm64"]-checksum = "sha256:eb85c6903552b1cb40b183a119681a9326fa97e31cf0e4b8c78bf26735ff1552"-url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.12.13+20260610-aarch64-unknown-linux-gnu-install_only_stripped.tar.gz"+checksum = "sha256:b85154b9c7ca9de3f85f2c9f032d503151db16ef198de86b885fc61890c075ed"+url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260623/cpython-3.12.13+20260623-aarch64-unknown-linux-gnu-install_only_stripped.tar.gz"provenance = "github-attestations"[tools.python."platforms.linux-x64"]-checksum = "sha256:6682afef6b510037a0ad84e61150e5121af2e2785c9ca27b047e029270a840fe"-url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.12.13+20260610-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"+checksum = "sha256:10a452caac7041357805f0c19a60576df53f1ab06d1abfc9200f1f0157cb3bd1"+url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260623/cpython-3.12.13+20260623-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz"provenance = "github-attestations"[tools.python."platforms.macos-arm64"]-checksum = "sha256:f0a7fa7decc75df2b1a789329a44f657c4a15c0a683f197ce46a5cb621bc6ef4"-url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.12.13+20260610-aarch64-apple-darwin-install_only_stripped.tar.gz"+checksum = "sha256:41df7d3ae4757e84b97874f76d634268456aaa271740d33f968d826374998fb7"+url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260623/cpython-3.12.13+20260623-aarch64-apple-darwin-install_only_stripped.tar.gz"provenance = "github-attestations"[tools.python."platforms.macos-x64"]-checksum = "sha256:c56c2dfe3fb5569430f4eabcff1fc1334c66db708bf17c103eef3dd237f3e3ab"-url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260610/cpython-3.12.13+20260610-x86_64-apple-darwin-install_only_stripped.tar.gz"+checksum = "sha256:a6bbea996c5f14eb55ab275889d2df45408deec504b4a7219d7b59c045b2555e"+url = "https://github.com/astral-sh/python-build-standalone/releases/download/20260623/cpython-3.12.13+20260623-x86_64-apple-darwin-install_only_stripped.tar.gz"provenance = "github-attestations"[[tools.uv]]diff --git a/ts/packages/cli/src/generation/typescript/transpile.ts b/ts/packages/cli/src/generation/typescript/transpile.tsindex 4ef1ae94f..290785e68 100644--- a/ts/packages/cli/src/generation/typescript/transpile.ts+++ b/ts/packages/cli/src/generation/typescript/transpile.ts@@ -1,5 +1,10 @@import ts from 'typescript';import { Effect, Data } from 'effect';+import {+ buildVirtualFileMap,+ patchCompilerHostWithVirtualFiles,+ formatDiagnostic,+} from './virtual-compiler-host';export class TypeScriptTranspileError extends Data.TaggedError('error/TypeScriptTranspileError')<{readonly message: string;@@ -13,10 +18,13 @@ type TranspileTypeScriptFilesParams = {/*** Compiles TypeScript files to JavaScript using the TypeScript compiler.- * TODO: unify with `test/__utils__/typescript-compiler.ts`.*/export function transpileTypeScriptSources({ sources, outputDir }: TranspileTypeScriptFilesParams) {return Effect.gen(function* () {+ if (sources.length === 0) {+ return yield* Effect.void;+ }+const compilerOptions = {target: ts.ScriptTarget.ES2022,module: ts.ModuleKind.NodeNext,@@ -30,31 +38,11 @@ export function transpileTypeScriptSources({ sources, outputDir }: TranspileTypenoEmitOnError: true,} satisfies ts.CompilerOptions;- const virtualFileMap = new Map(- sources.map(- ([filename, code]) =>- [- filename,- ts.createSourceFile(filename, code, compilerOptions.target, true, ts.ScriptKind.TS),- ] as const- )- );+ const virtualFileMap = buildVirtualFileMap(sources, compilerOptions.target);const virtualFileNames = Array.from(virtualFileMap.keys());- const tsFiles = sources.map(([filePath, _]) => filePath);-- if (tsFiles.length === 0) {- return yield* Effect.void;- }const tsHost = ts.createCompilerHost(compilerOptions);- const ogGetSourceFile = tsHost.getSourceFile;- tsHost.getSourceFile = (filename, languageVersion, onError, shouldCreateNewSourceFile) => {- if (virtualFileMap.has(filename)) {- return virtualFileMap.get(filename);- }-- return ogGetSourceFile(filename, languageVersion, onError, shouldCreateNewSourceFile);- };+ patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'delegate');const program = ts.createProgram(virtualFileNames, compilerOptions, tsHost);const emitResult = program.emit();@@ -63,19 +51,6 @@ export function transpileTypeScriptSources({ sources, outputDir }: TranspileTypeconst diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);if (diagnostics.length > 0) {- const formatDiagnostic = (diagnostic: ts.Diagnostic): string => {- if (diagnostic.file && diagnostic.start !== undefined) {- const { line, character } = ts.getLineAndCharacterOfPosition(- diagnostic.file,- diagnostic.start- );- const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');- return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;- } else {- return ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');- }- };-const errorMessages = diagnostics.map(d => formatDiagnostic(d)).join('\n');return yield* Effect.fail(new TypeScriptTranspileError({diff --git a/ts/packages/cli/src/generation/typescript/virtual-compiler-host.ts b/ts/packages/cli/src/generation/typescript/virtual-compiler-host.tsnew file mode 100644index 000000000..5e3b813d3--- /dev/null+++ b/ts/packages/cli/src/generation/typescript/virtual-compiler-host.ts@@ -0,0 +1,119 @@+import path from 'node:path';+import ts from 'typescript';++/**+ * Builds a virtual file map from source tuples for in-memory TypeScript compilation.+ */+export function buildVirtualFileMap(+ sources: readonly (readonly [filePath: string, content: string])[],+ target: ts.ScriptTarget+): Map<string, ts.SourceFile> {+ return new Map(+ sources.map(+ ([filename, code]) =>+ [filename, ts.createSourceFile(filename, code, target, true, ts.ScriptKind.TS)] as const+ )+ );+}++/**+ * Patches a TypeScript compiler host to resolve virtual files before+ * falling back to the default file system resolution.+ *+ * Overrides both `getSourceFile` and `fileExists` so that module resolution+ * can discover virtual files that don't exist on the real file system.+ *+ * @param fallback - `'throw'` raises when a file cannot be resolved by the+ * virtual file map, which keeps validation hosts isolated.+ * `'delegate'` falls back to the real file system without+ * throwing. Defaults to `'delegate'`.+ */+export function patchCompilerHostWithVirtualFiles(+ tsHost: ts.CompilerHost,+ virtualFileMap: Map<string, ts.SourceFile>,+ fallback: 'throw' | 'delegate' = 'delegate'+): void {+ const currentDirectory = normalizeSlashes(tsHost.getCurrentDirectory());+ const canonicalizePath = tsHost.getCanonicalFileName.bind(tsHost);+ const ogGetSourceFile = tsHost.getSourceFile;+ const ogFileExists = tsHost.fileExists;++ const virtualFileLookupMap = new Map<string, ts.SourceFile>();+ for (const [filename, sourceFile] of virtualFileMap) {+ for (const lookupKey of getVirtualFileLookupKeys(filename, currentDirectory)) {+ virtualFileLookupMap.set(canonicalizePath(lookupKey), sourceFile);+ }+ }++ const resolveVirtualFile = (filename: string): ts.SourceFile | undefined =>+ virtualFileLookupMap.get(canonicalizePath(normalizeSlashes(path.normalize(filename))));++ tsHost.fileExists = filename => {+ const hasVirtualFile = resolveVirtualFile(filename) !== undefined;+ if (fallback === 'throw') {+ return hasVirtualFile;+ }++ return hasVirtualFile || ogFileExists(filename);+ };++ tsHost.getSourceFile = (filename, languageVersion, onError, shouldCreateNewSourceFile) => {+ const virtualFile = resolveVirtualFile(filename);+ if (virtualFile) {+ return virtualFile;+ }++ if (fallback === 'throw') {+ throw new Error(`Unexpected filename ${filename}`);+ }++ const sourceFile = ogGetSourceFile(+ filename,+ languageVersion,+ onError,+ shouldCreateNewSourceFile+ );+ if (sourceFile) {+ return sourceFile;+ }++ return sourceFile;+ };+}++function getVirtualFileLookupKeys(filename: string, currentDirectory: string): string[] {+ const normalizedFilename = normalizeSlashes(path.normalize(filename));++ if (path.isAbsolute(normalizedFilename)) {+ const relativeFilename = normalizeSlashes(path.relative(currentDirectory, normalizedFilename));+ return uniqueLookupKeys(normalizedFilename, relativeFilename, `./${relativeFilename}`);+ }++ const relativeFilename = normalizedFilename.replace(/^\.\//, '');+ return uniqueLookupKeys(+ normalizedFilename,+ relativeFilename,+ `./${relativeFilename}`,+ normalizeSlashes(path.resolve(currentDirectory, relativeFilename))+ );+}++function normalizeSlashes(filename: string): string {+ return filename.replaceAll('\\', '/');+}++function uniqueLookupKeys(...keys: string[]): string[] {+ return Array.from(new Set(keys.filter(key => key.length > 0)));+}++/**+ * Formats a TypeScript diagnostic into a human-readable string.+ */+export function formatDiagnostic(diagnostic: ts.Diagnostic): string {+ if (diagnostic.file && diagnostic.start !== undefined) {+ const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);+ const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');+ return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;+ }+ return ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');+}diff --git a/ts/packages/cli/test/__utils__/typescript-compiler.ts b/ts/packages/cli/test/__utils__/typescript-compiler.tsindex a8cb1cab5..08d4ffa26 100644--- a/ts/packages/cli/test/__utils__/typescript-compiler.ts+++ b/ts/packages/cli/test/__utils__/typescript-compiler.ts@@ -1,7 +1,12 @@-import { describe, it, expect } from 'vitest';+import { describe, it, expect, vi } from 'vitest';import { Effect, Stream, String } from 'effect';import { Command } from '@effect/platform';import ts from 'typescript';+import {+ buildVirtualFileMap,+ formatDiagnostic,+ patchCompilerHostWithVirtualFiles,+} from 'src/generation/typescript/virtual-compiler-host';interface AssertTypeScriptIsValidInput {files: {@@ -9,6 +14,16 @@ interface AssertTypeScriptIsValidInput {};}+const VALIDATION_TYPE_STUBS = /* typescript */ `+ type Record<K extends keyof any, T> = {+ [P in K]: T;+ };++ declare module "@composio/core" {+ export type TriggerEvent<TPayload> = { payload: TPayload };+ }+`;+/*** Asserts that the provided TypeScript code is syntactically and semantically valid.*/@@ -28,29 +43,17 @@ export function assertTypeScriptIsValid({ files }: AssertTypeScriptIsValidInput)allowImportingTsExtensions: true,} satisfies ts.CompilerOptions;- const virtualFileMap = new Map(- Object.entries(files).map(- ([filename, code]) =>- [- filename,- ts.createSourceFile(filename, code, compilerOptions.target, true, ts.ScriptKind.TS),- ] as const- )+ const virtualFileMap = buildVirtualFileMap(+ Object.entries({+ './__validation-stubs.d.ts': VALIDATION_TYPE_STUBS,+ ...files,+ }),+ compilerOptions.target);const virtualFileNames = Array.from(virtualFileMap.keys());const tsHost = ts.createCompilerHost(compilerOptions);- tsHost.getSourceFile = (filename, _languageVersion) => {- if (virtualFileMap.has(filename)) {- return virtualFileMap.get(filename);- }-- if (virtualFileMap.has(`./${filename}`)) {- return virtualFileMap.get(filename);- }-- throw new Error(`Unexpected filename ${filename}`);- };+ patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');const program = ts.createProgram(virtualFileNames, compilerOptions, tsHost);@@ -58,7 +61,7 @@ export function assertTypeScriptIsValid({ files }: AssertTypeScriptIsValidInput)const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];// Assert that there are no TypeScript errors- expect(diagnostics).toEqual([]);+ expect(diagnostics.map(formatDiagnostic)).toEqual([]);}type AssertTranspiledTypeScriptIsValidInput = {@@ -121,6 +124,16 @@ if (import.meta.vitest) {});});+ it('[Given] generated code imports Composio core types [Then] no errors are found', () => {+ const code = /* typescript */ `+ import { type TriggerEvent } from '@composio/core';++ export type Event = TriggerEvent<{ id: string }>;+ `;++ assertTypeScriptIsValid({ files: { 'index.ts': code } });+ });+it('[Given] invalid TypeScript code [Then] errors are found', () => {const code = /* typescript */ `export const id<T> = (x: T) => T: x;@@ -129,5 +142,34 @@ if (import.meta.vitest) {assertTypeScriptIsValid({ files: { 'index.ts': code } });}).toThrowError();});++ it('[Given] throw fallback [Then] the virtual host does not delegate misses to disk', () => {+ const compilerOptions = {+ target: ts.ScriptTarget.ES2022,+ module: ts.ModuleKind.ES2022,+ } satisfies ts.CompilerOptions;+ const virtualFileMap = buildVirtualFileMap(+ [['index.ts', 'export const ok = true;']],+ compilerOptions.target+ );+ const tsHost = ts.createCompilerHost(compilerOptions);+ const getSourceFile = vi.fn(() => {+ throw new Error('delegated getSourceFile');+ });+ const fileExists = vi.fn(() => {+ throw new Error('delegated fileExists');+ });++ tsHost.getSourceFile = getSourceFile;+ tsHost.fileExists = fileExists;+ patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');++ expect(tsHost.fileExists('node_modules/@composio/core/index.d.ts')).toBe(false);+ expect(() =>+ tsHost.getSourceFile('node_modules/@composio/core/index.d.ts', compilerOptions.target)+ ).toThrowError('Unexpected filename node_modules/@composio/core/index.d.ts');+ expect(fileExists).not.toHaveBeenCalled();+ expect(getSourceFile).not.toHaveBeenCalled();+ });});}diff --git a/ts/packages/cli/test/src/commands/ts/ts.generate.cmd.test.ts b/ts/packages/cli/test/src/commands/ts/ts.generate.cmd.test.tsindex 84902e1db..f9117e60f 100644--- a/ts/packages/cli/test/src/commands/ts/ts.generate.cmd.test.ts+++ b/ts/packages/cli/test/src/commands/ts/ts.generate.cmd.test.ts@@ -84,6 +84,31 @@ describe('CLI: composio generate ts', () => {yield* fs.writeFileString(path.join(nodeModulesDir, 'index.js'), 'export {};');});+ const readGeneratedTypeScriptFiles = ({+ fs,+ outputDir,+ }: {+ fs: FileSystem.FileSystem;+ outputDir: string;+ }) =>+ Effect.gen(function* () {+ const files = yield* fs.readDirectory(outputDir);+ const tsFiles = files+ .map(file => path.basename(file))+ .filter(file => file.endsWith('.ts') && !file.endsWith('.d.ts'));++ const sources = yield* Effect.all(+ tsFiles.map(file =>+ fs+ .readFileString(path.join(outputDir, file))+ .pipe(Effect.map(content => [`./${file}`, content] as const))+ ),+ { concurrency: 'unbounded' }+ );++ return Object.fromEntries(sources);+ });+describe('[Given] valid fetched app data', () => {layer(TestLive({@@ -779,11 +804,8 @@ describe('CLI: composio generate ts', () => {expect(jsFiles.length).toBeGreaterThan(0);expect(dtsFiles.length).toBeGreaterThan(0);- // Verify the files are valid TypeScript- for (const file of tsFiles) {- const filePath = path.join(outputDir, file);- assertTypeScriptIsValid({ files: { './index.ts': filePath } });- }+ const filesByPath = yield* readGeneratedTypeScriptFiles({ fs, outputDir });+ assertTypeScriptIsValid({ files: filesByPath });}));@@ -893,7 +915,8 @@ describe('CLI: composio generate ts', () => {"`);- assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });+ const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });+ assertTypeScriptIsValid({ files: generatedFiles });}));@@ -922,7 +945,8 @@ describe('CLI: composio generate ts', () => {expect(indexSourceCode).toContain('GMAIL');expect(indexSourceCode).not.toContain('SLACK');- assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });+ const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });+ assertTypeScriptIsValid({ files: generatedFiles });}));@@ -960,7 +984,8 @@ describe('CLI: composio generate ts', () => {expect(indexSourceCode).toContain('GMAIL');expect(indexSourceCode).toContain('SLACK');- assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });+ const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });+ assertTypeScriptIsValid({ files: generatedFiles });}));diff --git a/ts/packages/cli/test/src/generation/typescript/generate.test.ts b/ts/packages/cli/test/src/generation/typescript/generate.test.tsindex a559712f4..a96110313 100644--- a/ts/packages/cli/test/src/generation/typescript/generate.test.ts+++ b/ts/packages/cli/test/src/generation/typescript/generate.test.ts@@ -244,7 +244,9 @@ describe('generateTypeScriptSources', () => {"`);- assertTypeScriptIsValid({ files: { './index.ts': sources[1][1] } });+ assertTypeScriptIsValid({+ files: { './index.ts': sources[1][1], './slack.ts': sources[0][1] },+ });}));});