Craft Anything: understand how any software works, and craft your own.
Rust
Say where a program is when it is there but can't start, instead of "isn't installed" (#583)
Fábio Soares78e1f7dce0e4
diff --git a/crates/platform/src/process.rs b/crates/platform/src/process.rsindex e0630fd5..8eb6dd70 100644--- a/crates/platform/src/process.rs+++ b/crates/platform/src/process.rs@@ -368,7 +368,30 @@ fn use_search_path(cmd: &mut std::process::Command) {}fn spawn_error(name: &str, program: &OsStr, e: std::io::Error, remediation: Option<&str>) -> ToolError {- match e.kind() {+ let found = crate::which::which_in(&program.to_string_lossy(), crate::which::search_path()).into_iter().next();+ spawn_error_for(name, program, e, remediation, found)+}++/// `found` is where the program was looked up. "Not found" from the system for a file that is+/// there means something it needs is missing (the interpreter of an npm script, #152, #95): saying+/// the program isn't installed sent people to reinstall what they had.+fn spawn_error_for(name: &str, program: &OsStr, e: std::io::Error, remediation: Option<&str>, found: Option<std::path::PathBuf>) -> ToolError {+ match (e.kind(), found) {+ (std::io::ErrorKind::NotFound, Some(path)) => ToolError::new(+ ErrorCode::ProgramFailed,+ format!("{name} is at {} but could not be started: {e}.", path.display()),+ )+ .with_remediation(format!(+ "The file is there, so something it needs to run is missing. Programs installed with npm need Node.js on the PATH: run `{}` in a terminal to see what it says.",+ program.to_string_lossy()+ ))+ .with_details(serde_json::json!({"program": program.to_string_lossy(), "reason": "found_but_not_started", "path": path.to_string_lossy()})),+ (kind, _) => spawn_error_kind(name, program, e, kind, remediation),+ }+}++fn spawn_error_kind(name: &str, program: &OsStr, e: std::io::Error, kind: std::io::ErrorKind, remediation: Option<&str>) -> ToolError {+ match kind {std::io::ErrorKind::NotFound => ToolError::new(ErrorCode::EngineMissing, format!("{name} isn't installed or isn't on your PATH.")).with_remediation(remediation.map(str::to_string).unwrap_or_else(|| format!("Install {name}, or set its full path in Settings."))).with_details(serde_json::json!({"program": program.to_string_lossy(), "reason": "not_found"})),@@ -1272,6 +1295,30 @@ mod tests {use super::*;use std::sync::atomic::AtomicUsize;+ /// #152, #95: a program that is there but can't start (an npm script whose `node` is missing)+ /// said "isn't installed or isn't on your PATH". It names the file and what to look at; a+ /// program that really isn't there still says so.+ #[cfg(unix)]+ #[test]+ fn a_program_that_is_there_but_cannot_start_is_not_called_missing() {+ use std::os::unix::fs::PermissionsExt as _;+ let dir = tempfile::tempdir().unwrap();+ let script = dir.path().join("opencode");+ std::fs::write(&script, "#!/nonexistent/interpreter\n").unwrap();+ std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();+ let err = Process::new(script.as_os_str()).arg("--version").run().unwrap_err();+ assert_eq!(err.code, ErrorCode::ProgramFailed);+ assert!(err.message.contains("is at") && err.message.contains("could not be started"), "{}", err.message);+ assert!(!err.message.contains("isn't installed"), "{}", err.message);+ assert!(err.remediation.unwrap_or_default().contains("Node.js"));+ assert_eq!(err.details.as_ref().and_then(|d| d["reason"].as_str()), Some("found_but_not_started"));++ let err = Process::new("craft-anything-no-such-program").run().unwrap_err();+ assert_eq!(err.code, ErrorCode::EngineMissing);+ assert!(err.message.contains("isn't installed or isn't on your PATH"), "{}", err.message);+ assert_eq!(err.details.as_ref().and_then(|d| d["reason"].as_str()), Some("not_found"));+ }+#[test]fn captures_output_and_exit_code() {let out = Process::new("sh").args(["-c", "echo hello; echo oops >&2; exit 3"]).run().unwrap();diff --git a/crates/platform/src/which.rs b/crates/platform/src/which.rsindex 9897b2e5..578cfdd3 100644--- a/crates/platform/src/which.rs+++ b/crates/platform/src/which.rs@@ -51,8 +51,10 @@ pub fn search_path() -> Option<OsString> {#[cfg(windows)]pub(crate) fn launch_target(program: &std::ffi::OsStr) -> (OsString, Vec<OsString>) {let Some(found) = which_in(&program.to_string_lossy(), search_path()).into_iter().next() else { return (program.to_owned(), Vec::new()) };+ // Started by the path it was found at, not by name: a child with a cleared environment, or a+ // lookup that differs from ours, would say "not found" for a program that is there (#152).if !found.extension().is_some_and(|e| e.eq_ignore_ascii_case("cmd") || e.eq_ignore_ascii_case("bat")) {- return (program.to_owned(), Vec::new());+ return (found.into_os_string(), Vec::new());}let dir = found.parent().map(Path::to_path_buf).unwrap_or_default();let shim = std::fs::metadata(&found)@@ -378,8 +380,8 @@ mod tests {std::fs::write(dir.path().join("node.exe"), "").unwrap();// Found without its extension too, as a bare name is.assert_eq!(launch_target(dir.path().join("gemini").as_os_str()), (dir.path().join("node.exe").into_os_string(), vec![script.into_os_string()]));- // An .exe is left to the system's own lookup.- assert_eq!(launch_target(dir.path().join("node").as_os_str()), (dir.path().join("node").into_os_string(), Vec::new()));+ // An .exe is started by the path it was found at.+ assert_eq!(launch_target(dir.path().join("node").as_os_str()), (dir.path().join("node.exe").into_os_string(), Vec::new()));}/// A prompt with line breaks and quotes reaches the agent whole (needs Node; skipped without).diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 8c32632d..002678ae 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -121,6 +121,11 @@ cheapest help we can give anyone, so nothing below should hold it up for long.- [ ] Sometimes starting the app shows nothing: after the release, see from `desktop.log` whether itstill happens. DX12-only may have ended it; a GOG Galaxy Vulkan overlay layer was seen loadinginto the app+- [x] A program that is found but can't start (an npm script whose `node` is missing) says where it is and+ what to look at, not "isn't installed" (#152, #95); on Windows a program found is started by its+ path, not its name+- [ ] Live-check #152 on Windows 11 with OpenCode from npm, and on a machine with the agent but no+ Node.js- [ ] Add to `docs/releasing.md`: before publishing, a Deep build through Claude Code from thefinished package, on a Mac and on Windows. Starting the CLI from the package (#91) doesn'tshow whether Claude Code accepts its toolsdiff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex 0cc26862..0ae6c5e7 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -182,6 +182,12 @@ with the system (FXC), and keep the environment variable (`WGPU_DX12_COMPILER`)another. A window that can't start now says how to draw without the graphics card(`CRAFT_ANYTHING_RENDERER=software`). Not run on the reporter's machine or on Windows here.+**"Not installed" for a program that was there.** The system answers "not found" both for a missing+program and for a file whose interpreter is missing (an npm script with no `node`), and we turned+every one into "isn't installed or isn't on your PATH": #95, #130 and #152 reinstalled what they+had. When a spawn says not found, look the program up yourself; if it's there, name the file and+what it needs (`spawn_error_for`). Not run on Windows here.+**A Mac app that couldn't find Claude Code, and offered a GitHub key instead.** An app opened fromthe Dock gets the system's short `PATH`, not the shell's, so `claude` in `~/.local/bin` wasn'tfound; "Built by" then fell back to "Claude Code on github.com-work", an `~/.ssh/config` alias