Skip to content

Commit f30110b

Browse files
committed
Merge branch 'main' of https://github.com/bazel-contrib/rules_python into pr-3426
2 parents 237aad1 + 23397d5 commit f30110b

File tree

22 files changed

+88
-343
lines changed

22 files changed

+88
-343
lines changed

.bazelci/presubmit.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,20 +614,6 @@ tasks:
614614
- "bazel run //:os_specific_requirements.update"
615615
- "git diff --exit-code"
616616

617-
618-
integration_test_ignore_root_user_error_macos_workspace:
619-
<<: *reusable_build_test_all
620-
<<: *common_workspace_flags
621-
name: "ignore_root_user_error: macOS, workspace"
622-
working_directory: tests/integration/ignore_root_user_error
623-
platform: macos_arm64
624-
integration_test_ignore_root_user_error_windows_workspace:
625-
<<: *reusable_build_test_all
626-
<<: *common_workspace_flags
627-
name: "ignore_root_user_error: Windows, workspace"
628-
working_directory: tests/integration/ignore_root_user_error
629-
platform: windows
630-
631617
integration_compile_pip_requirements_test_from_external_repo_ubuntu_min_workspace:
632618
<<: *minimum_supported_version
633619
<<: *common_workspace_flags_min_bazel

.bazelignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ gazelle/examples/bzlmod_build_file_generation/bazel-bzlmod_build_file_generation
3131
gazelle/examples/bzlmod_build_file_generation/bazel-out
3232
gazelle/examples/bzlmod_build_file_generation/bazel-testlog
3333
tests/integration/compile_pip_requirements/bazel-compile_pip_requirements
34-
tests/integration/ignore_root_user_error/bazel-ignore_root_user_error
3534
tests/integration/local_toolchains/bazel-local_toolchains
3635
tests/integration/py_cc_toolchain_registered/bazel-py_cc_toolchain_registered

.bazelrc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
# Trick bazel into treating BUILD files under examples/* as being regular files
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
5-
# To update these lines, execute
6-
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
5+
# To update the file, execute
6+
import %workspace%/.bazelrc.deleted_packages
97

108
test --test_output=errors
119

12-
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation
13-
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation/runfiles
14-
1510
# Do NOT implicitly create empty __init__.py files in the runfiles tree.
1611
# By default, these are created in every directory containing Python source code
1712
# or shared libraries, and every parent directory of those directories,
@@ -30,6 +25,9 @@ build --enable_runfiles
3025
# Make Bazel 7 use bzlmod by default
3126
common --enable_bzlmod
3227

28+
# Local disk cache greatly speeds up builds if the regular cache is lost
29+
common --disk_cache=~/.cache/bazel/bazel-disk-cache
30+
3331
# Additional config to use for readthedocs builds.
3432
# See .readthedocs.yml for additional flags that can only be determined from
3533
# the runtime environment.

