#file-path #source-file #absolute #macro #abs #invoked

abs-file-macro

A macro that returns the absolute file path of the Rust source file in which it is invoked

3 releases

new 0.1.2 Mar 3, 2025
0.1.1 Mar 3, 2025
0.1.0 Mar 3, 2025

#552 in Rust patterns

Download history 520/week @ 2025-02-27

520 downloads per month

MIT license

7KB

abs-file-macro

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

A macro that returns the absolute file path of the Rust source file in which it is invoked.

This macro ensures that the correct absolute path is resolved, even when used within a Cargo workspace or a nested crate. It prevents issues with duplicated path segments by properly handling the crate root.

Install

cargo add abs-file-macro

Usage

Example

use abs_file_macro::abs_file;

let path = abs_file!();

assert!(
    std::fs::metadata(&path).is_ok(),
    "abs_file!() should point to a real file, but {:?} does not exist",
    path
);

Testing

To ensure correctness, test this macro in two ways:

  1. As part of the full workspace

    cargo test --workspace
    

    This verifies that the macro functions correctly across all workspace members.

  2. Within an individual workspace member

    cd test-workspace-1
    cargo test
    

    This helps catch cases where path resolution might differ based on the working directory.

License

MIT License (c) 2025 Jeremy Harris.

No runtime deps