@@ -35,6 +35,7 @@ static PROJECT_ROOT: Lazy<String> =
3535 Lazy :: new ( || std:: env:: var ( "BUCK_PROJECT_ROOT" ) . expect ( "BUCK_PROJECT_ROOT must be set" ) ) ;
3636
3737const MOUNT_TIMEOUT_SECS : u64 = 7200 ;
38+ const DEFAULT_PREHEAT_SHALLOW_DEPTH : usize = 3 ;
3839static BUILD_CONFIG : Lazy < Option < BuildConfig > > = Lazy :: new ( load_build_config) ;
3940
4041/// Mounts filesystem via remote API for repository access.
@@ -411,6 +412,9 @@ async fn unmount_fs(repo: &str, cl: Option<&str>) -> Result<bool, Box<dyn Error
411412
412413/// Buck2 targets stats every directory, which is slow on FUSE.
413414/// We pre-warm metadata with `ls -lR` to reduce statx latency.
415+ /// TODO(perf): Replace this full-tree walk with a targeted preheat plan that
416+ /// only touches changed paths + ancestors and buck-critical files
417+ /// (PACKAGE/BUCK/.buckconfig) to avoid duplicated tree scans.
414418/// TODO: Rewrite the targets logic in the monolith.
415419fn preheat ( repo_path : & Path ) -> anyhow:: Result < ( ) > {
416420 let preheat_status = std:: process:: Command :: new ( "ls" )
@@ -485,7 +489,7 @@ fn preheat_shallow_depth() -> usize {
485489
486490 build_config ( )
487491 . map ( |config| config. orion_preheat_shallow_depth )
488- . unwrap_or ( 0 )
492+ . unwrap_or ( DEFAULT_PREHEAT_SHALLOW_DEPTH )
489493}
490494
491495fn parse_env_usize ( key : & str ) -> Option < usize > {
@@ -501,21 +505,6 @@ fn parse_env_usize(key: &str) -> Option<usize> {
501505 }
502506}
503507
504- fn parse_env_path ( key : & str ) -> Option < PathBuf > {
505- match std:: env:: var ( key) {
506- Ok ( value) => {
507- let trimmed = value. trim ( ) ;
508- if trimmed. is_empty ( ) {
509- tracing:: warn!( "Empty {key} provided, ignoring." ) ;
510- None
511- } else {
512- Some ( PathBuf :: from ( trimmed) )
513- }
514- }
515- Err ( _) => None ,
516- }
517- }
518-
519508fn build_config ( ) -> Option < & ' static BuildConfig > {
520509 BUILD_CONFIG . as_ref ( )
521510}
@@ -558,63 +547,26 @@ fn resolve_config_path() -> Option<PathBuf> {
558547 None
559548}
560549
561- fn buck2_isolation_dir_base ( ) -> ( PathBuf , bool ) {
562- if let Some ( path) = parse_env_path ( "ORION_BUCK2_ISOLATION_DIR_BASE" ) {
563- return ( path, true ) ;
564- }
565-
566- if let Some ( path) = parse_env_path ( "MEGA_BUILD__ORION_BUCK2_ISOLATION_DIR_BASE" ) {
567- return ( path, true ) ;
568- }
569-
570- if let Some ( config) = build_config ( )
571- && let Some ( path) = config. orion_buck2_isolation_dir_base . as_ref ( )
572- {
573- let trimmed = path. trim ( ) ;
574- if !trimmed. is_empty ( ) {
575- return ( PathBuf :: from ( trimmed) , true ) ;
576- }
577- }
578-
579- ( std:: env:: temp_dir ( ) , false )
580- }
581-
582- /// Derive a stable per-mount buck2 isolation directory and ensure it exists.
583- fn buck2_isolation_dir ( repo_path : & Path ) -> anyhow:: Result < PathBuf > {
550+ /// Derive a stable per-mount buck2 isolation directory name.
551+ ///
552+ /// Buck2 `--isolation-dir` expects a plain directory **name** (no path
553+ /// separators). Buck2 itself stores daemon state under
554+ /// `<project_root>/.buck2/<isolation_dir>/`, so we only need to return a
555+ /// unique name – not a full path.
556+ fn buck2_isolation_dir ( repo_path : & Path ) -> anyhow:: Result < String > {
584557 let digest = ring:: digest:: digest (
585558 & ring:: digest:: SHA256 ,
586559 repo_path. to_string_lossy ( ) . as_bytes ( ) ,
587560 ) ;
588561 let suffix = & hex:: encode ( digest. as_ref ( ) ) [ ..16 ] ;
589- let ( base, from_env) = buck2_isolation_dir_base ( ) ;
590- let mut path = base. join ( format ! ( "buck2-isolation-{suffix}" ) ) ;
591- if let Err ( err) = std:: fs:: create_dir_all ( & path) {
592- if from_env {
593- tracing:: warn!(
594- "Failed to create buck2 isolation dir {path:?} from ORION_BUCK2_ISOLATION_DIR_BASE: {err}. Falling back to temp dir."
595- ) ;
596- let fallback = std:: env:: temp_dir ( ) ;
597- path = fallback. join ( format ! ( "buck2-isolation-{suffix}" ) ) ;
598- std:: fs:: create_dir_all ( & path)
599- . map_err ( |err| anyhow ! ( "Failed to create buck2 isolation dir {path:?}: {err}" ) ) ?;
600- } else {
601- return Err ( anyhow ! (
602- "Failed to create buck2 isolation dir {path:?}: {err}"
603- ) ) ;
604- }
605- }
606- Ok ( path)
562+ Ok ( format ! ( "buck2-isolation-{suffix}" ) )
607563}
608564
609565/// Get target of a specific repo under tmp directory.
610566fn get_repo_targets ( file_name : & str , repo_path : & Path ) -> anyhow:: Result < Targets > {
611567 const MAX_ATTEMPTS : usize = 2 ;
612568 let jsonl_path = PathBuf :: from ( repo_path) . join ( file_name) ;
613569 let isolation_dir = buck2_isolation_dir ( repo_path) ?;
614- let isolation_dir = isolation_dir
615- . to_str ( )
616- . ok_or_else ( || anyhow ! ( "Invalid isolation dir path: {isolation_dir:?}" ) ) ?
617- . to_string ( ) ;
618570
619571 preheat ( repo_path) ?;
620572
@@ -669,12 +621,7 @@ async fn get_build_targets(
669621
670622 preheat_shallow ( & mount_path, preheat_shallow_depth ( ) ) ?;
671623 let mut buck2 = Buck2 :: with_root ( "buck2" . to_string ( ) , mount_path. clone ( ) ) ;
672- buck2. set_isolation_dir (
673- buck2_isolation_dir ( & mount_path) ?
674- . to_str ( )
675- . ok_or_else ( || anyhow ! ( "Invalid isolation dir path: {mount_path:?}" ) ) ?
676- . to_string ( ) ,
677- ) ;
624+ buck2. set_isolation_dir ( buck2_isolation_dir ( & mount_path) ?) ;
678625 let mut cells = CellInfo :: parse (
679626 & buck2
680627 . cells ( )
@@ -894,10 +841,6 @@ pub async fn build(
894841 // This ensures buck2 uses the sub-project's .buckconfig and PACKAGE files.
895842 let project_root = PathBuf :: from ( & mount_point) . join ( repo_prefix) ;
896843 let isolation_dir = buck2_isolation_dir ( & project_root) ?;
897- let isolation_dir = isolation_dir
898- . to_str ( )
899- . ok_or_else ( || anyhow ! ( "Invalid isolation dir path: {isolation_dir:?}" ) ) ?
900- . to_string ( ) ;
901844 let mut cmd = Command :: new ( "buck2" ) ;
902845 cmd. args ( [ "--isolation-dir" , & isolation_dir] ) ;
903846 let cmd = cmd
0 commit comments