{"record":{"id":"40e327e1fe49444e","repo":"firecrawl/open-lovable","slug":"no-available-method-to-write-files-to-sandbox","errorCode":null,"errorMessage":"No available method to write files to sandbox","messagePattern":"No available method to write files to sandbox","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/morph-fast-apply.ts","lineNumber":165,"sourceCode":"    const escaped = content\n      .replace(/\\\\/g, '\\\\\\\\')\n      .replace(/\"\"\"/g, '\\\"\\\"\\\"');\n    await sandbox.runCode(`\nimport os\nos.makedirs(os.path.dirname(\"${fullPath}\"), exist_ok=True)\nwith open(\"${fullPath}\", 'w') as f:\n    f.write(\"\"\"${escaped}\"\"\")\nprint(\"WROTE:${fullPath}\")\n    `);\n  } else if (sandbox?.commands?.run) {\n    // Shell redirection (fallback)\n    // Note: beware of special chars; this is a last-resort path\n    const result = await sandbox.commands.run(`bash -lc 'mkdir -p \"$(dirname \"${fullPath}\")\" && cat > \"${fullPath}\" << \\EOF\\n${content}\\nEOF'`, { cwd: '/home/user/app', timeout: 60 });\n    if (result?.exitCode !== 0) {\n      throw new Error(`Failed to write file via shell: ${normalizedPath}`);\n    }\n  } else {\n    throw new Error('No available method to write files to sandbox');\n  }\n\n  // Update backend cache if available\n  if ((global as any).sandboxState?.fileCache) {\n    (global as any).sandboxState.fileCache.files[normalizedPath] = {\n      content,\n      lastModified: Date.now()\n    };\n  }\n  if ((global as any).existingFiles) {\n    (global as any).existingFiles.add(normalizedPath);\n  }\n}\n\nexport async function applyMorphEditToFile(params: {\n  sandbox: any;\n  targetPath: string;\n  instructions: string;","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/firecrawl/open-lovable/blob/69bd93bae7a9c97ef989eb70aabe6797fb3dac89/lib/morph-fast-apply.ts#L147-L183","documentation":"writeFileToSandbox throws this when the sandbox object exposes none of the supported write mechanisms: no provider-native writeFile, no alternative write API, and no sandbox.commands.run. It is a capability-detection failure — the injected sandbox is not a recognized provider that this library knows how to write files with.","triggerScenarios":"Passing a custom, mock, or incorrectly constructed sandbox object lacking both a writeFile-style method and a commands.run method; instantiating a provider class that does not implement the expected interface; a refactor/version change that renamed the sandbox API surface.","commonSituations":"Unit tests injecting a partial sandbox stub; upgrading the sandbox SDK so method names changed (e.g. commands.run removed); wiring the wrong object (e.g. a config object) in place of the sandbox instance.","solutions":["Pass a supported sandbox instance created via SandboxFactory.create ('e2b' or 'vercel') that implements commands.run or a native write method.","Inspect the sandbox object at runtime (log Object.keys(sandbox)) to see which methods it actually exposes.","Update custom providers/mocks to implement the expected provider interface (commands.run or writeFile).","Pin/verify sandbox SDK versions so the API surface (commands.run) matches what this library expects.","Catch the error early at sandbox setup time with a capability check rather than mid-edit."],"exampleFix":"// before\nawait applyMorphEditToFile(anyObjectPassedAsSandbox, path, edit);\n// after\nif (typeof (sandbox as any)?.commands?.run !== 'function' && typeof (sandbox as any)?.writeFile !== 'function') {\n  throw new Error('Sandbox instance does not support file writes (needs commands.run or writeFile)');\n}\nawait applyMorphEditToFile(sandbox, path, edit);","handlingStrategy":"type-guard","validationCode":"function sandboxCanWrite(sandbox: any): boolean {\n  return typeof sandbox?.writeFile === 'function' ||\n    typeof sandbox?.commands?.run === 'function' ||\n    typeof sandbox?.files?.write === 'function';\n}\nif (!sandboxCanWrite(sandbox)) throw new Error('Sandbox does not support file writes');","typeGuard":"function isWritableSandbox(s: unknown): s is { commands: { run: (cmd: string, opts?: object) => Promise<{ exitCode: number; stdout?: unknown }> } } {\n  return !!s && typeof s === 'object' &&\n    typeof (s as any).commands?.run === 'function';\n}","tryCatchPattern":"try {\n  await writeFileToSandbox(sandbox, normalizedPath, fullPath, content);\n} catch (err) {\n  if (err instanceof Error && err.message === 'No available method to write files to sandbox') {\n    console.error('Sandbox object lacks write capability; keys:', Object.keys(sandbox || {}));\n    throw new Error('Sandbox provider not supported — create it via SandboxFactory.create(\"e2b\" | \"vercel\")');\n  }\n  throw err;\n}","preventionTips":["Always create sandbox instances via SandboxFactory.create rather than hand-rolled objects.","Add a capability check (writeFile/commands.run exists) immediately after sandbox creation.","Keep test mocks implementing the full provider interface, not partial stubs.","Write a TypeScript interface for the sandbox and type the parameter instead of `any`.","On SDK upgrades, re-verify the provider API surface (method names) still matches."],"tags":["sandbox","interface","configuration"],"backgroundTag":"unsupported-provider","analyzedSha":"69bd93bae7a9c97ef989eb70aabe6797fb3dac89","analyzedAt":"2026-08-28T22:20:32.339Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}