color-spantrace: print_source_if_avail: can't extract source if working dir is crate in workspace
Hi,
I'm debugging my PR for color_eyre at the moment. For some reason, the source code lines were not printed out.
The reason was, that I'm using a workspace and that tracing_core::metadata::Metadata contains a file path relative to the workspace, not my current working directory.
So I'm in "$workspace/color_eyre" and execute a test.
This line is then executed because filename is "color-spantrace/tests/color_schemes.rs" – which is relative to the workspace, not my current working directory:
// `filename` can't be found, because it's relative to the workspace
let file = match File::open(filename) {
Ok(file) => file,
// return without printing the source:
Err(ref e) if e.kind() == ErrorKind::NotFound => return Ok(()),
e @ Err(_) => e.unwrap(),
};
I believe this is only a problem for people who work on color_spantrace, and not for end-users who install color_spantrace via cargo (but I'm not 100% sure).
I'm reporting this in case there is an easy workaround (I can't see one), or in case this could lead to problems for end-users.
Feel free, to close this if you don't think this is a problem.
Maybe a note in the README for people who want to work on color_spantrace would make sense?
Basically, normally you'd get the following:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SPANTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
0: styles::get_error with msg="test"
at tests/styles.rs:13
11 │
12 │ #[rustfmt::skip]
13 > #[tracing::instrument]
14 │ fn get_error(msg: &'static str) -> Report {
15 │
Because of the bug I got:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SPANTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
0: styles::get_error with msg="test"
at tests/styles.rs:13
Aah yea, I have a PR that attempts to fix this but I never figured out the proper way to resolve the relative paths we should be searching in and ended up forgetting about the PR, I should really try to finish that one...
I think Cargo should offer the root directory of the current base directory (the workspace or the standalone dir) as an environment variable, but it seems this doesn't exist.
Maybe OUT_DIR or CARGO_MANIFEST_DIR could be used to derive the right directory?