Rollup merge of #125205 - ChrisDenton:verbatim-include, r=jieyouxu
Fixup Windows verbatim paths when used with the `include!` macro On Windows, the following code can fail if the `OUT_DIR` environment variable is a [verbatim path](https://doc.rust-lang.org/std/path/enum.Prefix.html) (i.e. begins with `\\?\`): ```rust include!(concat!(env!("OUT_DIR"), "/src/repro.rs")); ``` This is because verbatim paths treat `/` literally, as if it were just another character in the file name. The good news is that the standard library already has code to fix this. We can simply use `components` to normalize the path so it works as intended.
This commit is contained in:
commit
4d378f26a9
@ -1,5 +1,6 @@
|
||||
use std::default::Default;
|
||||
use std::iter;
|
||||
use std::path::Component::Prefix;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
@ -1293,7 +1294,12 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
|
||||
base_path.push(path);
|
||||
Ok(base_path)
|
||||
} else {
|
||||
Ok(path)
|
||||
// This ensures that Windows verbatim paths are fixed if mixed path separators are used,
|
||||
// which can happen when `concat!` is used to join paths.
|
||||
match path.components().next() {
|
||||
Some(Prefix(prefix)) if prefix.kind().is_verbatim() => Ok(path.components().collect()),
|
||||
_ => Ok(path),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
1
tests/run-make/import-macro-verbatim/include/include.txt
Normal file
1
tests/run-make/import-macro-verbatim/include/include.txt
Normal file
@ -0,0 +1 @@
|
||||
static TEST: &str = "Hello World!";
|
8
tests/run-make/import-macro-verbatim/rmake.rs
Normal file
8
tests/run-make/import-macro-verbatim/rmake.rs
Normal file
@ -0,0 +1,8 @@
|
||||
//@ only-windows other platforms do not have Windows verbatim paths
|
||||
use run_make_support::rustc;
|
||||
fn main() {
|
||||
// Canonicalizing the path ensures that it's verbatim (i.e. starts with `\\?\`)
|
||||
let mut path = std::fs::canonicalize(file!()).unwrap();
|
||||
path.pop();
|
||||
rustc().input("verbatim.rs").env("VERBATIM_DIR", path).run();
|
||||
}
|
12
tests/run-make/import-macro-verbatim/verbatim.rs
Normal file
12
tests/run-make/import-macro-verbatim/verbatim.rs
Normal file
@ -0,0 +1,12 @@
|
||||
//! Include a file by concating the verbatim path using `/` instead of `\`
|
||||
|
||||
include!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
|
||||
fn main() {
|
||||
assert_eq!(TEST, "Hello World!");
|
||||
|
||||
let s = include_str!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
|
||||
assert_eq!(s, "static TEST: &str = \"Hello World!\";\n");
|
||||
|
||||
let b = include_bytes!(concat!(env!("VERBATIM_DIR"), "/include/include.txt"));
|
||||
assert_eq!(b, b"static TEST: &str = \"Hello World!\";\n");
|
||||
}
|
Loading…
Reference in New Issue
Block a user