Skip to content

Commit

Permalink
Fix running buck in virtualenv (#14)
Browse files Browse the repository at this point in the history
Buck was broken when run in a virtualenv inside the buck project, since a core python file becomes
subject to `allow_unsafe_import(false)`. This change detects whether the file we're parsing is inside
a virtualenv, and doesn't block imports if we are.

See facebook#2162 for a longer bug description and investigation.
  • Loading branch information
jiangty-addepar authored and navkast committed Oct 13, 2023
1 parent a8e25cb commit bb09c6b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions python-dsl/buck_parser/buck.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,8 @@ def __init__(
self._import_whitelist_manager = ImportWhitelistManager(
import_whitelist=self._create_import_whitelist(project_import_whitelist),
safe_modules_config=self.SAFE_MODULES_CONFIG,
path_predicate=lambda path: is_in_dir(path, self._project_root),
path_predicate=lambda path: is_in_dir(path, self._project_root) \
and not self._is_path_in_virtualenv(path)
)
# Set of helpers callable from the child environment.
self._default_globals_for_extension = self._create_default_globals(False, False)
Expand Down Expand Up @@ -1714,6 +1715,14 @@ def _create_import_whitelist(project_import_whitelist):

return set(global_whitelist + project_import_whitelist)

@staticmethod
def _is_path_in_virtualenv(path):
"""
If Python is executing in a virtualenv, check whether the path is part of the virtualenv.
"""
if hasattr(sys, 'real_prefix'): # in a virtualenv
return is_in_dir(path, sys.prefix)

def _file_access_wrapper(self, real):
"""
Return wrapper around function so that accessing a file produces warning if it is
Expand All @@ -1727,7 +1736,8 @@ def wrapper(filename, *arg, **kwargs):
with self._wrap_file_access(wrap=False):
if self._called_from_project_file():
path = os.path.abspath(filename)
if path not in self._current_build_env.includes:
if path not in self._current_build_env.includes \
and not self._is_path_in_virtualenv(path):
dep_path = "//" + os.path.relpath(path, self._project_root)
warning_message = (
"Access to a non-tracked file detected! {0} is not a ".format(
Expand Down

0 comments on commit bb09c6b

Please sign in to comment.