Skip to content

Commit 20bf67f

Browse files
authored
Merge pull request #23 from dtolnay/verbatim
Fix up Windows verbatim paths when used with `include!` macro
2 parents e30b8e7 + 17e4b14 commit 20bf67f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use proc_macro2::TokenStream;
7373
use quote::{quote, ToTokens};
7474
use std::env;
7575
use std::fs;
76-
use std::path::Path;
76+
use std::path::{Component, Path, PathBuf};
7777
use syn::parse::{Error, Parse, ParseBuffer, ParseStream, Parser, Result};
7878
use syn::punctuated::Punctuated;
7979
use syn::token::{Brace, Bracket, Paren};
@@ -150,14 +150,25 @@ impl Expr {
150150
}
151151

152152
fn fs_read(span: &dyn ToTokens, path: impl AsRef<Path>) -> Result<String> {
153-
let path = path.as_ref();
153+
let mut path = path.as_ref();
154154
if path.is_relative() {
155155
let name = span.to_token_stream().into_iter().next().unwrap();
156156
return Err(Error::new_spanned(
157157
span,
158158
format!("a relative path is not supported here; use `{name}!(concat!(env!(\"CARGO_MANIFEST_DIR\"), ...))`"),
159159
));
160160
}
161+
162+
// Make Windows verbatim paths work even with mixed path separators, which
163+
// can happen when a path is produced using `concat!`.
164+
let path_buf: PathBuf;
165+
if let Some(Component::Prefix(prefix)) = path.components().next() {
166+
if prefix.kind().is_verbatim() {
167+
path_buf = path.components().collect();
168+
path = &path_buf;
169+
}
170+
}
171+
161172
match fs::read_to_string(path) {
162173
Ok(content) => Ok(content),
163174
Err(err) => Err(Error::new_spanned(

0 commit comments

Comments
 (0)