{"record":{"id":"434b17049110bb9b","repo":"the-benchmarker/web-frameworks","slug":"failed-to-get-current-directory","errorCode":null,"errorMessage":"Failed to get current directory","messagePattern":"Failed to get current directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rust/reinhardt-web/src/config/settings.rs","lineNumber":77,"sourceCode":"///\n/// ```no_run\n/// use server::config::settings::get_settings;\n///\n/// let settings = get_settings();\n/// ```\n///\n/// # Panics\n///\n/// Panics if:\n/// - Settings files cannot be read\n/// - Settings cannot be deserialized\n/// - Required settings are missing\npub fn get_settings() -> ProjectSettings {\n    let profile_str = env::var(\"REINHARDT_ENV\").unwrap_or_else(|_| \"local\".to_string());\n    let profile = Profile::parse(&profile_str);\n\n    // Get the project root directory (parent of src/)\n    let base_dir = env::current_dir().expect(\"Failed to get current directory\");\n    let settings_dir = base_dir.join(\"settings\");\n\n    // Build settings by merging sources in priority order.\n    // `build_composed::<T>()` uses `MergeStrategy::Deep` by default, so a\n    // single key in `production.toml` overrides only that key — sibling\n    // entries inside the same nested table inherit from `base.toml`.\n    SettingsBuilder::new()\n        .profile(profile)\n        // Lowest priority: Default values\n        .add_source(DefaultSource::new())\n        // Medium priority: Base TOML file\n        .add_source(TomlFileSource::new(settings_dir.join(\"base.toml\")))\n        // Profile priority: Environment-specific TOML file\n        .add_source(TomlFileSource::new(\n            settings_dir.join(format!(\"{}.toml\", profile_str)),\n        ))\n        // Highest priority: explicit process environment overrides\n        .add_source(HighPriorityEnvSource::new().with_prefix(\"REINHARDT_\"))","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/the-benchmarker/web-frameworks/blob/3795a31d724e41cdb87b6e0d3ac941b7bfac3ea2/rust/reinhardt-web/src/config/settings.rs#L59-L95","documentation":"reinhardt-web's get_settings() reads REINHARDT_ENV and then calls std::env::current_dir() to locate the project's settings directory. If the operating system cannot determine the current working directory (process cwd deleted, or unavailable), the expect() panics with \"Failed to get current directory\".","triggerScenarios":"Calling get_settings() (e.g. from test_get_settings) while the process's working directory has been unlinked, or in a sandbox/runtime where cwd is not accessible; current_dir() returns Err and expect unwraps it into a panic.","commonSituations":"Tests run in a temp directory that a cleanup step deletes mid-run; a long-lived process whose cwd was removed by another job; container runtimes started with an invalid Workdir; cwd on a mounted volume that was unmounted.","solutions":["Ensure the process is started with a valid, existing working directory (check container Workdir, service WorkingDirectory, test runner cwd)","Do not delete or unmount the directory the process was started from while it is running","If you control the code, replace expect with graceful handling: fall back to a configured base dir or a settings path from an env var instead of relying on cwd","Pass the project root explicitly (e.g. REINHARDT_BASE_DIR env var) so settings lookup is independent of cwd"],"exampleFix":"// before\nlet base_dir = env::current_dir().expect(\"Failed to get current directory\");\n\n// after\nlet base_dir = env::var(\"REINHARDT_BASE_DIR\")\n    .map(PathBuf::from)\n    .or_else(|_| env::current_dir())\n    .expect(\"Failed to determine project base directory\");","handlingStrategy":"try-catch","validationCode":"// before calling get_settings(), verify the process cwd is usable\nuse std::env;\nmatch env::current_dir() {\n    Ok(dir) if dir.exists() => println!(\"cwd ok: {:?}\", dir),\n    _ => {\n        eprintln!(\"current working directory is invalid; start the process from an existing directory\");\n        std::process::exit(1);\n    }\n}","typeGuard":"fn cwd_is_valid() -> bool {\n    std::env::current_dir().map(|d| d.exists()).unwrap_or(false)\n}","tryCatchPattern":"// get_settings panics rather than returning Result, so catch the unwind if you embed it\nlet settings = std::panic::catch_unwind(reinhardt_web::config::settings::get_settings)\n    .unwrap_or_else(|_| {\n        eprintln!(\"settings unavailable: check working directory and REINHARDT_ENV\");\n        std::process::exit(1);\n    });","preventionTips":["Start services and tests from a directory that outlives the process; avoid deleting the cwd while running","Set container/service workdir explicitly to an existing path","Prefer passing the project root via an env var instead of relying on cwd","Add a startup health check that fails fast with a clear message when cwd is unusable"],"tags":["rust","config","cwd","panic"],"backgroundTag":"directory-not-found","analyzedSha":"3795a31d724e41cdb87b6e0d3ac941b7bfac3ea2","analyzedAt":"2026-09-15T02:39:22.409Z","contentChangedAt":"2026-09-15T02:39:22.409Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}