Skip to content

Commit f37eef1

Browse files
RReverserathre0z
authored andcommitted
Fix is_dependency_code for Windows
The previous check relied on string comparisons, which are not cross-platfom since Windows uses `\` and Unix uses `/`, so on Windows I was always seeing dependencies printed as if they're 1st-party code and couldn't easily filter them out. Instead of comparing paths as strings, it's better to use `Path::starts_with` which automatically compares paths component-wise regardless of OS delimiter. I've also changed the 3rd-party dependency check to use CARGO_HOME which is automatically provided by Cargo at build time and allows to precisely check for both dependencies from registries (.../.cargo/registry/src) and git dependencies (.../.cargo/git/checkouts) in one go. I verified this fixes the issues I've been having with color-backtrace on my Windows project.
1 parent e34327a commit f37eef1

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/lib.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,17 @@ impl Frame {
296296
}
297297

298298
const FILE_PREFIXES: &[&str] = &[
299-
"/rustc/",
300-
"src/libstd/",
301-
"src/libpanic_unwind/",
302-
"src/libtest/",
299+
"/rustc",
300+
"src/libstd",
301+
"src/libpanic_unwind",
302+
"src/libtest",
303+
env!("CARGO_HOME"),
303304
];
304305

305306
// Inspect filename.
306-
if let Some(ref filename) = self.filename {
307-
let filename = filename.to_string_lossy();
308-
if FILE_PREFIXES.iter().any(|x| filename.starts_with(x))
309-
|| filename.contains("/.cargo/registry/src/")
310-
{
311-
return true;
312-
}
313-
}
314-
315-
false
307+
self.filename
308+
.as_deref()
309+
.is_some_and(|filename| FILE_PREFIXES.iter().any(|x| filename.starts_with(x)))
316310
}
317311

318312
/// Heuristically determine whether a frame is likely to be a post panic

0 commit comments

Comments
 (0)