rust-lang/mdBook · error

Unable to determine the current directory

Error message

Unable to determine the current directory

What it means

mdbook's path-resolution helper resolves the serve/open path against the current working directory using `env::current_dir().expect("Unable to determine the current directory")`. This panics when the OS cannot report the cwd — most commonly because the directory has been deleted or is no longer accessible to the process.

Source

Thrown at src/main.rs:135

    tracing_subscriber::fmt()
        .without_time()
        .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr()))
        .with_writer(std::io::stderr)
        .with_env_filter(filter)
        .with_target(with_target)
        .init();
}

fn get_book_dir(args: &ArgMatches) -> PathBuf {
    if let Some(p) = args.get_one::<PathBuf>("dir") {
        // Check if path is relative from current dir, or absolute...
        if p.is_relative() {
            env::current_dir().unwrap().join(p)
        } else {
            p.to_path_buf()
        }
    } else {
        env::current_dir().expect("Unable to determine the current directory")
    }
}

fn open<P: AsRef<OsStr>>(path: P) {
    info!("Opening web browser");
    if let Err(e) = opener::open(path) {
        error!("Error opening web browser: {}", e);
    }
}

#[test]
fn verify_app() {
    create_clap_command().debug_assert();
}

View on GitHub (pinned to dc21064fc2)

Solutions

  1. `cd` to an existing directory (e.g. the book root) before running mdbook.
  2. Recreate the deleted directory or restart the shell so it re-resolves the cwd.
  3. Pass an explicit `--dest-dir`/path argument so the helper takes the non-cwd branch.
  4. Check filesystem mount/permissions of the working directory (df, ls) if it appears to exist.

Example fix

# before (shell sitting in a deleted dir)
$ mdbook serve   # panics: Unable to determine the current directory
# after
$ cd /path/to/book && mdbook serve
Defensive patterns

Strategy: validation

Validate before calling

use std::env;
match env::current_dir() {
    Ok(cwd) if cwd.exists() => { /* safe to proceed */ }
    _ => {
        eprintln!("current directory unavailable; cd to a valid directory first");
        std::process::exit(1);
    }
}

Try / catch

let cwd = env::current_dir().unwrap_or_else(|e| {
    eprintln!("Unable to determine the current directory: {e}");
    std::process::exit(1);
});

Prevention

When it happens

Trigger: Calling `mdbook serve` (or the path-normalizing helper) with no path argument while the process's current working directory has been removed (e.g. shell in a deleted directory), permission loss, or cwd on an unmounted volume.

Common situations: Developer deletes the directory they launched mdbook from; running inside a container where WORKDIR was removed; running mdbook from a temp dir that a script cleaned up.

Related errors


AI-assisted analysis of rust-lang/mdBook@dc21064fc2 (2026-09-01). Data as JSON: /api/errors/2cec095e2af12c77. Report an issue: GitHub.