xpzouying/xiaohongshu-mcp · error
path is required
Error message
path is required
What it means
NewLoadCookie panics with "path is required" when given an empty string. The cookie store requires an explicit file path; an empty path would silently point at a directory or fail later on read/write, so the constructor fails fast. It is a programming/config error, hence the panic rather than an error return.
Source
Thrown at cookies/cookies.go:40
const localCookiesPath = "cookies.json"
type Cookier interface {
LoadCookies() ([]byte, error)
SaveCookies(data []byte) error
DeleteCookies() error
// LoadSeed 读取会话绑定的 seed;老格式、文件损坏或未设时返回 0。
LoadSeed() int
// SaveSeed 写入 seed,保留文件中已有的 cookies。
SaveSeed(seed int) error
}
type localCookie struct {
path string
}
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 {View on GitHub (pinned to 332d196854)
Solutions
- Use cookies.GetCookiesFilePath() to obtain a valid default path instead of a raw env lookup
- Guard the path with a non-empty check before calling NewLoadCookie
- Set COOKIES_PATH to a concrete file path in the deployment environment
- Recover from this panic at startup, or switch to validating config before construction
Example fix
// before
loader := cookies.NewLoadCookie(os.Getenv("COOKIES_PATH")) // panics when env unset
// after
path := os.Getenv("COOKIES_PATH")
if path == "" {
path = cookies.GetCookiesFilePath()
}
loader := cookies.NewLoadCookie(path) Defensive patterns
Strategy: validation
Validate before calling
path := os.Getenv("COOKIES_PATH")
if path == "" {
path = cookies.GetCookiesFilePath() // applies fallbacks
}
if path == "" {
return errors.New("cookies path unresolved")
} Try / catch
func newCookieLoader(path string) (c cookies.Cookier, err error) {
defer func() { if r := recover(); r != nil { err = fmt.Errorf("cookie loader: %v", r) } }()
return cookies.NewLoadCookie(path), nil
} Prevention
- Always source the path via cookies.GetCookiesFilePath(), never raw env reads
- Validate non-empty config before constructing loaders
- Set COOKIES_PATH explicitly in Docker/CI environments
- Wrap constructors that panic in a recover at the config layer
When it happens
Trigger: Calling cookies.NewLoadCookie("") directly, or wiring a path variable that resolved to empty — e.g. os.Getenv("COOKIES_PATH") returned "" and the caller passed it through without applying GetCookiesFilePath's fallback logic.
Common situations: Environment variable COOKIES_PATH set to empty in CI/Docker and used without a default; config struct field not populated before constructing the cookie loader; refactoring removed the GetCookiesFilePath() call and passed a raw env value.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/dda0cc5ac54d7eb8.
Report an issue: GitHub.