#resolve #path


Easily resolve tilde paths and relative paths

1 unstable release

0.1.0 Mar 7, 2022

#856 in Filesystem

Download history 1716/week @ 2023-08-08 2595/week @ 2023-08-15 1756/week @ 2023-08-22 1933/week @ 2023-08-29 4041/week @ 2023-09-05 1338/week @ 2023-09-12 1564/week @ 2023-09-19 1856/week @ 2023-09-26 1836/week @ 2023-10-03 1759/week @ 2023-10-10 6995/week @ 2023-10-17 3135/week @ 2023-10-24 2268/week @ 2023-10-31 1679/week @ 2023-11-07 3248/week @ 2023-11-14 2587/week @ 2023-11-21

10,296 downloads per month
Used in 21 crates (5 directly)


199 lines


A crate for resolving relative (./) and tilde paths (~/) in Rust.

Note that this does not perform path canonicalization, i.e. it will not eliminate segments like .. or ./././ in a path. This crate is intended simply to anchor relative paths such that they have an absolute path from the root.


Rust has Path and PathBuf in the standard library for working with file paths, but unfortunately there is no easy and ergonomic way to resolve relative paths in the following ways:

  • with respect to the process current-working-directory (CWD)
  • with respect to the active user's home directory (~/)
  • with respect to a user-provided absolute path


This crate provides an extension trait PathResolveExt with extension methods for path-like types. The following methods are provided:

resolve and try_resolve

These methods will resolve relative paths (./...) with respect to the process current-working-directory, and will also resolve tilde-paths (~/...) to the active user's home directory.

Assuming a home directory of /home/user and a CWD of /home/user/Documents, the resolve methods will evaluate in the following ways:

use std::path::Path;
use resolve_path::PathResolveExt;

// Direct variant (may panic)
assert_eq!("~/.vimrc".resolve(), Path::new("/home/user/.vimrc"));
assert_eq!("./notes.txt".resolve(), Path::new("/home/user/Documents/notes.txt"));

// Try variant (returns Result)
assert_eq!("~/.vimrc".try_resolve().unwrap(), Path::new("/home/user/.vimrc"));
assert_eq!("./notes.txt".try_resolve().unwrap(), Path::new("/home/user/Documents/notes.txt"));

resolve_in and try_resolve_in

These methods will resolve tilde-paths (~/...) in the normal way, but will resolve relative paths (./...) with respect to a provided base directory. This can be very useful, for example when evaluating paths given in a config file with respect to the location of the config file, rather than with respect to the process CWD.

Assuming the same home directory of /home/user and CWD of /home/user/Documents, the resolve_in methods will evaluate in the following ways:

use std::path::Path;
use resolve_path::PathResolveExt;

// Direct variant (may panic)
assert_eq!("~/.vimrc".resolve_in("~/.config/alacritty/"), Path::new("/home/user/.vimrc"));
assert_eq!("./alacritty.yml".resolve_in("~/.config/alacritty/"), Path::new("/home/user/.config/alacritty/alacritty.yml"));

// Try variant (returns Result)
assert_eq!("~/.vimrc".try_resolve_in("~/.config/alacritty/").unwrap(), Path::new("/home/user/.vimrc"));
assert_eq!("./alacritty.yml".try_resolve_in("~/.config/alacritty/").unwrap(), Path::new("/home/user/.config/alacritty/alacritty.yml"));

Why use Cow<Path>?

If any of the PathResolveExt methods are called on a path that does not actually need to be resolved (i.e. a path that is already absolute), then the resolver methods will simply return Cow::Borrowed(&Path) with the original path ref within. If resolution does occur, then the path will one way or another be edited (e.g. by adding an absolute path prefix), and will be returned as a Cow::Owned(PathBuf). This way we can avoid allocation where it is unnecessary.