Skip to content

Commit

Permalink
fix: allow spaces in whl_librarys (#2334)
Browse files Browse the repository at this point in the history
Fixes #617.

Modern `setuptools` versions contain files critical to setuptools
functionality loaded proactively on module load that contain spaces.
Bazel 7.4.0+ now supports files with spaces in their names.

---------

Co-authored-by: Richard Levasseur <[email protected]>
Co-authored-by: Richard Levasseur <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 7c5e7cf commit f40038e
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ A brief description of the categories of changes:
* (bzlmod) Generate `config_setting` values for all available toolchains instead
of only the registered toolchains, which restores the previous behaviour that
`bzlmod` users would have observed.
* (pypi) (Bazel 7.4+) Allow spaces in filenames included in `whl_library`s
([617](https://github.com/bazelbuild/rules_python/issues/617)).

{#v0-0-0-added}
### Added
Expand Down
6 changes: 3 additions & 3 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ bzl_library(
srcs = ["full_version.bzl"],
)

bzl_library(
name = "glob_excludes_bzl",
srcs = ["glob_excludes.bzl"],
deps = [":util_bzl"],
)

bzl_library(
name = "internal_config_repo_bzl",
srcs = ["internal_config_repo.bzl"],
Expand Down
32 changes: 32 additions & 0 deletions python/private/glob_excludes.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"Utilities for glob exclusions."

load(":util.bzl", "IS_BAZEL_7_4_OR_HIGHER")

def _version_dependent_exclusions():
"""Returns glob exclusions that are sensitive to Bazel version.
Returns:
a list of glob exclusion patterns
"""
if IS_BAZEL_7_4_OR_HIGHER:
return []
else:
return ["**/* *"]

glob_excludes = struct(
version_dependent_exclusions = _version_dependent_exclusions,
)
4 changes: 2 additions & 2 deletions python/private/hermetic_runtime_repo_setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load(":glob_excludes.bzl", "glob_excludes")
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
load(":semver.bzl", "semver")

Expand Down Expand Up @@ -64,7 +65,6 @@ def define_hermetic_runtime_toolchain_impl(
# Platform-agnostic filegroup can't match on all patterns.
allow_empty = True,
exclude = [
"**/* *", # Bazel does not support spaces in file names.
# Unused shared libraries. `python` executable and the `:libpython` target
# depend on `libpython{python_version}.so.1.0`.
"lib/libpython{major}.{minor}.so".format(**version_dict),
Expand All @@ -74,7 +74,7 @@ def define_hermetic_runtime_toolchain_impl(
"lib/python{major}.{minor}/**/test/**".format(**version_dict),
"lib/python{major}.{minor}/**/tests/**".format(**version_dict),
"**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
] + extra_files_glob_exclude,
] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude,
),
)
cc_import(
Expand Down
1 change: 1 addition & 0 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bzl_library(
srcs = ["deps.bzl"],
deps = [
"//python/private:bazel_tools_bzl",
"//python/private:glob_excludes_bzl",
],
)

Expand Down
4 changes: 2 additions & 2 deletions python/private/pypi/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ _GENERIC_WHEEL = """\
package(default_visibility = ["//visibility:public"])
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python/private:glob_excludes.bzl", "glob_excludes")
py_library(
name = "lib",
Expand All @@ -111,11 +112,10 @@ py_library(
"**/*.py",
"**/*.pyc",
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
"**/* *",
"**/*.dist-info/RECORD",
"BUILD",
"WORKSPACE",
]),
] + glob_excludes.version_dependent_exclusions()),
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["."],
Expand Down
4 changes: 2 additions & 2 deletions python/private/pypi/whl_library_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_library.bzl", "py_library")
load("//python/private:glob_excludes.bzl", "glob_excludes")
load("//python/private:normalize_name.bzl", "normalize_name")
load(
":labels.bzl",
Expand Down Expand Up @@ -222,15 +223,14 @@ def whl_library_targets(

if hasattr(rules, "py_library"):
_data_exclude = [
"**/* *",
"**/*.py",
"**/*.pyc",
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created
# RECORD is known to contain sha256 checksums of files which might include the checksums
# of generated files produced when wheels are installed. The file is ignored to avoid
# Bazel caching issues.
"**/*.dist-info/RECORD",
]
] + glob_excludes.version_dependent_exclusions()
for item in data_exclude:
if item not in _data_exclude:
_data_exclude.append(item)
Expand Down
2 changes: 2 additions & 0 deletions python/private/util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def define_bazel_6_provider(doc, fields, **kwargs):
return provider("Stub, not used", fields = []), None
return provider(doc = doc, fields = fields, **kwargs)

IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals")

IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract")

# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a
Expand Down
15 changes: 13 additions & 2 deletions tests/pypi/whl_library_targets/whl_library_targets_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
""

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private:glob_excludes.bzl", "glob_excludes") # buildifier: disable=bzl-visibility
load("//python/private/pypi:whl_library_targets.bzl", "whl_library_targets") # buildifier: disable=bzl-visibility

_tests = []
Expand Down Expand Up @@ -246,7 +247,12 @@ def _test_whl_and_library_deps(env):
),
"data": [] + _glob(
["site-packages/**/*"],
exclude = ["**/* *", "**/*.py", "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD"],
exclude = [
"**/*.py",
"**/*.pyc",
"**/*.pyc.*",
"**/*.dist-info/RECORD",
] + glob_excludes.version_dependent_exclusions(),
),
"imports": ["site-packages"],
"deps": [
Expand Down Expand Up @@ -312,7 +318,12 @@ def _test_group(env):
"srcs": _glob(["site-packages/**/*.py"], exclude = [], allow_empty = True),
"data": [] + _glob(
["site-packages/**/*"],
exclude = ["**/* *", "**/*.py", "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD"],
exclude = [
"**/*.py",
"**/*.pyc",
"**/*.pyc.*",
"**/*.dist-info/RECORD",
] + glob_excludes.version_dependent_exclusions(),
),
"imports": ["site-packages"],
"deps": ["@pypi_bar_baz//:pkg"] + _select({
Expand Down

0 comments on commit f40038e

Please sign in to comment.