{"record":{"id":"14ebc6b4c5b45ba4","repo":"denoland/deno","slug":"failed-to-spawn-cwd-is-not-a-directory","errorCode":null,"errorMessage":"Failed to spawn '{}': cwd is not a directory '{}'","messagePattern":"Failed to spawn '(.+?)': cwd is not a directory '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ext/process/lib.rs","lineNumber":1135,"sourceCode":"        #[allow(clippy::disallowed_methods, reason = \"requires real fs\")]\n        if !cwd.exists() {\n          return Err(\n            std::io::Error::new(\n              std::io::ErrorKind::NotFound,\n              format!(\n                \"Failed to spawn '{}': No such cwd '{}'\",\n                command_name,\n                cwd.to_string_lossy()\n              ),\n            )\n            .into(),\n          );\n        }\n\n        #[allow(clippy::disallowed_methods, reason = \"requires real fs\")]\n        if !cwd.is_dir() {\n          return Err(\n            std::io::Error::new(\n              std::io::ErrorKind::NotFound,\n              format!(\n                \"Failed to spawn '{}': cwd is not a directory '{}'\",\n                command_name,\n                cwd.to_string_lossy()\n              ),\n            )\n            .into(),\n          );\n        }\n      }\n\n      return Err(ProcessError::SpawnFailed {\n        command: command.get_program().to_string_lossy().into_owned(),\n        error: Box::new(err.into()),\n      });\n    }\n  };","sourceCodeStart":1117,"sourceCodeEnd":1153,"githubUrl":"https://github.com/denoland/deno/blob/a961cdec3b1948844414ebeecc697dcad76df2d1/ext/process/lib.rs#L1117-L1153","documentation":"Companion check to the 'No such cwd' error in Deno's spawn wrapper: the path in cwd exists (the earlier exists() check passed) but is not a directory — typically a regular file. Spawn aborts with ErrorKind::NotFound and this message before attempting exec.","triggerScenarios":"Deno.Command/child_process.spawn with cwd set to a file path: pointing cwd at a script or archive instead of its directory, a path where a file shadows an expected directory name, or a symlink target that is a file.","commonSituations":"Passing __filename or a config file path where the containing directory was intended; a file created at the location a build step was supposed to make a directory; incorrect path joining (path.join vs path.dirname confusion).","solutions":["Wrap the intended directory: use path.dirname(filePath) instead of the file path itself.","statSync the path and require isDirectory() before spawning; fail with the path in the error.","Delete or rename the stray file if a directory is genuinely expected there and something else created it.","Add a startup assertion for configured cwd values so misconfiguration is caught at boot."],"exampleFix":"// before\nconst cwd = import.meta.filename; // a file, not a directory\nconst child = spawn(\"git\", [\"status\"], { cwd }); // fails: cwd is not a directory\n\n// after\nimport { dirname } from \"node:path\";\nconst cwd = dirname(fileURLToPath(import.meta.url));\nconst child = spawn(\"git\", [\"status\"], { cwd });","handlingStrategy":"validation","validationCode":"import { statSync } from \"node:fs\";\nif (statSync(cwd, { throwIfNoEntry: false })?.isDirectory() !== true) throw new Error(`cwd must be a directory: ${cwd}`);","typeGuard":"import { statSync } from \"node:fs\";\nconst isDirectoryPath = (p: string): p is `${string}/` => statSync(p, { throwIfNoEntry: false })?.isDirectory() ?? false;","tryCatchPattern":"try { spawn(prog, args, { cwd }); } catch (e) { if (/cwd is not a directory/.test(String(e))) throw new Error(`cwd points at a file — use path.dirname(): ${cwd}`); throw e; }","preventionTips":["When you mean 'next to this file', pass path.dirname(fileURLToPath(import.meta.url)), never the file path.","Run a startup check on every configured cwd: exists AND isDirectory.","Beware path.join(a, b) where b is a filename — that builds a file path, not a dir."],"tags":["process","spawn","cwd","filesystem","path-confusion"],"backgroundTag":"working-directory-not-found","analyzedSha":"a961cdec3b1948844414ebeecc697dcad76df2d1","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}