{"record":{"id":"fe60ae8abb09568e","repo":"pola-rs/polars","slug":"error-initializing-temporary-directory-e-consider-explicitly","errorCode":null,"errorMessage":"error initializing temporary directory: {e} consider explicitly setting POLARS_TEMP_DIR","messagePattern":"error initializing temporary directory: (.+?) consider explicitly setting POLARS_TEMP_DIR","errorType":"error_code","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"crates/polars-io/src/path_utils/mod.rs","lineNumber":80,"sourceCode":"            // Setting permissions on Windows is not as easy compared to Unix, but fortunately\n            // the default temporary directory location is underneath the user profile, so we\n            // shouldn't need to do anything.\n            std::env::temp_dir().join(\"polars/\")\n        } else {\n            std::env::temp_dir().join(\"polars/\")\n        }\n        .into_boxed_path();\n\n        let perm_result = create_dir_owner_only(path.as_ref());\n\n        if std::env::var(\"POLARS_ALLOW_UNSECURED_TEMP_DIR\").as_deref() != Ok(\"1\") {\n            perm_result?;\n        }\n\n        std::io::Result::Ok(path)\n    })()\n    .map_err(|e| {\n        std::io::Error::new(\n            e.kind(),\n            format!(\n                \"error initializing temporary directory: {e} \\\n                 consider explicitly setting POLARS_TEMP_DIR\"\n            ),\n        )\n    })\n    .unwrap()\n});\n\n/// Create a directory (and parents) with owner-only permissions (0o700) on Unix.\npub fn create_dir_owner_only(path: &Path) -> std::io::Result<()> {\n    std::fs::create_dir_all(path)?;\n\n    #[cfg(target_family = \"unix\")]\n    {\n        use std::os::unix::fs::PermissionsExt;\n","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-io/src/path_utils/mod.rs#L62-L98","documentation":"This io::Error wraps any failure that occurs while Polars initializes its temporary directory (e.g. directory creation or path probing failed). The message suggests setting POLARS_TEMP_DIR explicitly because the default temporary-directory location may be unusable. The original error kind is preserved.","triggerScenarios":"Any path that triggers temporary-directory initialization in polars-io path_utils when the chosen temp location cannot be created or resolved: permission-denied mkdir, read-only filesystem, missing parent directory, invalid path.","commonSituations":"Running in sandboxed/air-gapped environments where TMPDIR is unset or points somewhere non-writable; containers running as non-root; cloud workers with restricted /tmp; users with a broken TMPDIR env var.","solutions":["Set POLARS_TEMP_DIR to a writable directory path before running the process.","Check/fix TMPDIR (or OS temp conventions) so the default temp path is writable.","Ensure the process user has write permission on the temp directory's parent.","Read the inner e.kind()/cause to identify the specific OS failure (PermissionDenied vs NotFound)."],"exampleFix":"// before\n$ cargo run  # fails: default temp dir not writable\n// after\n$ POLARS_TEMP_DIR=/data/mytmp cargo run","handlingStrategy":"validation","validationCode":"let tmp = std::env::var(\"POLARS_TEMP_DIR\")\n    .or_else(|_| std::env::var(\"TMPDIR\"))\n    .unwrap_or_else(|_| \"/tmp\".to_string());\nassert!(std::path::Path::new(&tmp).is_dir(), \"temp dir missing: {tmp}\");\nassert!(std::fs::metadata(&tmp)?.permissions().readonly() == false);","typeGuard":null,"tryCatchPattern":"match std::io::Result::Err(e) {\n    e if e.kind() == std::io::ErrorKind::PermissionDenied => {\n        std::env::set_var(\"POLARS_TEMP_DIR\", \"/data/writable-tmp\");\n        // retry the operation\n    }\n    _ => return Err(e),\n}","preventionTips":["Set POLARS_TEMP_DIR explicitly in container/CI/cloud environments","Verify the process user can create directories in the temp location at startup","Log TMPDIR/POLARS_TEMP_DIR values during deployment health checks"],"tags":["temp-directory","environment","io","config"],"backgroundTag":"missing-env-var","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}