Skip to content

Commit 7777008

Browse files
committed
claude fixes
1 parent d1f0787 commit 7777008

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

packages/codemod/src/migrations/v1-to-v2/mappings/importMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface ImportMapping {
55
/** Route specific symbols to a different target package than `target`. */
66
symbolTargetOverrides?: Record<string, string>;
77
removalMessage?: string;
8+
/** No entries currently set this; scaffolding for when a v1 symbol has no v2 equivalent yet. */
89
isV2Gap?: boolean;
910
}
1011

packages/codemod/src/runner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,24 @@ export function run(migration: Migration, options: RunnerOptions): RunnerResult
7373
const fileDiagnostics: Diagnostic[] = [];
7474
const originalText = sourceFile.getFullText();
7575

76+
const fileUsedPackages = new Set<string>();
7677
try {
7778
for (const transform of enabledTransforms) {
7879
const result = transform.apply(sourceFile, context);
7980
fileChanges += result.changesCount;
8081
fileDiagnostics.push(...result.diagnostics);
8182
if (result.usedPackages) {
8283
for (const pkg of result.usedPackages) {
83-
allUsedPackages.add(pkg);
84+
fileUsedPackages.add(pkg);
8485
}
8586
}
8687
}
88+
for (const pkg of fileUsedPackages) {
89+
allUsedPackages.add(pkg);
90+
}
8791
} catch (error_) {
8892
const filePath = sourceFile.getFilePath();
93+
fileDiagnostics.length = 0;
8994
fileDiagnostics.push(error(filePath, 1, `Transform failed: ${error_ instanceof Error ? error_.message : String(error_)}`));
9095
sourceFile.replaceWithText(originalText);
9196
fileChanges = 0;

packages/codemod/test/integration.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,63 @@ describe('integration', () => {
212212
expect(result.filesChanged).toBeGreaterThanOrEqual(1);
213213
});
214214

215+
it('rollback on transform error does not leak packages or diagnostics', () => {
216+
const dir = createTempDir();
217+
writePkgJson(dir, {
218+
dependencies: { '@modelcontextprotocol/sdk': '^1.0.0' }
219+
});
220+
221+
const input = [
222+
`import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';`,
223+
`const server = new McpServer({ name: 'test', version: '1.0' });`,
224+
``
225+
].join('\n');
226+
writeFileSync(path.join(dir, 'broken.ts'), input);
227+
228+
const leakyTransform: Transform = {
229+
name: 'leaky',
230+
id: 'imports',
231+
apply(sourceFile) {
232+
return {
233+
changesCount: 1,
234+
diagnostics: [{ level: DiagnosticLevel.Warning, file: sourceFile.getFilePath(), line: 1, message: 'should not survive rollback' }],
235+
usedPackages: new Set(['@modelcontextprotocol/phantom-pkg'])
236+
};
237+
}
238+
};
239+
240+
const failingTransform: Transform = {
241+
name: 'failing',
242+
id: 'failing',
243+
apply() {
244+
throw new Error('boom');
245+
}
246+
};
247+
248+
const testMigration: Migration = {
249+
name: 'test-rollback',
250+
description: 'Tests that rollback cleans up all side effects',
251+
transforms: [leakyTransform, failingTransform]
252+
};
253+
254+
const result = run(testMigration, { targetDir: dir });
255+
256+
// File should be rolled back
257+
const output = readFileSync(path.join(dir, 'broken.ts'), 'utf8');
258+
expect(output).toBe(input);
259+
260+
// Only the error diagnostic should survive — not the warning from the reverted transform
261+
const warnings = result.diagnostics.filter(d => d.level === DiagnosticLevel.Warning);
262+
expect(warnings).toHaveLength(0);
263+
const errors = result.diagnostics.filter(d => d.level === DiagnosticLevel.Error);
264+
expect(errors).toHaveLength(1);
265+
expect(errors[0].message).toContain('boom');
266+
267+
// Phantom package from the reverted transform should not leak into package.json
268+
expect(result.packageJsonChanges).toBeDefined();
269+
expect(result.packageJsonChanges!.added).not.toContain('@modelcontextprotocol/phantom-pkg');
270+
});
271+
215272
it('respects transform filter option', () => {
216273
const dir = createTempDir();
217274
const input = [

0 commit comments

Comments
 (0)