Skip to content

Commit bb09c6b

Browse files
jiangty-addeparnavkast
authored andcommitted
Fix running buck in virtualenv (#14)
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.
1 parent a8e25cb commit bb09c6b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

python-dsl/buck_parser/buck.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,8 @@ def __init__(
11111111
self._import_whitelist_manager = ImportWhitelistManager(
11121112
import_whitelist=self._create_import_whitelist(project_import_whitelist),
11131113
safe_modules_config=self.SAFE_MODULES_CONFIG,
1114-
path_predicate=lambda path: is_in_dir(path, self._project_root),
1114+
path_predicate=lambda path: is_in_dir(path, self._project_root) \
1115+
and not self._is_path_in_virtualenv(path)
11151116
)
11161117
# Set of helpers callable from the child environment.
11171118
self._default_globals_for_extension = self._create_default_globals(False, False)
@@ -1714,6 +1715,14 @@ def _create_import_whitelist(project_import_whitelist):
17141715

17151716
return set(global_whitelist + project_import_whitelist)
17161717

1718+
@staticmethod
1719+
def _is_path_in_virtualenv(path):
1720+
"""
1721+
If Python is executing in a virtualenv, check whether the path is part of the virtualenv.
1722+
"""
1723+
if hasattr(sys, 'real_prefix'): # in a virtualenv
1724+
return is_in_dir(path, sys.prefix)
1725+
17171726
def _file_access_wrapper(self, real):
17181727
"""
17191728
Return wrapper around function so that accessing a file produces warning if it is
@@ -1727,7 +1736,8 @@ def wrapper(filename, *arg, **kwargs):
17271736
with self._wrap_file_access(wrap=False):
17281737
if self._called_from_project_file():
17291738
path = os.path.abspath(filename)
1730-
if path not in self._current_build_env.includes:
1739+
if path not in self._current_build_env.includes \
1740+
and not self._is_path_in_virtualenv(path):
17311741
dep_path = "//" + os.path.relpath(path, self._project_root)
17321742
warning_message = (
17331743
"Access to a non-tracked file detected! {0} is not a ".format(

0 commit comments

Comments
 (0)