#path #ci #xtask #check #repo #compile-time #repository

build repo_path

Access paths in your repository, with compile-time checks

3 stable releases

new 1.2.0 Mar 16, 2025
1.1.0 Jan 25, 2025
1.0.0 Jan 12, 2025

#160 in Build Utils

Download history 102/week @ 2025-01-08 82/week @ 2025-01-15 133/week @ 2025-01-22 107/week @ 2025-01-29 316/week @ 2025-02-05 120/week @ 2025-02-12 166/week @ 2025-02-19 215/week @ 2025-02-26 68/week @ 2025-03-05 195/week @ 2025-03-12

645 downloads per month
Used in 4 crates (via cicero_path)

Apache-2.0

9KB

repo_path

Access paths in your repository, with compile-time checks.
This is useful in particular when you're writing your CI code in Rust, e.g. via the cargo-xtask pattern.

use repo_path::repo_path;

let readme = repo_path!("README.md");
let src_dir = repo_path!("src/");
let repo_root = repo_path!(); //equivalent to repo_path!(".")

The macro checks whether the path exists and constructs a PathBuf containing an absolute path. If the path does not exist, the compiler will error during compilation:

use repo_path::repo_path;

let non_existent = repo_path!("non/existent/path.txt");

This allows you to move files around without having to worry that a reference silently breaks, similar to the way std::include_str!() works.

Custom base path

If you'd like to paths to be relative to a different directory in your repository, you can generate your own macro with custom_base_path_macro!():

use repo_path::custom_base_path_macro;

custom_base_path_macro!(src_path, "src/"); //generates the src_path!() macro
let lib_file = src_path!("lib.rs");

This is useful when you need to reference a particular sub-directory often or in case your Rust code is placed in a sub-directory of the repository.

How it works

During compilation, the macro looks for the .git directory to determine the root of your repository.
Then it joins the path you specified to the root (or your custom base path) and checks if the resulting path exists and lies within your repository.

The path of your repository root is cached during compilation, so that you can use repo_path!() many times without a significant compile-time burden.

Usage in a library

Because the macro fetches the path during compilation, you cannot use it in libraries that want to access your user's repository (it will return paths for your own repository).
For that purpose, use the repo_path_lib crate instead.

Changelog

See the changelog here: https://codeberg.org/trem/repo_path/src/branch/main/CHANGELOG.md

Dependencies

~205–630KB
~15K SLoC