refactory_fix_imports
Fix broken require() paths in project files after module extraction. Scans consumers using pure path resolution; optionally preview changes with dry-run mode.
Instructions
Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| moduleDir | Yes | Directory containing extracted modules | |
| projectDir | No | Project root to scan for consumers | |
| dryRun | No | Report changes without writing (default: false) |
Implementation Reference
- src/tools/fix-imports.js:111-172 (handler)Main handler for fixImports: scans broken require() paths in the module directory and in consumer files under projectDir, resolves them by finding the target file by basename, and rewrites require() paths. Supports dryRun mode.
function fixImports({ moduleDir, projectDir, dryRun = false }) { const fixed = []; const errors = []; const modDirAbs = path.resolve(moduleDir); const projDirAbs = path.resolve(projectDir); // Phase 1: fix broken requires inside moduleDir const broken = scanBrokenRequires({ moduleDir: modDirAbs }); for (const b of broken) { const target = findTarget(modDirAbs, b.require); if (!target) { errors.push(`No target found for "${b.require}" in ${b.file}:${b.line}`); continue; } const newPath = relativeRequirePath(b.file, target); if (newPath === b.require) continue; if (!dryRun) { let src = fs.readFileSync(b.file, "utf8"); // Replace this specific require string (escape for regex) const escaped = b.require.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); src = src.replace( new RegExp(`(require\\s*\\(\\s*['"])${escaped}(['"]\\s*\\))`, "g"), `$1${newPath}$2` ); fs.writeFileSync(b.file, src, "utf8"); } fixed.push({ file: b.file, old: b.require, new: newPath }); } // Phase 2: scan project consumers outside moduleDir const projFiles = collectFiles(projDirAbs).filter( (f) => !f.startsWith(modDirAbs + path.sep) ); for (const file of projFiles) { const src = fs.readFileSync(file, "utf8"); let m; REQUIRE_RE.lastIndex = 0; while ((m = REQUIRE_RE.exec(src)) !== null) { const reqPath = m[2]; if (!reqPath.startsWith(".")) continue; const resolved = resolveRequire(file, reqPath); if (resolved) continue; // already works // Try to find it in moduleDir const target = findTarget(modDirAbs, reqPath); if (!target) continue; // not a module-related require const newPath = relativeRequirePath(file, target); if (newPath === reqPath) continue; if (!dryRun) { let content = fs.readFileSync(file, "utf8"); const escaped = reqPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); content = content.replace( new RegExp(`(require\\s*\\(\\s*['"])${escaped}(['"]\\s*\\))`, "g"), `$1${newPath}$2` ); fs.writeFileSync(file, content, "utf8"); } fixed.push({ file, old: reqPath, new: newPath }); } } return { fixed, errors }; } - src/server.js:153-165 (schema)Input schema for refactory_fix_imports tool: accepts moduleDir (required), projectDir, and dryRun (boolean, default false).
{ name: "refactory_fix_imports", description: "Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.", inputSchema: { type: "object", properties: { moduleDir: { type: "string", description: "Directory containing extracted modules" }, projectDir: { type: "string", description: "Project root to scan for consumers" }, dryRun: { type: "boolean", description: "Report changes without writing (default: false)" }, }, required: ["moduleDir"], }, }, - src/server.js:153-165 (registration)Tool name 'refactory_fix_imports' is defined in the TOOLS array with description and inputSchema.
{ name: "refactory_fix_imports", description: "Mechanically fix broken require() paths after module extraction. No LLM needed — pure path resolution.", inputSchema: { type: "object", properties: { moduleDir: { type: "string", description: "Directory containing extracted modules" }, projectDir: { type: "string", description: "Project root to scan for consumers" }, dryRun: { type: "boolean", description: "Report changes without writing (default: false)" }, }, required: ["moduleDir"], }, }, - src/server.js:211-211 (registration)Case handler in the CallToolRequestSchema switch statement that dispatches 'refactory_fix_imports' to the fixImports() function.
case "refactory_fix_imports": result = fixImports(args); break; - src/server.js:33-35 (registration)Import of fixImports from src/tools/fix-imports.js into the server.
const { fixImports, scanBrokenRequires, generateReexport } = require("./tools/fix-imports"); const { decompose } = require("./tools/decompose");