@devries-cameron20 / composio-1 / commits / 446c6f65b
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): unify virtual TypeScript compiler host resolution (#3182)

## Summary This PR resolves the TODO in `ts/packages/cli/src/generation/typescript/transpile.ts` by extracting the shared virtual TypeScript compiler-host logic used by both production transpilation and test validation. While doing that refactor, it also fixes a real virtual module-resolution issue: TypeScript may resolve in-memory files using `./file.ts`, `file.ts`, or absolute paths, and the previous setup did not handle those lookups consistently. ## Changes - added `ts/packages/cli/src/generation/typescript/virtual-compiler-host.ts` - extracted shared helpers for virtual file-map creation, compiler-host patching, and diagnostic formatting - updated `ts/packages/cli/src/generation/typescript/transpile.ts` to use the shared helper - updated `ts/packages/cli/test/__utils__/typescript-compiler.ts` to use the same shared helper - updated affected tests to validate complete generated module sets instead of incomplete single-file inputs - added a patch changeset for `@composio/cli` ## Type of change - [x] Bug fix - [x] Refactor/Chore ## How Has This Been Tested? Ran focused checks on the touched CLI TypeScript generation/compiler-host paths: ```bash pnpm --dir ts/packages/cli exec prettier --check src/generation/typescript/transpile.ts src/generation/typescript/virtual-compiler-host.ts test/__utils__/typescript-compiler.ts test/src/generation/typescript/generate.test.ts test/src/commands/ts/ts.generate.cmd.test.ts pnpm --dir ts/packages/cli exec vitest run test/__utils__/typescript-compiler.ts pnpm --dir ts/packages/cli exec vitest run test/src/generation/typescript/generate.test.ts --------- Co-authored-by: Alberto Schiabel <jkomyno@users.noreply.github.com> Co-authored-by: jkomyno <alberto@composio.dev>

Kshitiz committed Jun 24, 2026 at 19:58 UTC 446c6f65bf417cfae1dc37a023013d54a51f056d
7 files changed +242 -74
.changeset/virtual-compiler-host-resolution.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/cli': patch
3 +---
4 +
5 +Fix virtual TypeScript file resolution used by CLI type generation so in-memory imports resolve consistently during transpilation and validation.
mise.lock
+8 -8
@@ -89,23 +89,23 @@ version = "3.12.13"
89 backend = "core:python"
90
91 [tools.python."platforms.linux-arm64"]
92 -checksum = "sha256:eb85c6903552b1cb40b183a119681a9326fa97e31cf0e4b8c78bf26735ff1552"
93 -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"
92 +checksum = "sha256:b85154b9c7ca9de3f85f2c9f032d503151db16ef198de86b885fc61890c075ed"
93 +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"
94 provenance = "github-attestations"
95
96 [tools.python."platforms.linux-x64"]
97 -checksum = "sha256:6682afef6b510037a0ad84e61150e5121af2e2785c9ca27b047e029270a840fe"
98 -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"
97 +checksum = "sha256:10a452caac7041357805f0c19a60576df53f1ab06d1abfc9200f1f0157cb3bd1"
98 +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"
99 provenance = "github-attestations"
100
101 [tools.python."platforms.macos-arm64"]
102 -checksum = "sha256:f0a7fa7decc75df2b1a789329a44f657c4a15c0a683f197ce46a5cb621bc6ef4"
103 -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"
102 +checksum = "sha256:41df7d3ae4757e84b97874f76d634268456aaa271740d33f968d826374998fb7"
103 +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"
104 provenance = "github-attestations"
105
106 [tools.python."platforms.macos-x64"]
107 -checksum = "sha256:c56c2dfe3fb5569430f4eabcff1fc1334c66db708bf17c103eef3dd237f3e3ab"
108 -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"
107 +checksum = "sha256:a6bbea996c5f14eb55ab275889d2df45408deec504b4a7219d7b59c045b2555e"
108 +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"
109 provenance = "github-attestations"
110
111 [[tools.uv]]
ts/packages/cli/src/generation/typescript/transpile.ts
+11 -36
@@ -1,5 +1,10 @@
1 import ts from 'typescript';
2 import { Effect, Data } from 'effect';
3 +import {
4 + buildVirtualFileMap,
5 + patchCompilerHostWithVirtualFiles,
6 + formatDiagnostic,
7 +} from './virtual-compiler-host';
8
9 export class TypeScriptTranspileError extends Data.TaggedError('error/TypeScriptTranspileError')<{
10 readonly message: string;
@@ -13,10 +18,13 @@ type TranspileTypeScriptFilesParams = {
18
19 /**
20 * Compiles TypeScript files to JavaScript using the TypeScript compiler.
16 - * TODO: unify with `test/__utils__/typescript-compiler.ts`.
21 */
22 export function transpileTypeScriptSources({ sources, outputDir }: TranspileTypeScriptFilesParams) {
23 return Effect.gen(function* () {
24 + if (sources.length === 0) {
25 + return yield* Effect.void;
26 + }
27 +
28 const compilerOptions = {
29 target: ts.ScriptTarget.ES2022,
30 module: ts.ModuleKind.NodeNext,
@@ -30,31 +38,11 @@ export function transpileTypeScriptSources({ sources, outputDir }: TranspileType
38 noEmitOnError: true,
39 } satisfies ts.CompilerOptions;
40
33 - const virtualFileMap = new Map(
34 - sources.map(
35 - ([filename, code]) =>
36 - [
37 - filename,
38 - ts.createSourceFile(filename, code, compilerOptions.target, true, ts.ScriptKind.TS),
39 - ] as const
40 - )
41 - );
41 + const virtualFileMap = buildVirtualFileMap(sources, compilerOptions.target);
42 const virtualFileNames = Array.from(virtualFileMap.keys());
43 - const tsFiles = sources.map(([filePath, _]) => filePath);
44 -
45 - if (tsFiles.length === 0) {
46 - return yield* Effect.void;
47 - }
43
44 const tsHost = ts.createCompilerHost(compilerOptions);
50 - const ogGetSourceFile = tsHost.getSourceFile;
51 - tsHost.getSourceFile = (filename, languageVersion, onError, shouldCreateNewSourceFile) => {
52 - if (virtualFileMap.has(filename)) {
53 - return virtualFileMap.get(filename);
54 - }
55 -
56 - return ogGetSourceFile(filename, languageVersion, onError, shouldCreateNewSourceFile);
57 - };
45 + patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'delegate');
46
47 const program = ts.createProgram(virtualFileNames, compilerOptions, tsHost);
48 const emitResult = program.emit();
@@ -63,19 +51,6 @@ export function transpileTypeScriptSources({ sources, outputDir }: TranspileType
51 const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
52
53 if (diagnostics.length > 0) {
66 - const formatDiagnostic = (diagnostic: ts.Diagnostic): string => {
67 - if (diagnostic.file && diagnostic.start !== undefined) {
68 - const { line, character } = ts.getLineAndCharacterOfPosition(
69 - diagnostic.file,
70 - diagnostic.start
71 - );
72 - const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
73 - return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
74 - } else {
75 - return ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
76 - }
77 - };
78 -
54 const errorMessages = diagnostics.map(d => formatDiagnostic(d)).join('\n');
55 return yield* Effect.fail(
56 new TypeScriptTranspileError({
ts/packages/cli/src/generation/typescript/virtual-compiler-host.ts new
+119
@@ -0,0 +1,119 @@
1 +import path from 'node:path';
2 +import ts from 'typescript';
3 +
4 +/**
5 + * Builds a virtual file map from source tuples for in-memory TypeScript compilation.
6 + */
7 +export function buildVirtualFileMap(
8 + sources: readonly (readonly [filePath: string, content: string])[],
9 + target: ts.ScriptTarget
10 +): Map<string, ts.SourceFile> {
11 + return new Map(
12 + sources.map(
13 + ([filename, code]) =>
14 + [filename, ts.createSourceFile(filename, code, target, true, ts.ScriptKind.TS)] as const
15 + )
16 + );
17 +}
18 +
19 +/**
20 + * Patches a TypeScript compiler host to resolve virtual files before
21 + * falling back to the default file system resolution.
22 + *
23 + * Overrides both `getSourceFile` and `fileExists` so that module resolution
24 + * can discover virtual files that don't exist on the real file system.
25 + *
26 + * @param fallback - `'throw'` raises when a file cannot be resolved by the
27 + * virtual file map, which keeps validation hosts isolated.
28 + * `'delegate'` falls back to the real file system without
29 + * throwing. Defaults to `'delegate'`.
30 + */
31 +export function patchCompilerHostWithVirtualFiles(
32 + tsHost: ts.CompilerHost,
33 + virtualFileMap: Map<string, ts.SourceFile>,
34 + fallback: 'throw' | 'delegate' = 'delegate'
35 +): void {
36 + const currentDirectory = normalizeSlashes(tsHost.getCurrentDirectory());
37 + const canonicalizePath = tsHost.getCanonicalFileName.bind(tsHost);
38 + const ogGetSourceFile = tsHost.getSourceFile;
39 + const ogFileExists = tsHost.fileExists;
40 +
41 + const virtualFileLookupMap = new Map<string, ts.SourceFile>();
42 + for (const [filename, sourceFile] of virtualFileMap) {
43 + for (const lookupKey of getVirtualFileLookupKeys(filename, currentDirectory)) {
44 + virtualFileLookupMap.set(canonicalizePath(lookupKey), sourceFile);
45 + }
46 + }
47 +
48 + const resolveVirtualFile = (filename: string): ts.SourceFile | undefined =>
49 + virtualFileLookupMap.get(canonicalizePath(normalizeSlashes(path.normalize(filename))));
50 +
51 + tsHost.fileExists = filename => {
52 + const hasVirtualFile = resolveVirtualFile(filename) !== undefined;
53 + if (fallback === 'throw') {
54 + return hasVirtualFile;
55 + }
56 +
57 + return hasVirtualFile || ogFileExists(filename);
58 + };
59 +
60 + tsHost.getSourceFile = (filename, languageVersion, onError, shouldCreateNewSourceFile) => {
61 + const virtualFile = resolveVirtualFile(filename);
62 + if (virtualFile) {
63 + return virtualFile;
64 + }
65 +
66 + if (fallback === 'throw') {
67 + throw new Error(`Unexpected filename ${filename}`);
68 + }
69 +
70 + const sourceFile = ogGetSourceFile(
71 + filename,
72 + languageVersion,
73 + onError,
74 + shouldCreateNewSourceFile
75 + );
76 + if (sourceFile) {
77 + return sourceFile;
78 + }
79 +
80 + return sourceFile;
81 + };
82 +}
83 +
84 +function getVirtualFileLookupKeys(filename: string, currentDirectory: string): string[] {
85 + const normalizedFilename = normalizeSlashes(path.normalize(filename));
86 +
87 + if (path.isAbsolute(normalizedFilename)) {
88 + const relativeFilename = normalizeSlashes(path.relative(currentDirectory, normalizedFilename));
89 + return uniqueLookupKeys(normalizedFilename, relativeFilename, `./${relativeFilename}`);
90 + }
91 +
92 + const relativeFilename = normalizedFilename.replace(/^\.\//, '');
93 + return uniqueLookupKeys(
94 + normalizedFilename,
95 + relativeFilename,
96 + `./${relativeFilename}`,
97 + normalizeSlashes(path.resolve(currentDirectory, relativeFilename))
98 + );
99 +}
100 +
101 +function normalizeSlashes(filename: string): string {
102 + return filename.replaceAll('\\', '/');
103 +}
104 +
105 +function uniqueLookupKeys(...keys: string[]): string[] {
106 + return Array.from(new Set(keys.filter(key => key.length > 0)));
107 +}
108 +
109 +/**
110 + * Formats a TypeScript diagnostic into a human-readable string.
111 + */
112 +export function formatDiagnostic(diagnostic: ts.Diagnostic): string {
113 + if (diagnostic.file && diagnostic.start !== undefined) {
114 + const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
115 + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
116 + return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
117 + }
118 + return ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
119 +}
ts/packages/cli/test/__utils__/typescript-compiler.ts
+63 -21
@@ -1,7 +1,12 @@
1 -import { describe, it, expect } from 'vitest';
1 +import { describe, it, expect, vi } from 'vitest';
2 import { Effect, Stream, String } from 'effect';
3 import { Command } from '@effect/platform';
4 import ts from 'typescript';
5 +import {
6 + buildVirtualFileMap,
7 + formatDiagnostic,
8 + patchCompilerHostWithVirtualFiles,
9 +} from 'src/generation/typescript/virtual-compiler-host';
10
11 interface AssertTypeScriptIsValidInput {
12 files: {
@@ -9,6 +14,16 @@ interface AssertTypeScriptIsValidInput {
14 };
15 }
16
17 +const VALIDATION_TYPE_STUBS = /* typescript */ `
18 + type Record<K extends keyof any, T> = {
19 + [P in K]: T;
20 + };
21 +
22 + declare module "@composio/core" {
23 + export type TriggerEvent<TPayload> = { payload: TPayload };
24 + }
25 +`;
26 +
27 /**
28 * Asserts that the provided TypeScript code is syntactically and semantically valid.
29 */
@@ -28,29 +43,17 @@ export function assertTypeScriptIsValid({ files }: AssertTypeScriptIsValidInput)
43 allowImportingTsExtensions: true,
44 } satisfies ts.CompilerOptions;
45
31 - const virtualFileMap = new Map(
32 - Object.entries(files).map(
33 - ([filename, code]) =>
34 - [
35 - filename,
36 - ts.createSourceFile(filename, code, compilerOptions.target, true, ts.ScriptKind.TS),
37 - ] as const
38 - )
46 + const virtualFileMap = buildVirtualFileMap(
47 + Object.entries({
48 + './__validation-stubs.d.ts': VALIDATION_TYPE_STUBS,
49 + ...files,
50 + }),
51 + compilerOptions.target
52 );
53 const virtualFileNames = Array.from(virtualFileMap.keys());
54
55 const tsHost = ts.createCompilerHost(compilerOptions);
43 - tsHost.getSourceFile = (filename, _languageVersion) => {
44 - if (virtualFileMap.has(filename)) {
45 - return virtualFileMap.get(filename);
46 - }
47 -
48 - if (virtualFileMap.has(`./${filename}`)) {
49 - return virtualFileMap.get(filename);
50 - }
51 -
52 - throw new Error(`Unexpected filename ${filename}`);
53 - };
56 + patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');
57
58 const program = ts.createProgram(virtualFileNames, compilerOptions, tsHost);
59
@@ -58,7 +61,7 @@ export function assertTypeScriptIsValid({ files }: AssertTypeScriptIsValidInput)
61 const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
62
63 // Assert that there are no TypeScript errors
61 - expect(diagnostics).toEqual([]);
64 + expect(diagnostics.map(formatDiagnostic)).toEqual([]);
65 }
66
67 type AssertTranspiledTypeScriptIsValidInput = {
@@ -121,6 +124,16 @@ if (import.meta.vitest) {
124 });
125 });
126
127 + it('[Given] generated code imports Composio core types [Then] no errors are found', () => {
128 + const code = /* typescript */ `
129 + import { type TriggerEvent } from '@composio/core';
130 +
131 + export type Event = TriggerEvent<{ id: string }>;
132 + `;
133 +
134 + assertTypeScriptIsValid({ files: { 'index.ts': code } });
135 + });
136 +
137 it('[Given] invalid TypeScript code [Then] errors are found', () => {
138 const code = /* typescript */ `
139 export const id<T> = (x: T) => T: x;
@@ -129,5 +142,34 @@ if (import.meta.vitest) {
142 assertTypeScriptIsValid({ files: { 'index.ts': code } });
143 }).toThrowError();
144 });
145 +
146 + it('[Given] throw fallback [Then] the virtual host does not delegate misses to disk', () => {
147 + const compilerOptions = {
148 + target: ts.ScriptTarget.ES2022,
149 + module: ts.ModuleKind.ES2022,
150 + } satisfies ts.CompilerOptions;
151 + const virtualFileMap = buildVirtualFileMap(
152 + [['index.ts', 'export const ok = true;']],
153 + compilerOptions.target
154 + );
155 + const tsHost = ts.createCompilerHost(compilerOptions);
156 + const getSourceFile = vi.fn(() => {
157 + throw new Error('delegated getSourceFile');
158 + });
159 + const fileExists = vi.fn(() => {
160 + throw new Error('delegated fileExists');
161 + });
162 +
163 + tsHost.getSourceFile = getSourceFile;
164 + tsHost.fileExists = fileExists;
165 + patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');
166 +
167 + expect(tsHost.fileExists('node_modules/@composio/core/index.d.ts')).toBe(false);
168 + expect(() =>
169 + tsHost.getSourceFile('node_modules/@composio/core/index.d.ts', compilerOptions.target)
170 + ).toThrowError('Unexpected filename node_modules/@composio/core/index.d.ts');
171 + expect(fileExists).not.toHaveBeenCalled();
172 + expect(getSourceFile).not.toHaveBeenCalled();
173 + });
174 });
175 }
ts/packages/cli/test/src/commands/ts/ts.generate.cmd.test.ts
+33 -8
@@ -84,6 +84,31 @@ describe('CLI: composio generate ts', () => {
84 yield* fs.writeFileString(path.join(nodeModulesDir, 'index.js'), 'export {};');
85 });
86
87 + const readGeneratedTypeScriptFiles = ({
88 + fs,
89 + outputDir,
90 + }: {
91 + fs: FileSystem.FileSystem;
92 + outputDir: string;
93 + }) =>
94 + Effect.gen(function* () {
95 + const files = yield* fs.readDirectory(outputDir);
96 + const tsFiles = files
97 + .map(file => path.basename(file))
98 + .filter(file => file.endsWith('.ts') && !file.endsWith('.d.ts'));
99 +
100 + const sources = yield* Effect.all(
101 + tsFiles.map(file =>
102 + fs
103 + .readFileString(path.join(outputDir, file))
104 + .pipe(Effect.map(content => [`./${file}`, content] as const))
105 + ),
106 + { concurrency: 'unbounded' }
107 + );
108 +
109 + return Object.fromEntries(sources);
110 + });
111 +
112 describe('[Given] valid fetched app data', () => {
113 layer(
114 TestLive({
@@ -779,11 +804,8 @@ describe('CLI: composio generate ts', () => {
804 expect(jsFiles.length).toBeGreaterThan(0);
805 expect(dtsFiles.length).toBeGreaterThan(0);
806
782 - // Verify the files are valid TypeScript
783 - for (const file of tsFiles) {
784 - const filePath = path.join(outputDir, file);
785 - assertTypeScriptIsValid({ files: { './index.ts': filePath } });
786 - }
807 + const filesByPath = yield* readGeneratedTypeScriptFiles({ fs, outputDir });
808 + assertTypeScriptIsValid({ files: filesByPath });
809 })
810 );
811
@@ -893,7 +915,8 @@ describe('CLI: composio generate ts', () => {
915 "
916 `);
917
896 - assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });
918 + const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });
919 + assertTypeScriptIsValid({ files: generatedFiles });
920 })
921 );
922
@@ -922,7 +945,8 @@ describe('CLI: composio generate ts', () => {
945 expect(indexSourceCode).toContain('GMAIL');
946 expect(indexSourceCode).not.toContain('SLACK');
947
925 - assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });
948 + const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });
949 + assertTypeScriptIsValid({ files: generatedFiles });
950 })
951 );
952
@@ -960,7 +984,8 @@ describe('CLI: composio generate ts', () => {
984 expect(indexSourceCode).toContain('GMAIL');
985 expect(indexSourceCode).toContain('SLACK');
986
963 - assertTypeScriptIsValid({ files: { './index.ts': indexSourceCode } });
987 + const generatedFiles = yield* readGeneratedTypeScriptFiles({ fs, outputDir });
988 + assertTypeScriptIsValid({ files: generatedFiles });
989 })
990 );
991
ts/packages/cli/test/src/generation/typescript/generate.test.ts
+3 -1
@@ -244,7 +244,9 @@ describe('generateTypeScriptSources', () => {
244
245 "
246 `);
247 - assertTypeScriptIsValid({ files: { './index.ts': sources[1][1] } });
247 + assertTypeScriptIsValid({
248 + files: { './index.ts': sources[1][1], './slack.ts': sources[0][1] },
249 + });
250 })
251 );
252 });