Skip to content

Commit f9bf99b

Browse files
committed
Fix verbatim paths used with include!
When using `concat!` to join paths, the Unix path separator (`/`) is often used. This breaks on Windows if the base path is a verbatim path (i.e. starts with `\\?\`).
1 parent 8af67ba commit f9bf99b

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/rustc_expand/src/base.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,12 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
12521252
base_path.push(path);
12531253
Ok(base_path)
12541254
} else {
1255-
Ok(path)
1255+
// This ensures that Windows verbatim paths are fixed if mixed path separators are used,
1256+
// which can happen when `concat!` is used to join paths.
1257+
match path.components().next() {
1258+
Some(prefix) if prefix.is_verbatim() => Ok(path.components().collect()),
1259+
_ => Ok(path),
1260+
}
12561261
}
12571262
}
12581263

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static TEST: &str = "Hello World!";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ only-windows other platforms do not have Windows verbatim paths
2+
use run_make_support::rustc;
3+
fn main() {
4+
// Canonicalizing the path ensures that it's verbatim (i.e. starts with `\\?\`)
5+
let mut path = std::fs::canonicalize(file!()).unwrap();
6+
path.pop();
7+
rustc().input("verbatim.rs").env("VERBATIM_DIR", path).run();
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Include a file by concating the verbatim path using `/` instead of `\`
2+
3+
include!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
4+
fn main() {
5+
assert_eq!(TEST, "Hello World!");
6+
7+
let s = include_str!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
8+
assert_eq!(s, "static TEST: &str = \"Hello World!\";\n");
9+
10+
let b = include_bytes!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
11+
assert_eq!(b, b"static TEST: &str = \"Hello World!\";\n");
12+
}

0 commit comments

Comments
 (0)