firecrawl/open-lovable · error · Error

Failed to write file via command: ${writeResult.stderr}

Error message

Failed to write file via command: ${writeResult.stderr}

What it means

VercelProvider.writeFile falls back to writing via a shell command (e.g. heredoc/echo) inside the sandbox; when that command exits non-zero, the provider throws 'Failed to write file via command: <stderr>'. Unlike the null-sandbox guard, the sandbox exists but the write itself failed.

Source

Thrown at lib/sandbox/providers/vercel-provider.ts:186

      // Write file using echo and redirection
      const escapedContent = content
        .replace(/\\/g, '\\\\')
        .replace(/"/g, '\\"')
        .replace(/\$/g, '\\$')
        .replace(/`/g, '\\`')
        .replace(/\n/g, '\\n');
      
      const writeResult = await this.sandbox.runCommand({
        cmd: 'sh',
        args: ['-c', `echo "${escapedContent}" > "${fullPath}"`]
      });
      
      // File written
      
      if (writeResult.exitCode === 0) {
        this.existingFiles.add(path);
      } else {
        throw new Error(`Failed to write file via command: ${writeResult.stderr}`);
      }
    }
  }

  async readFile(path: string): Promise<string> {
    if (!this.sandbox) {
      throw new Error('No active sandbox');
    }

    // Vercel sandbox default working directory is /vercel/sandbox
    const fullPath = path.startsWith('/') ? path : `/vercel/sandbox/${path}`;
    
    const result = await this.sandbox.runCommand({
      cmd: 'cat',
      args: [fullPath]
    });
    
    // Handle stdout and stderr - they might be functions in Vercel SDK

View on GitHub (pinned to 69bd93bae7)

Solutions

  1. Read writeResult.stderr in the thrown message and fix the specific shell/quoting issue; prefer base64-encoding content before embedding it in the command and decoding in the sandbox
  2. Use the provider's primary filesystem write API (files.write) and ensure createSandbox() ran so the fallback path is never taken
  3. Sanitize/escape path and content before interpolation
  4. Verify the target directory exists (mkdir -p) and is writable inside the sandbox

Example fix

// before (fragile interpolation)
const cmd = `cat > ${fullPath} <<EOF\n${content}\nEOF`;
await provider.runCommand(cmd);
// after (encode to survive shell)
const b64 = Buffer.from(content).toString('base64');
await provider.runCommand(`mkdir -p $(dirname ${fullPath}) && echo ${b64} | base64 -d > ${fullPath}`);
Defensive patterns

Strategy: try-catch

Validate before calling

// prefer the native write path and validate inputs first
if (!provider.sandbox) await provider.createSandbox();
if (/[`$"\\]/.test(content)) {
  // shell-sensitive content: use base64-encoded command, not raw heredoc
}
if (!content.endsWith('\n')) content += '\n';

Try / catch

try {
  await provider.writeFile(path, content);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Failed to write file via command:')) {
    const b64 = Buffer.from(content).toString('base64');
    await provider.runCommand(`mkdir -p $(dirname ${fullPath}) && echo ${b64} | base64 -d > ${fullPath}`);
  } else throw e;
}

Prevention

When it happens

Trigger: The fallback command's exitCode !== 0 — typically shell quoting/escaping problems when content contains backticks, $, quotes, or newlines; path not writable in /vercel/sandbox; disk-full or command not available in the sandbox image.

Common situations: Writing generated code/TSX with template literals or dollar signs that the shell interpolates; very large files blowing argument/heredoc limits; read-only working directory; sandbox image lacking the shell utility used by the fallback.

Related errors


AI-assisted analysis of firecrawl/open-lovable@69bd93bae7 (2026-08-28). Data as JSON: /api/errors/dd7e1f4a5769aa94. Report an issue: GitHub.