{"record":{"id":"6cbde88420410e7c","repo":"coding-horror/basic-computer-games","slug":"invalid-file-path","errorCode":null,"errorMessage":"Invalid file path!","messagePattern":"Invalid file path!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"81_Splat/rust/src/stats.rs","lineNumber":43,"sourceCode":"                let stats: Vec<&str> = stats.trim().split(\",\").collect();\n\n                for s in stats {\n                    if s.is_empty() {\n                        continue;\n                    }\n\n                    let s = s.parse::<f32>().expect(\"Corrupt stats file!\");\n                    altitudes.push(s);\n                }\n\n                return Some(Stats { altitudes });\n            } else {\n                println!(\"PREVIOUS GAME DATA NOT FOUND!\");\n\n                if !utility::prompt_bool(\"WOULD YOU LIKE TO CREATE ONE?\", false) {\n                    return None;\n                } else {\n                    let mut file = File::create(path).expect(\"Invalid file path!\");\n                    file.write_all(\"\".as_bytes())\n                        .expect(\"Could not create file!\");\n\n                    return Some(Stats {\n                        altitudes: Vec::new(),\n                    });\n                }\n            }\n        }\n\n        println!(\"\\nRESULTS OF THIS SESSION WILL NOT BE SAVED.\");\n        None\n    }\n\n    pub fn add_altitude(&mut self, a: f32) {\n        let all_jumps = self.altitudes.len() + 1;\n        let mut placement = all_jumps;\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/81_Splat/rust/src/stats.rs#L25-L61","documentation":"This panic is triggered by `.expect(\"Invalid file path!\")` on `File::create(path)` in Splat's `Stats::new()` (line 43), where `path` is the hardcoded string `\"src/stats.txt\"`. `File::create` returns `Err` for many reasons beyond an invalid path — missing parent directory, insufficient permissions, read-only filesystem, or disk full. The message implies the path string is wrong, which is only one possible cause.","triggerScenarios":"The `src/` directory does not exist in the current working directory (e.g., the binary is run from a different working directory than the project root). The user lacks write permissions. The filesystem is read-only. The path exists as a directory rather than a file.","commonSituations":"Running the compiled binary from `target/release/` or any directory other than the project root — the relative path `src/stats.txt` resolves to a non-existent `src/` parent. Deploying the binary without the `src/` directory. Running on a read-only filesystem (container, Live CD). Permission denied in shared hosting.","solutions":["Use an absolute path or a platform-appropriate config/data directory (e.g., `dirs::data_dir()`) instead of the hardcoded relative `src/stats.txt`.","Create the parent directory with `std::fs::create_dir_all` before `File::create`.","Replace `.expect` with proper error handling that reports the actual `io::Error` to the user.","Run the binary from the project root or provide a `--data-dir` CLI flag."],"exampleFix":"// before\nlet path = \"src/stats.txt\";\nlet mut file = File::create(path).expect(\"Invalid file path!\");\n\n// after\nlet path = \"src/stats.txt\";\nif let Some(parent) = std::path::Path::new(path).parent() {\n    let _ = std::fs::create_dir_all(parent);\n}\nlet mut file = match File::create(path) {\n    Ok(f) => f,\n    Err(e) => { eprintln!(\"Cannot create stats file: {}\", e); return None; }\n};","handlingStrategy":"validation","validationCode":"// Validate the path and parent directory before File::create\nuse std::path::Path;\nlet path = Path::new(\"src/stats.txt\");\nif let Some(parent) = path.parent() {\n    std::fs::create_dir_all(parent).ok();\n}\nassert!(path.parent().map(|p| p.exists()).unwrap_or(true),\n    \"Parent directory does not exist\");","typeGuard":"fn can_create_file(path: &str) -> bool {\n    let p = std::path::Path::new(path);\n    p.parent().map(|dir| dir.exists() || std::fs::create_dir_all(dir).is_ok())\n        .unwrap_or(true)\n}","tryCatchPattern":"let mut file = match File::create(path) {\n    Ok(f) => f,\n    Err(e) => {\n        eprintln!(\"Cannot create stats file '{}': {}\", path, e);\n        return None;\n    }\n};","preventionTips":["Use absolute or platform-appropriate data paths (dirs crate) instead of relative paths to source dirs.","Create parent directories with create_dir_all before File::create.","Report the actual io::Error to the user rather than a fixed misleading message.","Avoid hardcoding paths relative to the project source tree in shipped binaries."],"tags":["rust","file-io","panic","expect","relative-path","working-directory","misleading-message","splat"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}