{"record":{"id":"d476269ef97caf06","repo":"xpzouying/xiaohongshu-mcp","slug":"create-cookies-dir-failed","errorCode":null,"errorMessage":"create cookies dir failed","messagePattern":"create cookies dir failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cookies/cookies.go","lineNumber":111,"sourceCode":"// write 以 v2 格式落盘。cookies 用 RawMessage 原样嵌入，不经过结构体往返。\nfunc (c *localCookie) write(cks []byte, seed int) error {\n\tif len(cks) == 0 {\n\t\tcks = []byte(\"[]\")\n\t}\n\n\tdata, err := json.MarshalIndent(sessionFile{\n\t\tVersion: 2,\n\t\tSeed:    seed,\n\t\tSavedAt: time.Now().Format(time.RFC3339),\n\t\tCookies: json.RawMessage(cks),\n\t}, \"\", \"  \")\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"marshal session file failed\")\n\t}\n\n\tif dir := filepath.Dir(c.path); dir != \"\" && dir != \".\" {\n\t\tif err := os.MkdirAll(dir, 0755); err != nil {\n\t\t\treturn errors.Wrap(err, \"create cookies dir failed\")\n\t\t}\n\t}\n\n\treturn os.WriteFile(c.path, data, 0644)\n}\n\n// DeleteCookies 删除 cookies 文件。\nfunc (c *localCookie) DeleteCookies() error {\n\tif _, err := os.Stat(c.path); os.IsNotExist(err) {\n\t\t// 文件不存在，返回 nil（认为已经删除）\n\t\treturn nil\n\t}\n\treturn os.Remove(c.path)\n}\n\n// GetCookiesFilePath 获取 cookies 文件路径。\n// 为了向后兼容，如果旧路径 /tmp/cookies.json 存在，则继续使用；\n// 否则使用当前目录下的 cookies.json","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/xpzouying/xiaohongshu-mcp/blob/332d196854a9eac0d2b8c2c0e3d0cc43139d724c/cookies/cookies.go#L93-L129","documentation":"The write helper fails when os.MkdirAll cannot create the parent directory of the cookie file path (cookies/cookies.go:111). This runs before writing the file, guaranteeing nested save paths exist. Failure means the OS refused directory creation: permission denied, a non-directory file occupies the path, or the path is invalid.","triggerScenarios":"Calling SaveCookies or SaveSeed with a cookie path whose parent directory can't be created — e.g. COOKIES_PATH=/root/cookies/cookies.json as a non-root user, a path segment being an existing regular file, or read-only filesystem in containers.","commonSituations":"Pointing COOKIES_PATH into a system directory without write access; Docker volume mounted read-only; a file named like a directory already exists on the path; Windows path with illegal characters.","solutions":["Inspect the wrapped os error: EACCES → fix directory permissions or pick a writable path; ENOTDIR → remove/replace the file occupying a path segment.","Set COOKIES_PATH (or the NewLoadCookie path) to a directory the process can write, e.g. $HOME or the working directory.","In containers, mount the target volume as read-write.","Pre-create the directory manually and verify with a touch test if unsure."],"exampleFix":"// before\nos.Setenv(\"COOKIES_PATH\", \"/root/secure/cookies.json\")\n// after\nos.Setenv(\"COOKIES_PATH\", filepath.Join(os.Getenv(\"HOME\"), \".xhs\", \"cookies.json\"))","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(path)\nif err := os.MkdirAll(dir, 0755); err != nil {\n    return fmt.Errorf(\"cannot prepare cookie dir %s: %w\", dir, err)\n}","typeGuard":null,"tryCatchPattern":"err := store.SaveCookies(cks)\nif err != nil && strings.Contains(err.Error(), \"create cookies dir failed\") {\n    return fmt.Errorf(\"fix COOKIES_PATH permissions: %w\", err)\n}","preventionTips":["Point COOKIES_PATH at a directory the process user can write (e.g. $HOME).","Avoid read-only mounts and system directories for state files.","Ensure no regular file occupies a path segment of the target directory.","Pre-create directories during deployment/setup."],"tags":["filesystem","permissions","cookies","io"],"backgroundTag":"permission-denied","analyzedSha":"332d196854a9eac0d2b8c2c0e3d0cc43139d724c","analyzedAt":"2026-09-05T22:22:55.988Z","contentChangedAt":"2026-09-05T22:22:55.988Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}