@@ -350,6 +350,7 @@ def _create_executable(
350350            main_py  =  main_py ,
351351            imports  =  imports ,
352352            runtime_details  =  runtime_details ,
353+             venv  =  venv ,
353354        )
354355        extra_runfiles  =  ctx .runfiles ([stage2_bootstrap ] +  venv .files_without_interpreter )
355356        zip_main  =  _create_zip_main (
@@ -538,11 +539,14 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
538539    ctx .actions .write (pyvenv_cfg , "" )
539540
540541    runtime  =  runtime_details .effective_runtime 
542+ 
541543    venvs_use_declare_symlink_enabled  =  (
542544        VenvsUseDeclareSymlinkFlag .get_value (ctx ) ==  VenvsUseDeclareSymlinkFlag .YES 
543545    )
546+     recreate_venv_at_runtime  =  False 
544547
545-     if  not  venvs_use_declare_symlink_enabled :
548+     if  not  venvs_use_declare_symlink_enabled  or  not  runtime .supports_build_time_venv :
549+         recreate_venv_at_runtime  =  True 
546550        if  runtime .interpreter :
547551            interpreter_actual_path  =  runfiles_root_path (ctx , runtime .interpreter .short_path )
548552        else :
@@ -557,6 +561,8 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
557561        ctx .actions .write (interpreter , "actual:{}" .format (interpreter_actual_path ))
558562
559563    elif  runtime .interpreter :
564+         # Some wrappers around the interpreter (e.g. pyenv) use the program 
565+         # name to decide what to do, so preserve the name. 
560566        py_exe_basename  =  paths .basename (runtime .interpreter .short_path )
561567
562568        # Even though ctx.actions.symlink() is used, using 
@@ -594,7 +600,8 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
594600    if  "t"  in  runtime .abi_flags :
595601        version  +=  "t" 
596602
597-     site_packages  =  "{}/lib/python{}/site-packages" .format (venv , version )
603+     venv_site_packages  =  "lib/python{}/site-packages" .format (version )
604+     site_packages  =  "{}/{}" .format (venv , venv_site_packages )
598605    pth  =  ctx .actions .declare_file ("{}/bazel.pth" .format (site_packages ))
599606    ctx .actions .write (pth , "import _bazel_site_init\n " )
600607
@@ -616,10 +623,12 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
616623
617624    return  struct (
618625        interpreter  =  interpreter ,
619-         recreate_venv_at_runtime  =  not   venvs_use_declare_symlink_enabled ,
626+         recreate_venv_at_runtime  =  recreate_venv_at_runtime ,
620627        # Runfiles root relative path or absolute path 
621628        interpreter_actual_path  =  interpreter_actual_path ,
622629        files_without_interpreter  =  [pyvenv_cfg , pth , site_init ] +  site_packages_symlinks ,
630+         # string; venv-relative path to the site-packages directory. 
631+         venv_site_packages  =  venv_site_packages ,
623632    )
624633
625634def  _create_site_packages_symlinks (ctx , site_packages ):
@@ -716,7 +725,8 @@ def _create_stage2_bootstrap(
716725        output_sibling ,
717726        main_py ,
718727        imports ,
719-         runtime_details ):
728+         runtime_details ,
729+         venv  =  None ):
720730    output  =  ctx .actions .declare_file (
721731        # Prepend with underscore to prevent pytest from trying to 
722732        # process the bootstrap for files starting with `test_` 
@@ -741,6 +751,7 @@ def _create_stage2_bootstrap(
741751            "%main%" : main_py_path ,
742752            "%main_module%" : ctx .attr .main_module ,
743753            "%target%" : str (ctx .label ),
754+             "%venv_rel_site_packages%" : venv .venv_site_packages  if  venv  else  "" ,
744755            "%workspace_name%" : ctx .workspace_name ,
745756        },
746757        is_executable  =  True ,
@@ -766,6 +777,12 @@ def _create_stage1_bootstrap(
766777
767778    python_binary_actual  =  venv .interpreter_actual_path  if  venv  else  "" 
768779
780+     # Runtime may be None on Windows due to the --python_path flag. 
781+     if  runtime  and  runtime .supports_build_time_venv :
782+         resolve_python_binary_at_runtime  =  "0" 
783+     else :
784+         resolve_python_binary_at_runtime  =  "1" 
785+ 
769786    subs  =  {
770787        "%interpreter_args%" : "\n " .join ([
771788            '"{}"' .format (v )
@@ -775,7 +792,9 @@ def _create_stage1_bootstrap(
775792        "%python_binary%" : python_binary_path ,
776793        "%python_binary_actual%" : python_binary_actual ,
777794        "%recreate_venv_at_runtime%" : str (int (venv .recreate_venv_at_runtime )) if  venv  else  "0" ,
795+         "%resolve_python_binary_at_runtime%" : resolve_python_binary_at_runtime ,
778796        "%target%" : str (ctx .label ),
797+         "%venv_rel_site_packages%" : venv .venv_site_packages  if  venv  else  "" ,
779798        "%workspace_name%" : ctx .workspace_name ,
780799    }
781800
0 commit comments