siyuan-note/siyuan · error

create import dir failed: %s

Error message

create import dir failed: %s

What it means

Raised by HTTPRequest when a non-text (binary) HTTP response cannot be saved to disk. After reading the response body, the function calls os.MkdirAll on TempDir/import to stage the downloaded file for the agent; if that mkdir fails the whole request aborts. The wrapped %s is the underlying OS error. This path is only hit for content types that isTextContentType rejects (anything other than text/*, application/json, application/xml, and the +json/+xml suffixes).

Source

Thrown at kernel/util/httprequest.go:116

	maxReadBytes := int64(maxHTTPRequestBytes)
	if !isTextContentType(contentType) {
		maxReadBytes = maxHTTPRequestFileBytes
	}
	// ContentLength 为 -1(chunked)时跳过大小预检,交由 LimitReader 兜底截断。
	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
}

// sendByMethod 按 method 分发请求,统一走 NewBrowserRequest 返回的 *req.Request。
func sendByMethod(request *req.Request, method, rawURL string) (*req.Response, error) {
	switch method {
	case "GET", "":
		return request.Get(rawURL)
	case "POST":

View on GitHub (pinned to 251596fc0d)

Solutions

  1. Verify util.TempDir is set to a writable directory and that no regular file occupies TempDir/import (remove it if so).
  2. Free disk space on the volume holding TempDir and retry the http_request call.
  3. Run the kernel process with a user account that has create/write permission on the workspace temp directory.
  4. If the asset is actually text but mis-served as binary, set a correct Accept header so isTextContentType returns true and the file-save branch is skipped.

Example fix

// before: TempDir unwritable, binary save fails
// after: point TempDir at a writable location before booting
os.Setenv("TMPDIR", "/var/siyuan/tmp")
// or ensure the import dir is a directory, not a stray file
importDir := filepath.Join(util.TempDir, "import")
if info, err := os.Stat(importDir); err == nil && !info.IsDir() {
    os.Remove(importDir)
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: ensure TempDir/import is creatable before issuing binary fetches.
importDir := filepath.Join(util.TempDir, "import")
if info, err := os.Stat(importDir); err == nil && !info.IsDir() {
    return fmt.Errorf("%s exists and is not a directory", importDir)
}
if err := os.MkdirAll(importDir, 0755); err != nil {
    return fmt.Errorf("import dir unavailable: %w", err)
}

Prevention

When it happens

Trigger: util.HTTPRequest returns a binary Content-Type (e.g. image/png, application/pdf, application/octet-stream), then os.MkdirAll(filepath.Join(TempDir, "import"), 0755) fails. Typical causes: TempDir resolves to a path the process cannot create (read-only mount, removed parent), the disk is full, or a file (not a directory) already exists at that path.

Common situations: Running the kernel with TempDir pointing at a read-only or noexec filesystem; running as a user without write permission to the temp workspace; disk-full conditions during an agent http_request tool call that fetches a binary asset; a stale file named 'import' left in TempDir from a prior crash.

Related errors


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