Skip to content

Commit 6d2fa96

Browse files
committed
fix: Normalize CMake paths from 'dir/..' to '../dir' for compatibility
CMake and some build tools prefer the canonical '../dir/file' form over 'dir/../file', even though both are technically valid relative paths. The pathdiff crate can generate 'dir/..' patterns when calculating relative paths. This commit normalizes such patterns to the cleaner '../dir' form for better tool compatibility. Example: - Before: cpp/../generated/file.cpp - After: ../cpp/generated/file.cpp Discovered while testing the RustBuffer fix on Android builds.
1 parent 2bfdedd commit 6d2fa96

File tree

1 file changed

+20
-1
lines changed
  • crates/ubrn_cli/src/codegen

1 file changed

+20
-1
lines changed

crates/ubrn_cli/src/codegen/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ pub(crate) trait RenderedFile: DynTemplate {
2626
let from = file
2727
.parent()
2828
.expect("Expected this file to have a directory");
29-
pathdiff::diff_utf8_paths(to, from).expect("Should be able to find a relative path")
29+
let path = pathdiff::diff_utf8_paths(to, from).expect("Should be able to find a relative path");
30+
31+
// Normalize paths like "dir/../file" to "../dir/file" for better CMake compatibility
32+
// This pattern can occur when calculating relative paths and is technically correct
33+
// but some tools (like CMake) prefer the cleaner "../dir/file" form
34+
let path_str = path.as_str();
35+
let components: Vec<&str> = path_str.split('/').collect();
36+
37+
// Check if we have pattern like ["dir", "..", rest...]
38+
if components.len() >= 2 && components[1] == ".." && !components[0].starts_with('.') {
39+
let dir = components[0];
40+
let rest = components[2..].join("/");
41+
if rest.is_empty() {
42+
Utf8PathBuf::from(format!("../{}", dir))
43+
} else {
44+
Utf8PathBuf::from(format!("../{}/{}", dir, rest))
45+
}
46+
} else {
47+
path
48+
}
3049
}
3150
fn filter_by(&self) -> bool {
3251
true

0 commit comments

Comments
 (0)