.bazelrc.deleted_packages

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Generated via './tools/update_deleted_packages.sh'
2+
common --deleted_packages=examples/build_file_generation
3+
common --deleted_packages=examples/build_file_generation/random_number_generator
4+
common --deleted_packages=examples/bzlmod
5+
common --deleted_packages=examples/bzlmod/entry_points
6+
common --deleted_packages=examples/bzlmod/entry_points/tests
7+
common --deleted_packages=examples/bzlmod/libs/my_lib
8+
common --deleted_packages=examples/bzlmod/other_module
9+
common --deleted_packages=examples/bzlmod/other_module/other_module/pkg
10+
common --deleted_packages=examples/bzlmod/patches
11+
common --deleted_packages=examples/bzlmod/py_proto_library
12+
common --deleted_packages=examples/bzlmod/py_proto_library/example.com/another_proto
13+
common --deleted_packages=examples/bzlmod/py_proto_library/example.com/proto
14+
common --deleted_packages=examples/bzlmod/py_proto_library/foo_external
15+
common --deleted_packages=examples/bzlmod/runfiles
16+
common --deleted_packages=examples/bzlmod/tests
17+
common --deleted_packages=examples/bzlmod/tests/other_module
18+
common --deleted_packages=examples/bzlmod/whl_mods
19+
common --deleted_packages=examples/multi_python_versions/libs/my_lib
20+
common --deleted_packages=examples/multi_python_versions/requirements
21+
common --deleted_packages=examples/multi_python_versions/tests
22+
common --deleted_packages=examples/pip_parse
23+
common --deleted_packages=examples/pip_parse_vendored
24+
common --deleted_packages=examples/pip_repository_annotations
25+
common --deleted_packages=examples/py_proto_library
26+
common --deleted_packages=examples/py_proto_library/example.com/another_proto
27+
common --deleted_packages=examples/py_proto_library/example.com/proto
28+
common --deleted_packages=gazelle
29+
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation
30+
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation/other_module/other_module/pkg
31+
common --deleted_packages=gazelle/examples/bzlmod_build_file_generation/runfiles
32+
common --deleted_packages=gazelle/manifest
33+
common --deleted_packages=gazelle/manifest/generate
34+
common --deleted_packages=gazelle/manifest/hasher
35+
common --deleted_packages=gazelle/manifest/test
36+
common --deleted_packages=gazelle/modules_mapping
37+
common --deleted_packages=gazelle/python
38+
common --deleted_packages=gazelle/pythonconfig
39+
common --deleted_packages=gazelle/python/private
40+
common --deleted_packages=tests/integration/compile_pip_requirements
41+
common --deleted_packages=tests/integration/compile_pip_requirements_test_from_external_repo
42+
common --deleted_packages=tests/integration/custom_commands
43+
common --deleted_packages=tests/integration/local_toolchains
44+
common --deleted_packages=tests/integration/pip_parse
45+
common --deleted_packages=tests/integration/pip_parse/empty
46+
common --deleted_packages=tests/integration/py_cc_toolchain_registered
47+
common --deleted_packages=tests/modules/another_module
48+
common --deleted_packages=tests/modules/other
49+
common --deleted_packages=tests/modules/other/nspkg_delta
50+
common --deleted_packages=tests/modules/other/nspkg_gamma
51+
common --deleted_packages=tests/modules/other/nspkg_single
52+
common --deleted_packages=tests/modules/other/simple_v1
53+
common --deleted_packages=tests/modules/other/simple_v2
54+
common --deleted_packages=tests/modules/other/with_external_data

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ repos:
4848
language: system
4949
# 7.x is necessary until https://github.com/bazel-contrib/rules_bazel_integration_test/pull/414
5050
# is merged and released
51-
entry: env USE_BAZEL_VERSION=7.x bazel run @rules_bazel_integration_test//tools:update_deleted_packages
51+
entry: ./tools/update_deleted_packages.sh
5252
files: ^((examples|tests)/.*/(MODULE.bazel|WORKSPACE|WORKSPACE.bzlmod|BUILD.bazel)|.bazelrc)$
5353
pass_filenames: false

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ END_UNRELEASED_TEMPLATE
6262
* (toolchain) Remove all of the python 3.9 toolchain versions except for the `3.9.25`.
6363
This version has reached EOL and will no longer receive any security fixes, please update to
6464
`3.10` or above. ([#2704](https://github.com/bazel-contrib/rules_python/issues/2704))
65+
* (toolchain) `ignore_root_user_error` has now been flipped to be always enabled and
66+
the `chmod` of the python toolchain directories have been removed. From now on `rules_python`
67+
always adds the `pyc` files to the glob excludes and in order to avoid any problems when using
68+
the toolchains in the repository phase, ensure that you pass `-B` to the python interpreter.
69+
([#2016](https://github.com/bazel-contrib/rules_python/issues/2016))
6570

6671
{#v0-0-0-changed}
6772
### Changed

python/private/python.bzl

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
7676
# Map of string Major.Minor or Major.Minor.Patch to the toolchain_info struct
7777
global_toolchain_versions = {}
7878

79-
ignore_root_user_error = None
80-
81-
# if the root module does not register any toolchain then the
82-
# ignore_root_user_error takes its default value: True
83-
if not module_ctx.modules[0].tags.toolchain:
84-
ignore_root_user_error = True
85-
8679
config = _get_toolchain_config(modules = module_ctx.modules, _fail = _fail)
8780

8881
default_python_version = _compute_default_python_version(module_ctx)
@@ -115,15 +108,6 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
115108
# * The root module is allowed to override the rules_python default.
116109
is_default = default_python_version == toolchain_version
117110

118-
# Also only the root module should be able to decide ignore_root_user_error.
119-
# Modules being depended upon don't know the final environment, so they aren't
120-
# in the right position to know or decide what the correct setting is.
121-
122-
# If an inconsistency in the ignore_root_user_error among multiple toolchains is detected, fail.
123-
if ignore_root_user_error != None and toolchain_attr.ignore_root_user_error != ignore_root_user_error:
124-
fail("Toolchains in the root module must have consistent 'ignore_root_user_error' attributes")
125-
126-
ignore_root_user_error = toolchain_attr.ignore_root_user_error
127111
elif mod.name == "rules_python" and not default_toolchain:
128112
# This branch handles when the root module doesn't declare a
129113
# Python toolchain
@@ -166,7 +150,6 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
166150
global_toolchain_versions[toolchain_version] = toolchain_info
167151
if debug_info:
168152
debug_info["toolchains_registered"].append({
169-
"ignore_root_user_error": ignore_root_user_error,
170153
"module": {"is_root": mod.is_root, "name": mod.name},
171154
"name": toolchain_name,
172155
})
@@ -185,8 +168,6 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
185168
elif toolchain_info:
186169
toolchains.append(toolchain_info)
187170

188-
config.default.setdefault("ignore_root_user_error", ignore_root_user_error)
189-
190171
# A default toolchain is required so that the non-version-specific rules
191172
# are able to match a toolchain.
192173
if default_toolchain == None:
@@ -722,7 +703,6 @@ def _process_global_overrides(*, tag, default, _fail = fail):
722703
default["minor_mapping"] = tag.minor_mapping
723704

724705
forwarded_attrs = sorted(AUTH_ATTRS) + [
725-
"ignore_root_user_error",
726706
"base_url",
727707
"register_all_versions",
728708
]
@@ -974,7 +954,6 @@ def _create_toolchain_attrs_struct(
974954
is_default = is_default,
975955
python_version = python_version if python_version else tag.python_version,
976956
configure_coverage_tool = getattr(tag, "configure_coverage_tool", False),
977-
ignore_root_user_error = getattr(tag, "ignore_root_user_error", True),
978957
)
979958

980959
_defaults = tag_class(
@@ -1086,16 +1065,9 @@ Then the python interpreter will be available as `my_python_name`.
10861065
"ignore_root_user_error": attr.bool(
10871066
default = True,
10881067
doc = """\
1089-
The Python runtime installation is made read only. This improves the ability for
1090-
Bazel to cache it by preventing the interpreter from creating `.pyc` files for
1091-
the standard library dynamically at runtime as they are loaded (this often leads
1092-
to spurious cache misses or build failures).
1093-
1094-
However, if the user is running Bazel as root, this read-onlyness is not
1095-
respected. Bazel will print a warning message when it detects that the runtime
1096-
installation is writable despite being made read only (i.e. it's running with
1097-
root access) while this attribute is set `False`, however this messaging can be ignored by setting
1098-
this to `False`.
1068+
:::{versionchanged} VERSION_NEXT_FEATURE
1069+
Noop, will be removed in the next major release.
1070+
:::
10991071
""",
11001072
mandatory = False,
11011073
),

python/private/python_repository.bzl

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,45 +123,6 @@ def _python_repository_impl(rctx):
123123
logger = logger,
124124
)
125125

126-
# Make the Python installation read-only. This is to prevent issues due to
127-
# pycs being generated at runtime:
128-
# * The pycs are not deterministic (they contain timestamps)
129-
# * Multiple processes trying to write the same pycs can result in errors.
130-
#
131-
# Note, when on Windows the `chmod` may not work
132-
if "windows" not in platform and "windows" != repo_utils.get_platforms_os_name(rctx):
133-
repo_utils.execute_checked(
134-
rctx,
135-
op = "python_repository.MakeReadOnly",
136-
arguments = [repo_utils.which_checked(rctx, "chmod"), "-R", "ugo-w", "lib"],
137-
logger = logger,
138-
)
139-
140-
# If the user is not ignoring the warnings, then proceed to run a check,
141-
# otherwise these steps can be skipped, as they both result in some warning.
142-
if not rctx.attr.ignore_root_user_error:
143-
exec_result = repo_utils.execute_unchecked(
144-
rctx,
145-
op = "python_repository.TestReadOnly",
146-
arguments = [repo_utils.which_checked(rctx, "touch"), "lib/.test"],
147-
logger = logger,
148-
)
149-
150-
# The issue with running as root is the installation is no longer
151-
# read-only, so the problems due to pyc can resurface.
152-
if exec_result.return_code == 0:
153-
stdout = repo_utils.execute_checked_stdout(
154-
rctx,
155-
op = "python_repository.GetUserId",
156-
arguments = [repo_utils.which_checked(rctx, "id"), "-u"],
157-
logger = logger,
158-
)
159-
uid = int(stdout.strip())
160-
if uid == 0:
161-
logger.warn("The current user is root, which can cause spurious cache misses or build failures with the hermetic Python interpreter. See https://github.com/bazel-contrib/rules_python/pull/713.")
162-
else:
163-
logger.warn("The current user has CAP_DAC_OVERRIDE set, which can cause spurious cache misses or build failures with the hermetic Python interpreter. See https://github.com/bazel-contrib/rules_python/pull/713.")
164-
165126
python_bin = "python.exe" if ("windows" in platform) else "bin/python3"
166127

167128
if "linux" in platform:
@@ -186,17 +147,15 @@ def _python_repository_impl(rctx):
186147
break
187148

188149
glob_include = []
189-
glob_exclude = []
190-
if rctx.attr.ignore_root_user_error or "windows" in platform:
191-
glob_exclude += [
192-
# These pycache files are created on first use of the associated python files.
193-
# Exclude them from the glob because otherwise between the first time and second time a python toolchain is used,"
194-
# the definition of this filegroup will change, and depending rules will get invalidated."
195-
# See https://github.com/bazel-contrib/rules_python/issues/1008 for unconditionally adding these to toolchains so we can stop ignoring them."
196-
# pyc* is ignored because pyc creation creates temporary .pyc.NNNN files
197-
"**/__pycache__/*.pyc*",
198-
"**/__pycache__/*.pyo*",
199-
]
150+
glob_exclude = [
151+
# These pycache files are created on first use of the associated python files.
152+
# Exclude them from the glob because otherwise between the first time and second time a python toolchain is used,"
153+
# the definition of this filegroup will change, and depending rules will get invalidated."
154+
# See https://github.com/bazel-contrib/rules_python/issues/1008 for unconditionally adding these to toolchains so we can stop ignoring them."
155+
# pyc* is ignored because pyc creation creates temporary .pyc.NNNN files
156+
"**/__pycache__/*.pyc*",
157+
"**/__pycache__/*.pyo*",
158+
]
200159

201160
if "windows" in platform:
202161
glob_include += [
@@ -249,7 +208,6 @@ define_hermetic_runtime_toolchain_impl(
249208
"coverage_tool": rctx.attr.coverage_tool,
250209
"distutils": rctx.attr.distutils,
251210
"distutils_content": rctx.attr.distutils_content,
252-
"ignore_root_user_error": rctx.attr.ignore_root_user_error,
253211
"name": rctx.attr.name,
254212
"netrc": rctx.attr.netrc,
255213
"patch_strip": rctx.attr.patch_strip,
@@ -299,7 +257,7 @@ For more information see {attr}`py_runtime.coverage_tool`.
299257
),
300258
"ignore_root_user_error": attr.bool(
301259
default = True,
302-
doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files.",
260+
doc = "Noop, will be removed in the next major release",
303261
mandatory = False,
304262
),
305263
"netrc": attr.string(

0 commit comments

Comments
 (0)