diff --git a/gn.py b/gn.py index cca57f95b..e8a1f3a5c 100755 --- a/gn.py +++ b/gn.py @@ -18,7 +18,31 @@ import subprocess import sys +def PruneVirtualEnv(): + # Set by VirtualEnv, no need to keep it. + os.environ.pop('VIRTUAL_ENV', None) + + # Set by VPython, if scripts want it back they have to set it explicitly. + os.environ.pop('PYTHONNOUSERSITE', None) + + # Look for "activate_this.py" in this path, which is installed by VirtualEnv. + # This mechanism is used by vpython as well to sanitize VirtualEnvs from + # $PATH. + os.environ['PATH'] = os.pathsep.join([ + p for p in os.environ.get('PATH', '').split(os.pathsep) + if not os.path.isfile(os.path.join(p, 'activate_this.py')) + ]) + + def main(args): + # Prune all evidence of VPython/VirtualEnv out of the environment. This means + # that we 'unwrap' vpython VirtualEnv path/env manipulation. Invocations of + # `python` from GN should never inherit the gn.py's own VirtualEnv. This also + # helps to ensure that generated ninja files do not reference python.exe from + # the VirtualEnv generated from depot_tools' own .vpython file (or lack + # thereof), but instead reference the default python from the PATH. + PruneVirtualEnv() + # Try in primary solution location first, with the gn binary having been # downloaded by cipd in the projects DEPS. gn_path = os.path.join(gclient_utils.GetPrimarySolutionPath(), 'third_party',