siyuan-note/siyuan · error

write file failed:

Error message

write file failed: 

What it means

After creating the import directory, HTTPRequest writes the binary response body to <TempDir>/import/<filename> via os.WriteFile. This error wraps any failure of that write — permission denied, disk full, the target path being a directory, or an I/O error — appending the OS message after "write file failed: ".

Solutions

  1. Read the appended OS error (permission denied / no space left on device / is a directory) and fix the concrete cause
  2. Free disk space or raise quota on the temp volume
  3. Correct permissions on <TempDir>/import so the kernel process can create files
  4. If the filename collides with an existing directory, remove that directory or clear old import files
  5. Check antivirus/EDR exclusions for the workspace temp path

Example fix

// before
open /tmp/siyuan/import/report.pdf: is a directory
// after
rm -rf /tmp/siyuan/import/report.pdf   # remove the colliding directory, then retry the fetch
Defensive patterns

Strategy: validation

Validate before calling

importDir := filepath.Join(util.TempDir, "import")
probe := filepath.Join(importDir, ".write-probe")
if err := os.WriteFile(probe, []byte("ok"), 0o644); err != nil {
    return fmt.Errorf("import dir not writable: %w", err)
}
os.Remove(probe)

Try / catch

if err != nil && strings.HasPrefix(err.Error(), "write file failed:") {
    // free disk space / fix perms / clear filename collisions, then retry
}

Prevention

When it happens

Trigger: Calling HTTPRequest with a binary response when the import directory exists but is not writable, the disk fills mid-write, the derived filename (from extractFilename of URL/Content-Type) resolves to an existing directory or an invalid path, or antivirus/EDR locks the target file.

Common situations: Full disk during large binary downloads; a directory already exists with the exact filename the server's Content-Disposition/URL implies; read-only temp volume; security software quarantining the file mid-write; concurrent writes colliding on the same filename.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/3b8c56f258fdbd5b. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/httprequest.go:368

	if resp.ContentLength > maxReadBytes {
		return statusCode, contentType, "", errors.New("response too large")
	}

	respBody, rerr := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))
	if rerr != nil {
		return statusCode, contentType, "", errors.New("read body failed: " + rerr.Error())
	}

	// 二进制响应落盘,返回文件路径,供智能体按需进一步处理。
	if !isTextContentType(contentType) {
		importDir := filepath.Join(TempDir, "import")
		if merr := os.MkdirAll(importDir, 0755); merr != nil {
			return statusCode, contentType, "", errors.New("create import dir failed: " + merr.Error())
		}
		filename := extractFilename(rawURL, contentType)
		filePath := filepath.Join(importDir, filename)
		if werr := os.WriteFile(filePath, respBody, 0644); werr != nil {
			return statusCode, contentType, "", errors.New("write file failed: " + werr.Error())
		}
		return statusCode, contentType, fmt.Sprintf("Saved to: %s (%d bytes)", filePath, len(respBody)), nil
	}

	return statusCode, contentType, truncateRunes(string(respBody), maxHTTPRequestChars), nil
}

// isTextContentType 判断 Content-Type 是否为可直接展示给智能体的文本类响应。
// 覆盖 text/*、application/json、application/xml、application/*+json 等。
func isTextContentType(contentType string) bool {
	ct := strings.ToLower(strings.TrimSpace(strings.SplitN(contentType, ";", 2)[0]))
	if ct == "" {
		return false
	}
	if strings.HasPrefix(ct, "text/") {
		return true
	}
	switch ct {

View on GitHub (pinned to 9f775e8a12)