From a87e0df43ecab9c476fed8a2f0e28038168681f4 Mon Sep 17 00:00:00 2001 From: Conner Nilsen Date: Tue, 10 Feb 2026 12:31:28 -0800 Subject: [PATCH 1/2] Add logging when new sourcedb is loaded Reviewed By: kinto0 Differential Revision: D92773270 --- crates/pyrefly_build/src/lib.rs | 17 +++++++++++++++++ crates/pyrefly_build/src/query/buck.rs | 11 +++++++++++ crates/pyrefly_build/src/query/custom.rs | 11 +++++++++++ test/basic.md | 1 + 4 files changed, 40 insertions(+) diff --git a/crates/pyrefly_build/src/lib.rs b/crates/pyrefly_build/src/lib.rs index 06451be8b3..41f1cbca82 100644 --- a/crates/pyrefly_build/src/lib.rs +++ b/crates/pyrefly_build/src/lib.rs @@ -25,6 +25,7 @@ #![feature(const_type_name)] #![feature(if_let_guard)] +use std::fmt::Display; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; @@ -42,6 +43,7 @@ pub mod source_db; pub use source_db::SourceDatabase; use starlark_map::small_map::SmallMap; mod query; +use tracing::info; #[cfg(not(target_arch = "wasm32"))] use which::which; @@ -92,6 +94,15 @@ impl BuildSystemArgs { } } +impl Display for BuildSystemArgs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Buck(args) => write!(f, "Buck({})", args), + Self::Custom(args) => write!(f, "Custom({})", args), + } + } +} + #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] pub struct BuildSystem { @@ -147,6 +158,12 @@ impl BuildSystem { return Some(Ok(result.dupe())); } + info!( + "Loading new build system at {}: {}", + config_root.display(), + &self.args + ); + for path in &mut self.search_path_prefix { *path = config_root.join(&path); } diff --git a/crates/pyrefly_build/src/query/buck.rs b/crates/pyrefly_build/src/query/buck.rs index f7dd53a19c..b99f1db702 100644 --- a/crates/pyrefly_build/src/query/buck.rs +++ b/crates/pyrefly_build/src/query/buck.rs @@ -6,6 +6,7 @@ */ use std::fmt::Debug; +use std::fmt::Display; use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -77,6 +78,16 @@ impl BxlArgs { } } +impl Display for BxlArgs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "isolation_dir: {:?}, extras: {:?}", + self.isolation_dir, self.extras + ) + } +} + #[derive(Debug)] pub struct BxlQuerier(BxlArgs); diff --git a/crates/pyrefly_build/src/query/custom.rs b/crates/pyrefly_build/src/query/custom.rs index cf851cbc42..d8bd4b98eb 100644 --- a/crates/pyrefly_build/src/query/custom.rs +++ b/crates/pyrefly_build/src/query/custom.rs @@ -6,6 +6,7 @@ */ use std::fmt::Debug; +use std::fmt::Display; use std::path::Path; use std::path::PathBuf; use std::process::Command; @@ -45,6 +46,16 @@ impl CustomQueryArgs { } } +impl Display for CustomQueryArgs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "command: {:?}, repo_root: {:?}", + self.command, self.repo_root + ) + } +} + /// A querier allowing for a custom command when querying and constructing source DB. #[derive(Debug)] pub struct CustomQuerier(CustomQueryArgs); diff --git a/test/basic.md b/test/basic.md index ff3c61a724..ef077799ff 100644 --- a/test/basic.md +++ b/test/basic.md @@ -20,6 +20,7 @@ $ echo -e "from typing import reveal_type\nreveal_type(1)" > $TMPDIR/empty.py && ```scrut {output_stream: stderr} $ $PYREFLY check $TEST_PY + INFO Loading new build system at * (glob?) INFO Querying Buck for source DB (glob?) INFO Source DB build ID: * (glob?) INFO Finished querying Buck for source DB (glob?) From b164a205939855c3b2dd6e2456cbfe49676492fc Mon Sep 17 00:00:00 2001 From: Conner Nilsen Date: Tue, 10 Feb 2026 12:31:28 -0800 Subject: [PATCH 2/2] Update search path prefix when getting source db Summary: This should fix the sourcedb bug Jia mentioned last week, where sometimes search path prefix entries aren't used correctly. This happens when there's already a sourcedb entry loaded, so we skip appending the config root to the search path as expected. Reviewed By: kinto0 Differential Revision: D92848242 --- crates/pyrefly_build/src/lib.rs | 87 ++++++++++++++++++++++-- crates/pyrefly_build/src/query/custom.rs | 2 +- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/crates/pyrefly_build/src/lib.rs b/crates/pyrefly_build/src/lib.rs index 41f1cbca82..c8daed96f1 100644 --- a/crates/pyrefly_build/src/lib.rs +++ b/crates/pyrefly_build/src/lib.rs @@ -150,6 +150,11 @@ impl BuildSystem { Err(e) => return Some(Err(e)), Ok(path) => path, }; + + for path in &mut self.search_path_prefix { + *path = config_root.join(&path); + } + let mut cache = BUILD_SYSTEM_CACHE.lock(); let key = (repo_root.clone(), self.args.clone()); if let Some(maybe_result) = cache.get(&key) @@ -164,10 +169,6 @@ impl BuildSystem { &self.args ); - for path in &mut self.search_path_prefix { - *path = config_root.join(&path); - } - let querier: Arc = match &self.args { BuildSystemArgs::Buck(args) => Arc::new(BxlQuerier::new(args.clone())), BuildSystemArgs::Custom(args) => Arc::new(CustomQuerier::new(args.clone())), @@ -179,3 +180,81 @@ impl BuildSystem { Some(Ok(source_db)) } } + +#[cfg(test)] +mod tests { + use vec1::vec1; + + use super::*; + + #[test] + fn test_get_source_db_always_configures_paths() { + let mut bs = BuildSystem { + args: BuildSystemArgs::Custom(CustomQueryArgs { + command: vec1!["python3".to_owned()], + repo_root: None, + }), + ignore_if_build_system_missing: false, + search_path_prefix: vec![ + PathBuf::from("path/to/project"), + PathBuf::from("/absolute/path/to/project"), + ], + }; + let mut bs2 = bs.clone(); + + let root = Path::new("/root"); + + bs.get_source_db(root.to_path_buf()).unwrap().unwrap(); + assert_eq!( + &bs.search_path_prefix, + &[ + root.join("path/to/project"), + PathBuf::from("/absolute/path/to/project") + ] + ); + bs2.get_source_db(root.to_path_buf()).unwrap().unwrap(); + assert_eq!( + &bs2.search_path_prefix, + &[ + root.join("path/to/project"), + PathBuf::from("/absolute/path/to/project") + ] + ); + + // double check that configuring twice doesn't corrupt path, even though it should + // never be called twice + bs2.get_source_db(root.to_path_buf()).unwrap().unwrap(); + assert_eq!( + &bs2.search_path_prefix, + &[ + root.join("path/to/project"), + PathBuf::from("/absolute/path/to/project") + ] + ); + } + + #[test] + fn test_build_system_not_exist() { + let mut bs = BuildSystem { + args: BuildSystemArgs::Custom(CustomQueryArgs { + command: vec1!["this_command_should_not_exist_?/".to_owned()], + repo_root: None, + }), + ignore_if_build_system_missing: false, + search_path_prefix: vec![], + }; + let root = Path::new("/root"); + + bs.get_source_db(root.to_path_buf()).unwrap().unwrap_err(); + + let mut bs = BuildSystem { + args: BuildSystemArgs::Custom(CustomQueryArgs { + command: vec1!["this_command_should_not_exist_?/".to_owned()], + repo_root: None, + }), + ignore_if_build_system_missing: true, + search_path_prefix: vec![], + }; + assert!(bs.get_source_db(root.to_path_buf()).is_none()); + } +} diff --git a/crates/pyrefly_build/src/query/custom.rs b/crates/pyrefly_build/src/query/custom.rs index d8bd4b98eb..1d97060bea 100644 --- a/crates/pyrefly_build/src/query/custom.rs +++ b/crates/pyrefly_build/src/query/custom.rs @@ -37,7 +37,7 @@ pub struct CustomQueryArgs { /// The root of the repository. Repo roots here will be shared between configs. #[serde(default)] - repo_root: Option, + pub repo_root: Option, } impl CustomQueryArgs {