Skip to content

Commit 18a7bb5

Browse files
authored
fix: make import python.runfiles work with --experimental_python_import_all_repositories=false (#1243)
Because `--experimental_python_import_all_repositories` defaults to true, every repository's directory is added to `sys.path`, which makes `import python.runfiles` work. However, we shouldn't rely on that behavior for a couple reasons: * We recommend disabling it for fewer sys.path entries (even non-Python related repos get added to the path). * Some users _must_ disable it because the resulting PYTHONPATH is too long. To fix, set the `imports` attribute on `//python/runfiles:runfiles` so that `import python.runfiles` works. The net result is the `rules_python` repo directory is added to sys.path even if `--experimental_python_import_all_repositories=false`. Fixes #1241
1 parent 62e95a4 commit 18a7bb5

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

python/runfiles/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ py_library(
2727
"__init__.py",
2828
"runfiles.py",
2929
],
30+
imports = [
31+
# Add the repo root so `import python.runfiles.runfiles` works. This makes it agnostic
32+
# to the --experimental_python_import_all_repositories setting.
33+
"../..",
34+
],
3035
visibility = ["//visibility:public"],
3136
)
3237

0 commit comments

Comments
 (0)