Reliable fallback technique for writing large or special-character-heavy file content when inline write_file or shell_agent calls fail with unknown errors — use
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-large-file-write-via-temp-script-1e40c412f6f8 ,按照其中的说明把「large-file-write-via-temp-script」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Writing large files — especially TypeScript, TSX, or any content containing
Unicode emoji, backticks, ${} template literals, or multi-line heredoc
conflicts — can silently fail or truncate content when passed inline.
Failure modes observed:
write_file returns [ERROR] unknown error on large payloadsnode -e '...' with backtick template literals silently empties the target filepython3 -c '...' breaks on unescaped quotes or special charactersshell_agent may time out or fail when the inline content is too largeInstead of passing file content inline, write a small Python helper script to
/tmp/write_task.py (or similar), then execute it. The helper script holds
the target file content as a raw Python string or via base64 decoding.
Use write_file or a heredoc to create the helper, then run it:
# Step 1: Write the helper script to /tmp
cat > /tmp/write_task.py << 'PYEOF'
import pathlib
content = r"""
// Your TypeScript / file content goes here
// Backticks `like this`, ${template} literals, and emoji 🎉 are all safe
export const MyComponent = () => {
const msg = `Hello, world!`;
return <div>{msg}</div>;
};
"""
pathlib.Path("/path/to/target/file.ts").write_text(content.lstrip("\n"), encoding="utf-8")
print("Written successfully.")
PYEOF
# Step 2: Execute the helper
python3 /tmp/write_task.py
Why
r"""? A raw triple-quoted string avoids all backslash escaping issues. The only content that cannot appear verbatim is"""itself — if needed, split the string or escape that sequence only.
When the content contains """ or other Python string edge cases:
# Step 1: Base64-encode your content (done once, outside the target system)
# python3 -c "import base64; print(base64.b64encode(open('file.ts').read().encode()).decode())"
# Step 2: Embed the base64 blob in the helper script
cat > /tmp/write_task.py << 'PYEOF'
import base64, pathlib
b64 = (
"aW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0JzsKCmV4cG9ydCBjb25zdCBNeUNvbXBvbmVudCA9"
"ICgpID0+IHsKICByZXR1cm4gPGRpdj5IZWxsbzwvZGl2PjsKfTsK"
# ... more lines if needed
)
content = base64.b64decode(b64).decode("utf-8")
pathlib.Path("/path/to/target/file.ts").write_text(content, encoding="utf-8")
print(f"Written {len(content)} bytes successfully.")
PYEOF
python3 /tmp/write_task.py
Ask shell_agent to perform the write using the temp-script pattern:
"Write the following TypeScript content to
/src/components/MyPanel.ts. Do NOT usenode -eor inline Python. Instead write a Python script to/tmp/write_panel.pyusing a triple-quoted raw string, then execute it."
| Situation | Recommended Approach |
|---|---|
Content has backticks / ${} template literals | Approach A (raw r""") |
Content has """ sequences | Approach B (base64) |
| Content is binary or non-UTF-8 | Approach B (base64) |
| Delegating to shell_agent | Approach C with explicit instructions |
| Quick small file | Direct write_file (no workaround needed) |
# ❌ DANGEROUS — backtick template literals will break node -e
node -e "const fs = require('fs'); fs.writeFileSync('out.ts', \`${content}\`);"
# ❌ FRAGILE — breaks on unescaped quotes and special characters
python3 -c "open('out.ts','w').write('${content}')"
# ❌ UNRELIABLE for large payloads — may silently truncate
echo "${large_content}" > out.ts
Always verify the write succeeded:
# Check file exists and has non-zero size
python3 -c "
import pathlib
p = pathlib.Path('/path/to/target/file.ts')
assert p.exists() and p.stat().st_size > 0, f'Write failed: {p}'
print(f'OK — {p.stat().st_size} bytes written to {p}')
"
.ts, .tsx, .js, .json, .md, .py, etc.lstrip("\n") the triple-quoted string to avoid a leading blank line.rm /tmp/write_task.py