{"record":{"id":"342ab5a2494b3a32","repo":"screenpipe/screenpipe","slug":"failed-to-write-file","errorCode":null,"errorMessage":"Failed to write file {}: {}","messagePattern":"Failed to write file (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/screenpipe-engine/src/cli/mcp.rs","lineNumber":265,"sourceCode":"        .map_err(|e| anyhow::anyhow!(\"Failed to parse GitHub API response: {}\", e))?;\n\n    for item in contents {\n        let target_path = target_dir.join(&item.name);\n\n        match item.content_type.as_str() {\n            \"file\" => {\n                if let Some(download_url) = item.download_url {\n                    let file_response = client.get(&download_url).send().await.map_err(|e| {\n                        anyhow::anyhow!(\"Failed to download file {}: {}\", download_url, e)\n                    })?;\n\n                    let content = file_response\n                        .bytes()\n                        .await\n                        .map_err(|e| anyhow::anyhow!(\"Failed to get file content: {}\", e))?;\n\n                    tokio::fs::write(&target_path, content).await.map_err(|e| {\n                        anyhow::anyhow!(\"Failed to write file {}: {}\", target_path.display(), e)\n                    })?;\n\n                    debug!(\"Downloaded file: {}\", target_path.display());\n                }\n            }\n            \"dir\" => {\n                tokio::fs::create_dir_all(&target_path).await.map_err(|e| {\n                    anyhow::anyhow!(\n                        \"Failed to create directory {}: {}\",\n                        target_path.display(),\n                        e\n                    )\n                })?;\n\n                let subdir_api_url = format!(\n                    \"https://api.github.com/repos/{}/{}/contents/{}?ref={}\",\n                    \"screenpipe\", \"screenpipe\", item.path, \"main\"\n                );","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/screenpipe/screenpipe/blob/4ebf712990fee17eeaf904dacf749b6e96ac9bf3/crates/screenpipe-engine/src/cli/mcp.rs#L247-L283","documentation":"This error wraps an I/O failure from tokio::fs::write while persisting a downloaded file from an MCP directory archive to its target path on disk. The anyhow context adds the destination path and the underlying std::io::Error so you can see both where the write failed and why (permissions, disk full, missing parent dir, etc.).","triggerScenarios":"download_mcp_directory fetched a 'file' entry from the archive listing, downloaded its bytes successfully, but tokio::fs::write(&target_path, content) failed — e.g. the parent directory was not created first, the path is not writable, or the disk is full.","commonSituations":"Downloading an MCP directory into a location without write permissions (~/Library or C:\\Program Files), antivirus/backup software locking the file, or a race where the target path exists as a directory so the write fails with IsADirectory.","solutions":["Read the wrapped io::Error kind in the message and fix the underlying cause (permissions, disk space, path type).","Ensure the parent directory exists before writing (create_dir_all on target_path's parent).","Check that target_path is not an existing directory; remove or pick a different target.","Retry the download if it was a transient lock (AV scanner, cloud-sync placeholder)."],"exampleFix":"// before\ntokio::fs::write(&target_path, content).await.map_err(|e| {\n    anyhow::anyhow!(\"Failed to write file {}: {}\", target_path.display(), e)\n})?;\n// after\nif let Some(parent) = target_path.parent() {\n    tokio::fs::create_dir_all(parent).await?;\n}\ntokio::fs::write(&target_path, content).await.map_err(|e| {\n    anyhow::anyhow!(\"Failed to write file {}: {}\", target_path.display(), e)\n})?;","handlingStrategy":"try-catch","validationCode":"// before download\nif let Some(parent) = std::path::Path::new(&target_path).parent() {\n    tokio::fs::create_dir_all(parent).await?;\n}","typeGuard":null,"tryCatchPattern":"match tokio::fs::write(&target_path, content).await {\n    Ok(_) => {},\n    Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => eprintln!(\"no write permission at {}\", target_path.display()),\n    Err(e) => return Err(anyhow::anyhow!(\"write {} failed: {}\", target_path.display(), e)),\n}","preventionTips":["Create parent directories before writing downloaded files.","Run the CLI as a user with write access to the target directory.","Exclude the download directory from AV/cloud-sync interference.","Check disk free space for large MCP directories."],"tags":["filesystem","io","mcp","download"],"backgroundTag":"file-write-permission-denied","analyzedSha":"4ebf712990fee17eeaf904dacf749b6e96ac9bf3","analyzedAt":"2026-09-01T23:33:43.065Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}