Fix local C headers when rex runs inside their folder

The header's folder was an empty path there, so a bare -I swallowed the
'-' (stdin) argument and cc failed with 'no input files'. Pass the
header's absolute folder instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Your NameandClaude Opus 5.5 committed 2026-11-08 15:10:15 +01:00
1 parent a68c720755
commit 36eacc0011
2 files changed
+18 -1

No files matched your search

+5 -1
View File
@@ -193,7 +193,11 @@ fn load_uncached(header: &str, dir: &Path, link: Option<&str>) -> Result<Lib, St
// a header of the user's, next to the program
let local = dir.join(header);
if local.is_file() {
lib.cflags.push(format!("-I{}", local.parent().unwrap_or(dir).display()));
// absolute: a relative (or empty) folder breaks when cc runs elsewhere, and a bare `-I`
// would swallow the next argument
let local = local.canonicalize().unwrap_or(local);
let inc = local.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
lib.cflags.push(format!("-I{}", inc.display()));
let stem = local.with_extension("");
for ext in ["c", "cpp", "cc", "cxx"] {
let src = stem.with_extension(ext);
+13
View File
@@ -311,3 +311,16 @@ fn a_wrapper_file_can_have_the_headers_name() {
]);
assert_eq!(out, "shapes.Size{w: 2, h: 3} 6.0\n");
}
#[test]
fn a_local_header_from_inside_its_folder() {
// `rex run main.rex` in the folder itself: the header's folder is the empty path there
let dir = folder(&[
("consts.h", "#define ANSWER 42\nstatic inline int twice(int n) { return n * 2; }\n"),
("main.rex", "import \"consts.h\"\n\nfunc main() {\n print(consts.twice(consts.ANSWER))\n}\n"),
]);
let o = Command::new(env!("CARGO_BIN_EXE_rex")).args(["run", "main.rex"]).current_dir(&dir).output().unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
assert_eq!(String::from_utf8_lossy(&o.stdout), "84\n");
}