Skip to content

Commit

Permalink
python: fix bazel py_test testSmoke for Python 3.11+
Browse files Browse the repository at this point in the history
The test was failing because the `b.py` file couldn't be imported
because, starting with Python 3.11, the `PYTHONSAFEPATH` environment
variable (set by the bootstrap startup) is respected. This setting
inhibits the default Python behavior of adding the main script's
directory to sys.path.

To fix, use `py_library.imports` to explicitly add the necessary
directory to `sys.path`.

Fixes bazelbuild#20660
  • Loading branch information
rickeylev committed Jan 3, 2024
1 parent 48ce49b commit b1be2de
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/test/py/bazel/py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def createSimpleFiles(self):
'a/BUILD',
[
'py_binary(name="a", srcs=["a.py"], deps=[":b"])',
'py_library(name="b", srcs=["b.py"])',
'py_library(name="b", srcs=["b.py"], imports=["."])',
])

self.ScratchFile(
Expand Down
2 changes: 1 addition & 1 deletion src/test/py/bazel/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def RunProgram(
]

if not allow_failure:
self.AssertExitCode(exit_code, 0, stderr_lines)
self.AssertExitCode(exit_code, 0, stderr_lines, stdout_lines)

return exit_code, stdout_lines, stderr_lines

Expand Down

0 comments on commit b1be2de

Please sign in to comment.