From 1a60fad7d0a8d04399cca0e5bc5d9ee5b01858eb Mon Sep 17 00:00:00 2001 From: keertk Date: Tue, 18 Apr 2023 09:35:20 -0700 Subject: [PATCH] fix(python): Set envvar for runfiles manifest, not runfiles dir, when using a manifest (#18133) Unfortunately, we weren't able to find a way to reproduce the reported bug in a test environment, but the line of code in question is obviously wrong, so we'll just omit a test to cover this. Fixes #17675 Closes #17722. PiperOrigin-RevId: 525044539 Change-Id: I7e1eaa14eac1d4dabcdcf93d92720c41977b1fe2 Co-authored-by: Fabian Meumertzheim --- .../lib/bazel/rules/python/python_stub_template.txt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt index 19514526104b4c..92dd6b82fa0dae 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt @@ -215,7 +215,13 @@ def GetRepositoriesImports(module_space, import_all): return [os.path.join(module_space, '%workspace_name%')] def RunfilesEnvvar(module_space): - """Finds the runfiles manifest or the runfiles directory.""" + """Finds the runfiles manifest or the runfiles directory. + + Returns: + A tuple of (var_name, var_value) where var_name is either 'RUNFILES_DIR' or + 'RUNFILES_MANIFEST_FILE' and var_value is the path to that directory or + file, or (None, None) if runfiles couldn't be found. + """ # If this binary is the data-dependency of another one, the other sets # RUNFILES_MANIFEST_FILE or RUNFILES_DIR for our sake. runfiles = os.environ.get('RUNFILES_MANIFEST_FILE', None) @@ -236,9 +242,12 @@ def RunfilesEnvvar(module_space): return ('RUNFILES_MANIFEST_FILE', runfiles) # Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST" + # Normally .runfiles_manifest and MANIFEST are both present, but the + # former will be missing for zip-based builds or if someone copies the + # runfiles tree elsewhere. runfiles = os.path.join(module_space, 'MANIFEST') if os.path.exists(runfiles): - return ('RUNFILES_DIR', runfiles) + return ('RUNFILES_MANIFEST_FILE', runfiles) # If running in a sandbox and no environment variables are set, then # Look for the runfiles next to the binary.