repo: add repo main script's directory to PYTHONPATH.

Python 3.11 introduces PYTHONSAFEPATH and the -P flag which, if enabled,
does not prepend the script's directory to sys.path by default.
This breaks repo because main.py expects its own directory to be part of
Python's import path.

This causes problems with tools that add PYTHONSAFEPATH to python
programs, most notably Bazel.

We will simply prepend main.py's path to PYTHONPATH instead.

Bug: 307767740
Change-Id: I94f3fda50213e450df0d1e2df6a0b8b597416973
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/391236
Tested-by: Duy Truong <duytruong@google.com>
Commit-Queue: Mike Frysinger <vapier@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Duy Truong 2023-10-27 11:35:34 -07:00 committed by LUCI
parent c287428b37
commit 9f0ef5d926

8
repo
View File

@ -1494,6 +1494,14 @@ def main(orig_args):
if reqs:
reqs.assert_all()
# Python 3.11 introduces PYTHONSAFEPATH and the -P flag which, if enabled,
# does not prepend the script's directory to sys.path by default.
# repo relies on this import path, so add directory of REPO_MAIN to
# PYTHONPATH so that this continues to work when PYTHONSAFEPATH is enabled.
python_paths = os.environ.get("PYTHONPATH", "").split(os.pathsep)
new_python_paths = [os.path.join(rel_repo_dir, S_repo)] + python_paths
os.environ["PYTHONPATH"] = os.pathsep.join(new_python_paths)
ver_str = ".".join(map(str, VERSION))
me = [
sys.executable,