xpzouying/xiaohongshu-mcp · error

failed to read cookies from tmp file

Error message

failed to read cookies from tmp file

What it means

LoadCookies wraps an os.ReadFile failure on the cookie file path (cookies/cookies.go:54) via errors.Wrap. The most common wrapped cause is file-not-found (os.ErrNotExist), i.e. no session has been saved yet at the configured path. The path comes from NewLoadCookie(path) or GetCookiesFilePath(), which honors the COOKIES_PATH env var.

Source

Thrown at cookies/cookies.go:54

}

func NewLoadCookie(path string) Cookier {
	if path == "" {
		panic("path is required")
	}

	return &localCookie{
		path: path,
	}
}

// LoadCookies 从文件中加载 cookies 数组的原始字节。
// v2 从外层对象里取出 cookies 字段;v1 文件本身就是数组,原样返回。
func (c *localCookie) LoadCookies() ([]byte, error) {

	data, err := os.ReadFile(c.path)
	if err != nil {
		return nil, errors.Wrap(err, "failed to read cookies from tmp file")
	}

	var f sessionFile
	if err := json.Unmarshal(data, &f); err == nil && len(f.Cookies) > 0 {
		return f.Cookies, nil
	}

	return data, nil
}

// LoadSeed 读取会话绑定的 seed。老格式(裸数组)没有这个值,返回 0。
func (c *localCookie) LoadSeed() int {
	data, err := os.ReadFile(c.path)
	if err != nil {
		return 0
	}

	var f sessionFile

View on GitHub (pinned to 332d196854)

Solutions

  1. Check os.IsNotExist on the wrapped error — if so, perform a fresh login and SaveCookies to create the file.
  2. Verify COOKIES_PATH / the path passed to NewLoadCookie points to the right file.
  3. Check file permissions and that the path isn't a directory.
  4. Treat a missing file as an empty session: re-authenticate rather than retrying the load.

Example fix

// before
cks, err := store.LoadCookies()
if err != nil { return err }
// after
cks, err := store.LoadCookies()
if err != nil {
    if errors.Is(err, fs.ErrNotExist) {
        return loginAndSaveSession(ctx) // first run
    }
    return err
}
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
    return loginAndSaveSession(ctx) // no session yet
}

Try / catch

cks, err := store.LoadCookies()
if err != nil {
    if errors.Is(err, fs.ErrNotExist) {
        return freshLogin(ctx) // expected on first run
    }
    return err // real I/O problem (permissions, etc.)
}

Prevention

When it happens

Trigger: Calling LoadCookies (directly or via SaveSeed, which calls LoadCookies first) when the cookie file doesn't exist at c.path, the path points to a directory, or the process lacks read permission.

Common situations: First run before any SaveCookies; COOKIES_PATH env var pointing to a stale/nonexistent location; running in a container where /tmp/cookies.json (legacy path) was wiped; permission mismatch after switching users.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/7d9279adb198b1fa. Report an issue: GitHub.