From 2167ee28fdd4fcd970dadfbc9ed6a24bc46510ea Mon Sep 17 00:00:00 2001 From: javalsai Date: Tue, 24 Mar 2026 22:20:51 +0100 Subject: [PATCH] fix: make path resolution relative to project root this was already how it mostly behaved but there seemed to be exceptions like LSP warnings (in these tests themselves) --- const-macros-proc/src/lib.rs | 7 +++++-- src/lib.rs | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/const-macros-proc/src/lib.rs b/const-macros-proc/src/lib.rs index daed5ac..76affa7 100644 --- a/const-macros-proc/src/lib.rs +++ b/const-macros-proc/src/lib.rs @@ -1,5 +1,5 @@ #![feature(proc_macro_value)] -use std::{fs::File, io::Read}; +use std::{fs::File, io::Read, path::Path}; use proc_macro::{Delimiter, Group, Literal, Punct, TokenStream, TokenTree}; use sha2::Digest; @@ -114,7 +114,10 @@ fn get_sha256sum(buf: &[u8]) -> [u8; 32] { pub fn include_asset(input: TokenStream) -> TokenStream { let path = read_path_literal(input); let mut buf = Vec::new(); - File::open(path) + + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR to be set"); + + File::open(Path::new(&manifest_dir).join(&path)) .expect("couldn't open file") .read_to_end(&mut buf) .expect("error reading file"); diff --git a/src/lib.rs b/src/lib.rs index a118a48..b3df8a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,10 @@ pub mod file { } } + /// Similar to [`include_bytes!`], but it returns a lot more information about the file, like + /// its mime type, or its sha256sum (could be changed), all at compile time. + /// + /// The path is relative to the package's root. #[macro_export] macro_rules! include_asset { ($path:literal) => {