@@ -445,6 +445,7 @@ aspect-runfiles-repo = {1}
445445/// the depth of the `ip` then back down to the workspace-relative path of
446446/// the target file.
447447pub fn populate_venv_with_copies (
448+ repo : & str ,
448449 venv : Virtualenv ,
449450 pth_file : PthFile ,
450451 bin_dir : PathBuf ,
@@ -455,7 +456,6 @@ pub fn populate_venv_with_copies(
455456
456457 // Get $PWD, which is the build working directory.
457458 let action_src_dir = current_dir ( ) . into_diagnostic ( ) ?;
458- let main_module = action_src_dir. file_name ( ) . unwrap ( ) ;
459459 let action_bin_dir = action_src_dir. join ( bin_dir) ;
460460
461461 #[ cfg( feature = "debug" ) ]
@@ -483,7 +483,7 @@ pub fn populate_venv_with_copies(
483483 . into_diagnostic ( ) ?;
484484
485485 for line in BufReader :: new ( source_pth) . lines ( ) . map_while ( Result :: ok) {
486- // #[cfg(feature = "debug")]
486+ #[ cfg( feature = "debug" ) ]
487487 eprintln ! ( "Got pth line {}" , & line) ;
488488
489489 let line = line. trim ( ) . to_string ( ) ;
@@ -496,12 +496,12 @@ pub fn populate_venv_with_copies(
496496 format ! ( "{}/" , line)
497497 } ;
498498
499- let Some ( ( workspace , entry_path) ) = line. split_once ( "/" ) else {
499+ let Some ( ( entry_repo , entry_path) ) = line. split_once ( "/" ) else {
500500 return Err ( miette ! ( "Invalid path file entry!" ) ) ;
501501 } ;
502502
503503 #[ cfg( feature = "debug" ) ]
504- eprintln ! ( "Got pth entry @{}//{}" , workspace , entry_path) ;
504+ eprintln ! ( "Got pth entry @{}//{}" , entry_repo , entry_path) ;
505505
506506 let mut entry = PathBuf :: from ( entry_path) ;
507507
@@ -511,9 +511,10 @@ pub fn populate_venv_with_copies(
511511 eprintln ! ( "Entry is site-packages..." ) ;
512512
513513 // If the entry is external then we have to adjust the path
514- if workspace != main_module {
514+ // FIXME: This isn't quite right outside of bzlmod
515+ if entry_repo != repo {
515516 entry = PathBuf :: from ( "external" )
516- . join ( PathBuf :: from ( workspace ) )
517+ . join ( PathBuf :: from ( entry_repo ) )
517518 . join ( entry)
518519 }
519520
@@ -537,32 +538,25 @@ pub fn populate_venv_with_copies(
537538 }
538539 }
539540 } else {
540- // Need to insert an appropriate pth file entry. Pth file lines
541- // are relativized to the site dir [1] so here we need to take
542- // the path from the site dir back to the root of the runfiles
543- // tree and then append the entry to that relative path.
544- //
545- // This is the path from the venv's site-packages destination
546- // "back up to" the bazel-bin dir we're building into, plus one
547- // level.
548- //
549- // [1] https://github.com/python/cpython/blob/ce31ae5209c976d28d1c21fcbb06c0ae5e50a896/Lib/site.py#L215
550-
551- // aspect-build/rules_py#610
552- //
553- // While these relative paths seem to work fine for _internal_
554- // runfiles within the `_main` workspace, problems occur when we
555- // try to take relative paths to _other_ workspaces because bzlmod
556- // may munge the directory names to be something that doesn't
557- // exist.
558- let path_to_runfiles =
559- diff_paths ( & action_bin_dir, action_bin_dir. join ( & venv. site_dir ) ) . unwrap ( ) ;
541+ if entry_repo != repo {
542+ eprintln ! ( "Warning: @@{entry_repo}//{entry_path} is not `site-packages`via pth rather than copy" , )
543+ }
544+ // The path to the runfiles root is _one more than_ the relative
545+ // oath from the venv's target dir to the root of the module
546+ // containing the venv.
547+ let path_to_runfiles = diff_paths ( & action_bin_dir, action_bin_dir. join ( & venv. site_dir ) )
548+ . unwrap ( )
549+ . join ( ".." ) ;
560550
561551 writeln ! ( dest_pth_writer, "# @{}" , line) . into_diagnostic ( ) ?;
562552 writeln ! (
563553 dest_pth_writer,
564554 "{}" ,
565- path_to_runfiles. join( entry) . to_str( ) . unwrap( )
555+ path_to_runfiles // .runfiles
556+ . join( entry_repo) // ${REPO}
557+ . join( entry_path) // ${PATH}
558+ . to_str( )
559+ . unwrap( )
566560 )
567561 . into_diagnostic ( ) ?;
568562 }
@@ -579,6 +573,7 @@ pub fn populate_venv_with_copies(
579573
580574#[ expect( unused_variables) ]
581575pub fn populate_venv_with_pth (
576+ repo : & str ,
582577 venv : Virtualenv ,
583578 pth_file : PthFile ,
584579 bin_dir : PathBuf ,
0 commit comments