From 9f0ef5d926b29cabf8509be1b777c24c192c5e21 Mon Sep 17 00:00:00 2001 From: Duy Truong Date: Fri, 27 Oct 2023 11:35:34 -0700 Subject: [PATCH] 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 Commit-Queue: Mike Frysinger Reviewed-by: Mike Frysinger --- repo | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/repo b/repo index 704bb2eb..2dd9dd90 100755 --- a/repo +++ b/repo @@ -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,