siyuan-note/siyuan · error

create import dir failed: %s

Error message

create import dir failed: %s

What it means

Returned at webfetch.go:81 when os.MkdirAll(filepath.Join(TempDir, "import"), 0755) fails. This happens only on the non-text download branch (content type is not text/html or text/plain), after the body has already been read successfully. It is a local-filesystem error, not a network error.

Source

Thrown at kernel/util/webfetch.go:81

	contentType := resp.Header.Get("Content-Type")
	maxReadBytes := int64(maxWebFetchBytes)
	if !strings.HasPrefix(contentType, "text/html") && !strings.HasPrefix(contentType, "text/plain") {
		maxReadBytes = maxWebFetchFileBytes
	}
	if resp.ContentLength > maxReadBytes {
		return "", errors.New("response too large")
	}

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

	if !strings.HasPrefix(contentType, "text/html") && !strings.HasPrefix(contentType, "text/plain") {
		importDir := filepath.Join(TempDir, "import")
		if merr := os.MkdirAll(importDir, 0755); merr != nil {
			return "", errors.New("create import dir failed: " + merr.Error())
		}
		filename := extractFilename(rawURL, contentType)
		filePath := filepath.Join(importDir, filename)
		if werr := os.WriteFile(filePath, body, 0644); werr != nil {
			return "", errors.New("write file failed: " + werr.Error())
		}
		return fmt.Sprintf("Saved to: %s (%d bytes)", filePath, len(body)), nil
	}

	htmlStr := string(body)

	isHTML := strings.HasPrefix(contentType, "text/html")
	if !isHTML {
		return truncateRunes(htmlStr, maxWebFetchChars), nil
	}

	if htmlStr == "" {
		return "", nil

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Check util.TempDir value at runtime and confirm the directory can be created with `mkdir -p`.
  2. Free disk space on the volume holding TempDir.
  3. Fix ownership/permissions so the SiYuan process can write under TempDir.
  4. On sandboxed installs, ensure TempDir is inside the writable sandbox area.
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the import target is creatable before triggering a download.
func importDirWritable() error {
    dir := filepath.Join(util.TempDir, "import")
    return os.MkdirAll(dir, 0755)
}

Type guard

func isCreateImportDirFailed(err error) bool {
    return err != nil && strings.HasPrefix(err.Error(), "create import dir failed:")
}

Try / catch

if err := importDirWritable(); err != nil {
    return fmt.Errorf("cannot save downloads: %w", err)
}
out, err := util.WebFetch(raw, "markdown")

Prevention

When it happens

Trigger: TempDir resolves to a read-only or non-existent base path, the process lacks create permission on the target, the disk is full, or the composed path is invalid (e.g. embedded NUL, path-too-long on Windows).

Common situations: Snap/Flatpak confinement denying write outside the sandbox, iOS sandbox permission change, running off a read-only mount, TempDir pointing at a removed tmpfs, antivirus interfering on Windows.

Related errors


AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12). Data as JSON: /api/errors/5332e7999b6eaf9b. Report an issue: GitHub.