{"record":{"id":"ef53c9d026388e4c","repo":"jdx/mise","slug":"directory-changed-while-restoring-permissions","errorCode":null,"errorMessage":"directory changed while restoring permissions: {}","messagePattern":"directory changed while restoring permissions: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/system/history/replay.rs","lineNumber":1602,"sourceCode":"    // a regular file is replaced in place: the atomic write keeps its mode\n    file::write_atomic(path, &bytes)?;\n    #[cfg(unix)]\n    {\n        use std::os::unix::fs::PermissionsExt;\n        std::fs::set_permissions(path, std::fs::Permissions::from_mode(bits))?;\n    }\n    Ok(())\n}\n\n/// Puts back the recorded mode of the directories above a restored file\n/// (a `0700` directory recreated by `create_dir_all` would be `0755`).\n#[cfg(unix)]\nfn restore_dir_modes(_step: &Step, created: &[(PathBuf, u32)]) -> Result<()> {\n    use std::os::unix::fs::PermissionsExt;\n    for (dir, bits) in created {\n        let metadata = std::fs::symlink_metadata(dir)?;\n        if !metadata.is_dir() || metadata.file_type().is_symlink() {\n            bail!(\n                \"directory changed while restoring permissions: {}\",\n                display_path(dir)\n            );\n        }\n        std::fs::set_permissions(dir, std::fs::Permissions::from_mode(*bits))?;\n    }\n    Ok(())\n}\n\n#[cfg(not(unix))]\nfn restore_dir_modes(step: &Step, _created: &[(PathBuf, u32)]) -> Result<()> {\n    if step.bits.is_some() || !step.dir_bits.is_empty() {\n        debug!(\n            \"history: {}: recorded permission bits are not restored on this platform\",\n            display_path(&step.path)\n        );\n    }\n    Ok(())","sourceCodeStart":1584,"sourceCodeEnd":1620,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/history/replay.rs#L1584-L1620","documentation":"restore_dir_modes re-applies saved permission bits to directories it created earlier in a write step. Before chmod it re-checks each path is still a real directory (not a symlink or replaced file); if the filesystem entry changed, it aborts instead of setting permissions on the wrong object.","triggerScenarios":"Between capturing the (dir, mode) pairs and restoring them, the path was replaced by a symlink or a non-directory file — e.g. a concurrent process removed/renamed the directory and put something else there, or the write_object recovery path detected replaced paths (directory_permission_recovery_rejects_replaced_paths).","commonSituations":"Concurrent mise operations or external tools racing with a history write; a state-dir path being replaced by a symlink; interrupted operations partially cleaned up by other tooling.","solutions":["Remove or fix the object now at that path (it should be a real directory) and retry the operation","Stop concurrent processes touching MISE_STATE_DIR and re-run the operation","Check the path for unexpected symlinks: ls -la on the reported path"],"exampleFix":"// before: path became a symlink\nmv ~/.local/state/mise/history/dir ~/.local/state/mise/history/dir.bak\nmkdir ~/.local/state/mise/history/dir   // after: restore real dir, retry","handlingStrategy":"retry","validationCode":"let md = std::fs::symlink_metadata(dir)?;\nif !md.is_dir() || md.file_type().is_symlink() {\n    eprintln!(\"path changed, skipping: {}\", dir.display());\n}","typeGuard":null,"tryCatchPattern":"match history.write_object(step) {\n    Err(e) if e.to_string().contains(\"directory changed while restoring permissions\") => {\n        std::thread::sleep(Duration::from_millis(50));\n        history.write_object(step)?; // retry once after settling\n    }\n    other => other?,\n}","preventionTips":["Avoid concurrent mise runs against the same MISE_STATE_DIR","Never replace state-dir subdirectories with symlinks","Serialize history writes with the operation lock"],"tags":["filesystem","permissions","race-condition","unix"],"backgroundTag":"invalid-state-transition","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}