unicode-safe-file-writing

When write_file fails with an “unknown error” writing files that contain multi-byte Unicode (such as emojis or special symbols), fall back to using run_shell with a heredoc to reliably write the target file.

Last repository push
GitHub Stars
8,134
Forks
953
Issues / PRs
39

Screenshots

A demonstration from skillπ Lab

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "unicode-safe-file-writing" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-3d7dbe5bf5d1c5ff49a20c0010d8805e47d4e4c9889624d020c5efc758c88c63. Verify the upstream source and version against https://github.com/HKUDS/OpenSpace before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

When write_file fails with an “unknown error” writing files that contain multi-byte Unicode (such as emojis or special symbols), fall back to using run_shell with a heredoc to reliably write the target file.

0 Downloads

Introduction source: third_party · HKUDS · 0.0.1 Reference

SkillPi interpretation

Tasks
How to reliably write files containing multi-byte Unicode (emoji, special symbols) when write_file fails with 'unknown error' by falling back to run_shell with a heredoc.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: unicode-safe-file-writing
description: How to reliably write files containing multi-byte Unicode (emoji, special symbols) when write_file fails with 'unknown error' by falling back to run_shell with a heredoc.

Unicode-Safe File Writing

Problem

The write_file tool may fail with 'unknown error' when the file content contains multi-byte Unicode characters such as:

  • Emoji (e.g. 🚀, ✅, ❌)
  • Special typographic symbols (e.g. →, •, —, ©)
  • Non-ASCII characters outside the Latin-1 range

This is a known encoding limitation of write_file.

Solution

Fall back to run_shell and write the file using a shell heredoc (cat > file << 'EOF' ... EOF). The single-quoted delimiter 'EOF' prevents the shell from interpreting any special characters inside the block.


Step-by-Step Recovery

Step 1 — Detect the failure

If write_file returns an error such as:

Error: unknown error

and the content contains non-ASCII characters, assume a Unicode encoding issue and proceed to the heredoc fallback.

Step 2 — Strip or replace problematic characters (if needed)

Before embedding content in the heredoc, decide whether to:

  • Keep the characters as-is (works if the shell/terminal is UTF-8 aware)
  • Replace emoji/symbols with ASCII equivalents (safest, most portable)
  • Remove them entirely

Common replacements:

Original Replacement
✅ [OK]
❌ [ERROR]
🚀 [START]
→ ->
• -
— --

Step 3 — Write the file via heredoc

Use run_shell with the following pattern:

cat > path/to/file.ext << 'EOF'
...file content here (with Unicode stripped/replaced if needed)...
EOF

Key details:

  • Use 'EOF' (single-quoted) as the heredoc delimiter — this disables all shell variable expansion and special character interpretation inside the block.
  • Do not indent the closing EOF (it must start at column 0).
  • For deeply nested content, choose a delimiter that cannot appear in the content itself (e.g. 'HEREDOC_END', 'FILEEND').

Example

Failing call

write_file(
    path="src/components/StatusPanel.ts",
    content="// Status icons\nconst icons = { ok: '✅', err: '❌', run: '🚀' };\n"
)
# → Error: unknown error

Recovery via heredoc

cat > src/components/StatusPanel.ts << 'EOF'
// Status icons
const icons = { ok: '[OK]', err: '[ERROR]', run: '[START]' };
EOF

Or, if UTF-8 output is acceptable in the environment:

cat > src/components/StatusPanel.ts << 'EOF'
// Status icons
const icons = { ok: '✅', err: '❌', run: '🚀' };
EOF

Multi-File Scenario

When writing several files and one fails, write the others with write_file as normal and apply the heredoc fallback only to the failing file(s):

# Write multiple files that contain Unicode
cat > src/utils/icons.ts << 'EOF'
export const ICONS = {
  success: '[OK]',
  failure: '[FAIL]',
  pending: '[...]',
};
EOF

cat > src/utils/labels.ts << 'EOF'
export const LABELS = {
  title: 'Dashboard',
  subtitle: 'Real-time status',
};
EOF

Important Notes

  1. Always prefer write_file first — only fall back to the heredoc approach when write_file fails.
  2. Verify after writing — use run_shell with cat path/to/file or head -5 path/to/file to confirm the content was written correctly.
  3. Directory creation — write_file auto-creates parent directories; the heredoc approach does not. Pre-create directories with mkdir -p path/to/dir if needed.
  4. Large files — heredocs work well for files of any practical size; there is no meaningful shell limit.
  5. Escaping backslashes — inside a single-quoted heredoc, backslashes are treated literally (no escaping needed), which is usually desirable.

Current listing version

0.0.1

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value78/100

Highlights

  • 解决 write_file 工具在处理多字节 Unicode 字符时失败的实际问题
  • 提供明确的回退方案(run_shell + heredoc),适用于开发者在文件写入失败时的恢复场景
Trigger Precision92/100

Highlights

  • description 明确描述了触发条件(write_file 因“未知错误”无法写入包含多字节 Unicode 的文件)
  • 覆盖 emoji、特殊符号等典型场景,边界清晰
Structure and Standards95/100

Highlights

  • 文档结构清晰,包含 Problem、Solution、Step-by-Step Recovery、Example、Multi-File Scenario、Important Notes 等完整章节
  • 步骤划分明确,有端到端示例,frontmatter 完整
Execution Reliability90/100

Highlights

  • 工具来源明确(run_shell),参数正确
  • 覆盖了主要异常情况(如目录创建、大文件处理、字符替换策略),提供了验证写入结果的方法
Evidence Verifiability (mean of the first four)89/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.