Skip to content

Commit 25b58c0

Browse files
connernilsenmeta-codesync[bot]
authored andcommitted
Add better errors for BXL output
Summary: Ran into some issues where the wrong structure was getting parsed (something was a `PathBuf` instead of an `Option<PathBuf>`), resulting in Pyrefly refusing to parse massive output. To debug, I rewrote the error handler to print the output with some context and add a pointer to the line with an error. Reviewed By: grievejia Differential Revision: D87908234 fbshipit-source-id: 997b6f12e780cc9ad09096f1de1a3b874ce1c948
1 parent 2327b8e commit 25b58c0

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pyrefly_build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ license = "MIT"
1111
[dependencies]
1212
anyhow = "1.0.98"
1313
dupe = "0.9.1"
14+
itertools = "0.14.0"
1415
pyrefly_python = { path = "../pyrefly_python" }
1516
pyrefly_util = { path = "../pyrefly_util" }
1617
serde = { version = "1.0.219", features = ["derive", "rc"] }

crates/pyrefly_build/src/query/mod.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::process::Command;
1515

1616
use anyhow::Context as _;
1717
use dupe::Dupe as _;
18+
use itertools::Itertools as _;
1819
use pyrefly_python::module_name::ModuleName;
1920
use pyrefly_python::module_path::ModulePathBuf;
2021
use pyrefly_python::sys_info::SysInfo;
@@ -98,12 +99,39 @@ pub trait SourceDbQuerier: Send + Sync + fmt::Debug {
9899
));
99100
}
100101

101-
serde_json::from_slice(&result.stdout).with_context(|| {
102-
format!(
103-
"Failed to construct valid `TargetManifestDatabase` from querier result. Command run: `{}`",
104-
cmd.get_program().display(),
105-
)
106-
})
102+
match serde_json::from_slice(&result.stdout)
103+
.with_context(|| {
104+
format!(
105+
"Failed to construct valid `TargetManifestDatabase` from querier result. Command run: {} {}",
106+
cmd.get_program().display(),
107+
cmd.get_args().map(|a| a.to_string_lossy()).join(" "),
108+
)
109+
}) {
110+
Err(e) => {
111+
let Some(downcast) = e.downcast_ref::<serde_json::error::Error>() else {
112+
return Err(e);
113+
};
114+
let Ok(content) = String::from_utf8(result.stdout) else {
115+
return Err(e);
116+
};
117+
let lines = content.lines().collect::<Vec<_>>();
118+
let error_line = downcast.line();
119+
let start = std::cmp::max(0, error_line - 30);
120+
let end = std::cmp::min(lines.len() - 1, error_line + 20);
121+
let cont = std::cmp::min(error_line + 1, end);
122+
123+
let e = e.context(
124+
format!(
125+
"Context: ```\n{} # THIS LINE HAS A PROBLEM\n{}\n```",
126+
lines[start..=error_line].iter().join("\n"),
127+
lines[cont..=end].iter().join("\n"),
128+
)
129+
);
130+
131+
Err(e)
132+
},
133+
ok => ok,
134+
}
107135
}
108136

109137
fn construct_command(&self) -> Command;

pyrefly/lib/lsp/non_wasm/build_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn queue_source_db_rebuild_and_recheck(
8383
.filter(|(c, files)| match ConfigFile::requery_source_db(c, files) {
8484
Ok(reloaded) => reloaded,
8585
Err(error) => {
86-
info!("Error reloading source database for config: {error}");
86+
info!("Error reloading source database for config: {error:?}");
8787
false
8888
}
8989
})

0 commit comments

Comments
 (0)