touch_file

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

touch_file

touch_file

When a Write or Edit tool call fails while trying to create a new file, recover by using
touch
via the Bash tool to create the empty file first, then retry the Write.
当Write或Edit工具调用在尝试创建新文件失败时,通过Bash工具使用
touch
命令先创建空文件,然后重试Write操作来恢复。

Why this matters

为什么这很重要

The Write tool can fail when creating new files — often because parent directories don't exist, or the filesystem needs the file to be created differently. Rather than giving up or trying workarounds, a quick
mkdir -p
+
touch
reliably sets up the file path so the Write tool succeeds on retry.
Write工具在创建新文件时可能会失败——通常是因为父目录不存在,或者文件系统需要以不同方式创建文件。与其放弃或尝试其他解决方法,快速执行
mkdir -p
+
touch
可以可靠地设置文件路径,使Write工具在重试时成功。

Recovery steps

恢复步骤

When Write or Edit fails on a new file (not an existing one):
  1. Create parent directories — run
    mkdir -p <parent-directory>
    via Bash to ensure the full directory path exists.
  2. Create the empty file — run
    touch <filepath>
    via Bash.
  3. Retry the Write — use the Write tool again with the same content. It should succeed now.
You can combine steps 1 and 2:
bash
mkdir -p src/utils && touch src/utils/helpers.ts
当Write或Edit在新文件(而非现有文件)上失败时:
  1. 创建父目录——通过Bash运行
    mkdir -p <父目录>
    以确保完整的目录路径存在。
  2. 创建空文件——通过Bash运行
    touch <文件路径>
  3. 重试Write操作——再次使用Write工具并传入相同内容,现在应该会成功。
你可以将步骤1和2合并:
bash
mkdir -p src/utils && touch src/utils/helpers.ts

When this applies

适用场景

  • A Write tool call fails and the target file does not already exist
  • The error mentions missing directories, file not found, or path issues
  • You're trying to create a file in a nested directory structure
  • Write工具调用失败,且目标文件尚未存在
  • 错误提示提及缺少目录、文件未找到或路径问题
  • 你尝试在嵌套目录结构中创建文件

Edge case: broken symlinks

边缘情况:损坏的符号链接

If
mkdir -p
fails because a broken symlink occupies a path component (error like
No such file or directory
even though
mkdir -p
should create it), the symlink is pointing to a nonexistent target and blocking directory creation. Do not remove it automatically — the symlink may be there intentionally. Instead, tell the user what you found and ask how they'd like to proceed (e.g., remove the symlink, fix its target, or choose a different path).
如果
mkdir -p
因路径组件被损坏的符号链接占用而失败(例如错误提示“没有该文件或目录”,即使
mkdir -p
应该创建它),说明该符号链接指向不存在的目标并阻止了目录创建。请勿自动删除它——该符号链接可能是有意存在的。相反,告知用户你发现的问题并询问他们希望如何处理(例如删除符号链接、修复其目标,或选择其他路径)。

When this does NOT apply

不适用场景

  • Edit or Write fails on a file that already exists — that's a different problem
  • The error is unrelated to file creation (e.g., permission denied on an existing file, disk full)
  • Edit或Write在已存在的文件上失败——这是另一个问题
  • 错误与文件创建无关(例如现有文件权限被拒绝、磁盘已满)