Skip to content

Commit

Permalink
Use track_caller to make debugging bindgen build script errors ea…
Browse files Browse the repository at this point in the history
…sier (#3383)
  • Loading branch information
kennykerr authored Dec 11, 2024
1 parent c315e59 commit 273dced
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
32 changes: 23 additions & 9 deletions crates/libs/bindgen/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
use std::io::BufRead;

#[track_caller]
pub fn read_file_lines(path: &str) -> Vec<String> {
let file = std::io::BufReader::new(
std::fs::File::open(path).unwrap_or_else(|_| panic!("failed to open file `{path}`")),
);
let Ok(file) = std::fs::File::open(path) else {
panic!("failed to open file `{path}`")
};

file.lines()
.map(|line| line.unwrap_or_else(|_| panic!("failed to read file lines `{path}`")))
.collect()
let file = std::io::BufReader::new(file);
let mut lines = vec![];

for line in file.lines() {
let Ok(line) = line else {
panic!("failed to read file lines `{path}`");
};

lines.push(line);
}

lines
}

#[track_caller]
pub fn write_to_file<C: AsRef<[u8]>>(path: &str, contents: C) {
if let Some(parent) = std::path::Path::new(path).parent() {
std::fs::create_dir_all(parent)
.unwrap_or_else(|_| panic!("failed to create directory `{path}`"));
if std::fs::create_dir_all(parent).is_err() {
panic!("failed to create directory `{path}`");
}
}

std::fs::write(path, contents).unwrap_or_else(|_| panic!("failed to write file `{path}`"));
if std::fs::write(path, contents).is_err() {
panic!("failed to write file `{path}`");
}
}
20 changes: 15 additions & 5 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,19 @@ enum ArgKind {
Derive,
}

#[track_caller]
fn expand_args<I, S>(args: I) -> Vec<String>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
// This function is needed to avoid a recursion limit in the Rust compiler.
#[track_caller]
fn from_string(result: &mut Vec<String>, value: &str) {
expand_args(result, value.split_whitespace().map(|arg| arg.to_string()))
}

#[track_caller]
fn expand_args<I, S>(result: &mut Vec<String>, args: I)
where
I: IntoIterator<Item = S>,
Expand Down Expand Up @@ -241,7 +244,9 @@ where
result
}

#[track_caller]
fn expand_input(input: &[&str]) -> Vec<File> {
#[track_caller]
fn expand_input(result: &mut Vec<String>, input: &str) {
let path = std::path::Path::new(input);

Expand Down Expand Up @@ -295,12 +300,17 @@ fn expand_input(input: &[&str]) -> Vec<File> {
.collect();
}

input.extend(paths.iter().map(|path| {
let bytes =
std::fs::read(path).unwrap_or_else(|_| panic!("failed to read binary file `{path}`"));
for path in &paths {
let Ok(bytes) = std::fs::read(path) else {
panic!("failed to read binary file `{path}`");
};

File::new(bytes).unwrap_or_else(|| panic!("failed to read .winmd format `{path}`"))
}));
let Some(file) = File::new(bytes) else {
panic!("failed to read .winmd format `{path}`");
};

input.push(file);
}

input
}
Expand Down
2 changes: 2 additions & 0 deletions crates/libs/bindgen/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Writer {
clone
}

#[track_caller]
pub fn write(&self, tree: TypeTree) {
if self.config.package {
self.write_package(&tree);
Expand All @@ -28,6 +29,7 @@ impl Writer {
}
}

#[track_caller]
fn write_file(&self, tree: TypeTree) {
let tokens = if self.config.flat {
self.write_flat(tree)
Expand Down

0 comments on commit 273dced

Please sign in to comment.