Skip to content

Commit 3bfe376

Browse files
connernilsenmeta-codesync[bot]
authored andcommitted
Update search path prefix when getting source db (#2380)
Summary: Pull Request resolved: #2380 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 fbshipit-source-id: 414a93c166c4c418edae7788b98749a391ac01b8
1 parent 0bb51f6 commit 3bfe376

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

crates/pyrefly_build/src/lib.rs

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ impl BuildSystem {
150150
Err(e) => return Some(Err(e)),
151151
Ok(path) => path,
152152
};
153+
154+
for path in &mut self.search_path_prefix {
155+
*path = config_root.join(&path);
156+
}
157+
153158
let mut cache = BUILD_SYSTEM_CACHE.lock();
154159
let key = (repo_root.clone(), self.args.clone());
155160
if let Some(maybe_result) = cache.get(&key)
@@ -164,10 +169,6 @@ impl BuildSystem {
164169
&self.args
165170
);
166171

167-
for path in &mut self.search_path_prefix {
168-
*path = config_root.join(&path);
169-
}
170-
171172
let querier: Arc<dyn SourceDbQuerier> = match &self.args {
172173
BuildSystemArgs::Buck(args) => Arc::new(BxlQuerier::new(args.clone())),
173174
BuildSystemArgs::Custom(args) => Arc::new(CustomQuerier::new(args.clone())),
@@ -179,3 +180,81 @@ impl BuildSystem {
179180
Some(Ok(source_db))
180181
}
181182
}
183+
184+
#[cfg(test)]
185+
mod tests {
186+
use vec1::vec1;
187+
188+
use super::*;
189+
190+
#[test]
191+
fn test_get_source_db_always_configures_paths() {
192+
let mut bs = BuildSystem {
193+
args: BuildSystemArgs::Custom(CustomQueryArgs {
194+
command: vec1!["python3".to_owned()],
195+
repo_root: None,
196+
}),
197+
ignore_if_build_system_missing: false,
198+
search_path_prefix: vec![
199+
PathBuf::from("path/to/project"),
200+
PathBuf::from("/absolute/path/to/project"),
201+
],
202+
};
203+
let mut bs2 = bs.clone();
204+
205+
let root = Path::new("/root");
206+
207+
bs.get_source_db(root.to_path_buf()).unwrap().unwrap();
208+
assert_eq!(
209+
&bs.search_path_prefix,
210+
&[
211+
root.join("path/to/project"),
212+
PathBuf::from("/absolute/path/to/project")
213+
]
214+
);
215+
bs2.get_source_db(root.to_path_buf()).unwrap().unwrap();
216+
assert_eq!(
217+
&bs2.search_path_prefix,
218+
&[
219+
root.join("path/to/project"),
220+
PathBuf::from("/absolute/path/to/project")
221+
]
222+
);
223+
224+
// double check that configuring twice doesn't corrupt path, even though it should
225+
// never be called twice
226+
bs2.get_source_db(root.to_path_buf()).unwrap().unwrap();
227+
assert_eq!(
228+
&bs2.search_path_prefix,
229+
&[
230+
root.join("path/to/project"),
231+
PathBuf::from("/absolute/path/to/project")
232+
]
233+
);
234+
}
235+
236+
#[test]
237+
fn test_build_system_not_exist() {
238+
let mut bs = BuildSystem {
239+
args: BuildSystemArgs::Custom(CustomQueryArgs {
240+
command: vec1!["this_command_should_not_exist_?/".to_owned()],
241+
repo_root: None,
242+
}),
243+
ignore_if_build_system_missing: false,
244+
search_path_prefix: vec![],
245+
};
246+
let root = Path::new("/root");
247+
248+
bs.get_source_db(root.to_path_buf()).unwrap().unwrap_err();
249+
250+
let mut bs = BuildSystem {
251+
args: BuildSystemArgs::Custom(CustomQueryArgs {
252+
command: vec1!["this_command_should_not_exist_?/".to_owned()],
253+
repo_root: None,
254+
}),
255+
ignore_if_build_system_missing: true,
256+
search_path_prefix: vec![],
257+
};
258+
assert!(bs.get_source_db(root.to_path_buf()).is_none());
259+
}
260+
}

crates/pyrefly_build/src/query/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct CustomQueryArgs {
3737

3838
/// The root of the repository. Repo roots here will be shared between configs.
3939
#[serde(default)]
40-
repo_root: Option<PathBuf>,
40+
pub repo_root: Option<PathBuf>,
4141
}
4242

4343
impl CustomQueryArgs {

0 commit comments

Comments
 (0)