{"record":{"id":"614db4675b5712f9","repo":"Schniz/fnm","slug":"can-t-create-cd-cmd-file-for-use-on-cd-at","errorCode":null,"errorMessage":"Can't create cd.cmd file for use-on-cd at {}: {}","messagePattern":"Can't create cd\\.cmd file for use-on-cd at (.+?): (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/shell/windows_cmd/mod.rs","lineNumber":33,"sourceCode":"            std::env::var_os(\"path\").ok_or_else(|| anyhow::anyhow!(\"Can't read PATH env var\"))?;\n        let mut split_paths: Vec<_> = std::env::split_paths(&current_path).collect();\n        split_paths.insert(0, path.to_path_buf());\n        let new_path = std::env::join_paths(split_paths)\n            .map_err(|err| anyhow::anyhow!(\"Can't join paths: {err}\"))?;\n        let new_path = new_path\n            .to_str()\n            .ok_or_else(|| anyhow::anyhow!(\"Can't convert path to string\"))?;\n        Ok(format!(\"SET PATH={new_path}\"))\n    }\n\n    fn set_env_var(&self, name: &str, value: &str) -> String {\n        format!(\"SET {name}={value}\")\n    }\n\n    fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {\n        let path = config.base_dir_with_default().join(\"cd.cmd\");\n        create_cd_file_at(&path).map_err(|source| {\n            anyhow::anyhow!(\n                \"Can't create cd.cmd file for use-on-cd at {}: {}\",\n                path.display(),\n                source\n            )\n        })?;\n        let path = path\n            .to_str()\n            .ok_or_else(|| anyhow::anyhow!(\"Can't read path to cd.cmd\"))?;\n        Ok(format!(\"doskey cd=\\\"{path}\\\" $*\"))\n    }\n}\n\nfn create_cd_file_at(path: &std::path::Path) -> std::io::Result<()> {\n    use std::io::Write;\n    let cmd_contents = include_bytes!(\"./cd.cmd\");\n    let mut file = std::fs::File::create(path)?;\n    file.write_all(cmd_contents)?;\n    Ok(())","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/Schniz/fnm/blob/86adc9676ceb2a509b21e75e74048b93c89f097d/src/shell/windows_cmd/mod.rs#L15-L51","documentation":"For `--use-on-cd`, the WindowsCmd shell writes an embedded `cd.cmd` helper into the fnm base directory (`config.base_dir_with_default().join(\"cd.cmd\")`) using `File::create` + `write_all`. Any io failure is wrapped into this anyhow error (a returned Err, not a panic), naming the target path and the underlying cause.","triggerScenarios":"`fnm env --shell cmd --use-on-cd` when the fnm base dir (FNM_DIR if set, else the OS data dir such as %APPDATA%\\fnm / ~/.local/share/fnm) is unwritable, missing, on a read-only volume, or when a locked/stale cd.cmd already exists there.","commonSituations":"Corporate machines with AppData redirection or write restrictions; FNM_DIR on a network share with deny-write ACLs; antivirus holding the freshly created cd.cmd; a directory named `cd.cmd` occupying the target path; full disk.","solutions":["Verify the base dir: `echo %FNM_DIR%` (or default location) then `icacls <dir>` / try `echo test > <dir>\\cd.cmd` to reproduce the permission error.","Create the directory if missing: `mkdir \"%APPDATA%\\fnm\"`.","Delete a stale/locked cd.cmd (close other cmd windows / let AV finish) and retry.","Point FNM_DIR at a writable local path, or drop `--use-on-cd` and use PowerShell's use-on-cd instead."],"exampleFix":":: before\nset FNM_DIR=\\\\server\\share\\fnm\nfnm env --shell cmd --use-on-cd   :: error: Can't create cd.cmd file ... (Access is denied)\n\n:: after\nset FNM_DIR=%LOCALAPPDATA%\\fnm\nmkdir \"%FNM_DIR%\"\nfnm env --shell cmd --use-on-cd   :: doskey cd=\"...\" emitted","handlingStrategy":"validation","validationCode":"// run before `fnm env --shell cmd --use-on-cd`\nlet base = std::env::var_os(\"FNM_DIR\")\n    .map(std::path::PathBuf::from)\n    .unwrap_or_else(default_fnm_dir);\nif !base.is_dir() { std::fs::create_dir_all(&base)?; }\nlet probe = base.join(\".write-probe\");\nstd::fs::write(&probe, b\"\")?;      // PermissionDenied surfaces here, not inside fnm\nstd::fs::remove_file(&probe)?;","typeGuard":null,"tryCatchPattern":"match cmd_shell.use_on_cd(&config) {\n    Ok(macro_line) => println!(\"{macro_line}\"),\n    Err(e) if e.chain().any(|c| c.downcast_ref::<std::io::Error>()\n        .is_some_and(|io| io.kind() == std::io::ErrorKind::PermissionDenied)) =>\n        eprintln!(\"base dir not writable: fix FNM_DIR permissions or delete locked cd.cmd\"),\n    Err(e) => return Err(e),\n}","preventionTips":["Pre-create the fnm base dir and verify write access during machine setup.","Keep FNM_DIR on a local writable volume, not a locked network share.","If AV locks cd.cmd, exclude the fnm dir or retry after the scan completes."],"tags":["rust","cmd","windows","file-io","use-on-cd","permissions"],"backgroundTag":"file-write-permission-denied","analyzedSha":"86adc9676ceb2a509b21e75e74048b93c89f097d","analyzedAt":"2026-08-16T21:43:05.725Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}