Crate normpath[][src]

Expand description

This crate provides methods to normalize paths in the recommended way for the operating system.

It was made to fix a recurring bug caused by using fs::canonicalize on Windows: #45067, #48249, #52440, #55812, #58613, #59107, #74327. Normalization is usually a better choice unless you specifically need a canonical path.

Using these replacement methods will usually fix those issues, but see their documentation for more information:


These features are optional and can be enabled or disabled in a “Cargo.toml” file.

Optional Features

  • print_bytes - Provides implementations of [print_bytes::ToBytes] for types defined by this crate.

  • serde - Provides implementations of [serde::Deserialize] and [serde::Serialize] for types defined by this crate.


If this crate has been useful for your project, let me know with a sponsorship! Sponsorships help me create and maintain my open source libraries, and they are always very appreciated.


use std::io;
use std::path::Path;

use normpath::BasePathBuf;
use normpath::PathExt;

fn find_target_dir(path: &Path) -> io::Result<Option<BasePathBuf>> {
    let mut path = path.normalize()?;
    while !path.ends_with("target") {
        match path.pop() {
            Ok(true) => continue,
            Ok(false) => {}
            Err(_) => {
                eprintln!("Some components could not be normalized.");
        return Ok(None);


The error types defined by this crate.


A path that has a prefix on Windows.


Additional methods added to Path.