Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up Windows verbatim paths when used with include! macro #23

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use std::env;
use std::fs;
use std::path::Path;
use std::path::{Component, Path, PathBuf};
use syn::parse::{Error, Parse, ParseBuffer, ParseStream, Parser, Result};
use syn::punctuated::Punctuated;
use syn::token::{Brace, Bracket, Paren};
Expand Down Expand Up @@ -150,14 +150,25 @@ impl Expr {
}

fn fs_read(span: &dyn ToTokens, path: impl AsRef<Path>) -> Result<String> {
let path = path.as_ref();
let mut path = path.as_ref();
if path.is_relative() {
let name = span.to_token_stream().into_iter().next().unwrap();
return Err(Error::new_spanned(
span,
format!("a relative path is not supported here; use `{name}!(concat!(env!(\"CARGO_MANIFEST_DIR\"), ...))`"),
));
}

// Make Windows verbatim paths work even with mixed path separators, which
// can happen when a path is produced using `concat!`.
let path_buf: PathBuf;
if let Some(Component::Prefix(prefix)) = path.components().next() {
if prefix.kind().is_verbatim() {
path_buf = path.components().collect();
path = &path_buf;
}
}

match fs::read_to_string(path) {
Ok(content) => Ok(content),
Err(err) => Err(Error::new_spanned(
Expand Down