Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 99 additions & 3 deletions crates/pyrefly_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -139,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)
Expand All @@ -147,9 +163,11 @@ impl BuildSystem {
return Some(Ok(result.dupe()));
}

for path in &mut self.search_path_prefix {
*path = config_root.join(&path);
}
info!(
"Loading new build system at {}: {}",
config_root.display(),
&self.args
);

let querier: Arc<dyn SourceDbQuerier> = match &self.args {
BuildSystemArgs::Buck(args) => Arc::new(BxlQuerier::new(args.clone())),
Expand All @@ -162,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());
}
}
11 changes: 11 additions & 0 deletions crates/pyrefly_build/src/query/buck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use std::fmt::Debug;
use std::fmt::Display;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 12 additions & 1 deletion crates/pyrefly_build/src/query/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use std::fmt::Debug;
use std::fmt::Display;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -36,7 +37,7 @@ pub struct CustomQueryArgs {

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

impl CustomQueryArgs {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
Loading