Skip to content

Commit f40038e

Browse files
mark-thmrickeylev
andauthored
fix: allow spaces in whl_librarys (bazel-contrib#2334)
Fixes bazel-contrib#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]>
1 parent 7c5e7cf commit f40038e

File tree

10 files changed

+65
-11
lines changed

10 files changed

+65
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ A brief description of the categories of changes:
3939
* (bzlmod) Generate `config_setting` values for all available toolchains instead
4040
of only the registered toolchains, which restores the previous behaviour that
4141
`bzlmod` users would have observed.
42+
* (pypi) (Bazel 7.4+) Allow spaces in filenames included in `whl_library`s
43+
([617](https://github.com/bazelbuild/rules_python/issues/617)).
4244

4345
{#v0-0-0-added}
4446
### Added

examples/bzlmod/MODULE.bazel.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ bzl_library(
173173
srcs = ["full_version.bzl"],
174174
)
175175

176+
bzl_library(
177+
name = "glob_excludes_bzl",
178+
srcs = ["glob_excludes.bzl"],
179+
deps = [":util_bzl"],
180+
)
181+
176182
bzl_library(
177183
name = "internal_config_repo_bzl",
178184
srcs = ["internal_config_repo.bzl"],

python/private/glob_excludes.bzl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"Utilities for glob exclusions."
16+
17+
load(":util.bzl", "IS_BAZEL_7_4_OR_HIGHER")
18+
19+
def _version_dependent_exclusions():
20+
"""Returns glob exclusions that are sensitive to Bazel version.
21+
22+
Returns:
23+
a list of glob exclusion patterns
24+
"""
25+
if IS_BAZEL_7_4_OR_HIGHER:
26+
return []
27+
else:
28+
return ["**/* *"]
29+
30+
glob_excludes = struct(
31+
version_dependent_exclusions = _version_dependent_exclusions,
32+
)

python/private/hermetic_runtime_repo_setup.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
1717
load("//python:py_runtime.bzl", "py_runtime")
1818
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
1919
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
20+
load(":glob_excludes.bzl", "glob_excludes")
2021
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
2122
load(":semver.bzl", "semver")
2223

@@ -64,7 +65,6 @@ def define_hermetic_runtime_toolchain_impl(
6465
# Platform-agnostic filegroup can't match on all patterns.
6566
allow_empty = True,
6667
exclude = [
67-
"**/* *", # Bazel does not support spaces in file names.
6868
# Unused shared libraries. `python` executable and the `:libpython` target
6969
# depend on `libpython{python_version}.so.1.0`.
7070
"lib/libpython{major}.{minor}.so".format(**version_dict),
@@ -74,7 +74,7 @@ def define_hermetic_runtime_toolchain_impl(
7474
"lib/python{major}.{minor}/**/test/**".format(**version_dict),
7575
"lib/python{major}.{minor}/**/tests/**".format(**version_dict),
7676
"**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
77-
] + extra_files_glob_exclude,
77+
] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude,
7878
),
7979
)
8080
cc_import(

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ bzl_library(
8686
srcs = ["deps.bzl"],
8787
deps = [
8888
"//python/private:bazel_tools_bzl",
89+
"//python/private:glob_excludes_bzl",
8990
],
9091
)
9192

python/private/pypi/deps.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ _GENERIC_WHEEL = """\
101101
package(default_visibility = ["//visibility:public"])
102102
103103
load("@rules_python//python:defs.bzl", "py_library")
104+
load("@rules_python//python/private:glob_excludes.bzl", "glob_excludes")
104105
105106
py_library(
106107
name = "lib",
@@ -111,11 +112,10 @@ py_library(
111112
"**/*.py",
112113
"**/*.pyc",
113114
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
114-
"**/* *",
115115
"**/*.dist-info/RECORD",
116116
"BUILD",
117117
"WORKSPACE",
118-
]),
118+
] + glob_excludes.version_dependent_exclusions()),
119119
# This makes this directory a top-level in the python import
120120
# search path for anything that depends on this.
121121
imports = ["."],

python/private/pypi/whl_library_targets.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
1818
load("//python:py_binary.bzl", "py_binary")
1919
load("//python:py_library.bzl", "py_library")
20+
load("//python/private:glob_excludes.bzl", "glob_excludes")
2021
load("//python/private:normalize_name.bzl", "normalize_name")
2122
load(
2223
":labels.bzl",
@@ -222,15 +223,14 @@ def whl_library_targets(
222223

223224
if hasattr(rules, "py_library"):
224225
_data_exclude = [
225-
"**/* *",
226226
"**/*.py",
227227
"**/*.pyc",
228228
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created
229229
# RECORD is known to contain sha256 checksums of files which might include the checksums
230230
# of generated files produced when wheels are installed. The file is ignored to avoid
231231
# Bazel caching issues.
232232
"**/*.dist-info/RECORD",
233-
]
233+
] + glob_excludes.version_dependent_exclusions()
234234
for item in data_exclude:
235235
if item not in _data_exclude:
236236
_data_exclude.append(item)

python/private/util.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def define_bazel_6_provider(doc, fields, **kwargs):
9999
return provider("Stub, not used", fields = []), None
100100
return provider(doc = doc, fields = fields, **kwargs)
101101

102+
IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals")
103+
102104
IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract")
103105

104106
# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a

tests/pypi/whl_library_targets/whl_library_targets_tests.bzl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
""
1616

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

2021
_tests = []
@@ -246,7 +247,12 @@ def _test_whl_and_library_deps(env):
246247
),
247248
"data": [] + _glob(
248249
["site-packages/**/*"],
249-
exclude = ["**/* *", "**/*.py", "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD"],
250+
exclude = [
251+
"**/*.py",
252+
"**/*.pyc",
253+
"**/*.pyc.*",
254+
"**/*.dist-info/RECORD",
255+
] + glob_excludes.version_dependent_exclusions(),
250256
),
251257
"imports": ["site-packages"],
252258
"deps": [
@@ -312,7 +318,12 @@ def _test_group(env):
312318
"srcs": _glob(["site-packages/**/*.py"], exclude = [], allow_empty = True),
313319
"data": [] + _glob(
314320
["site-packages/**/*"],
315-
exclude = ["**/* *", "**/*.py", "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD"],
321+
exclude = [
322+
"**/*.py",
323+
"**/*.pyc",
324+
"**/*.pyc.*",
325+
"**/*.dist-info/RECORD",
326+
] + glob_excludes.version_dependent_exclusions(),
316327
),
317328
"imports": ["site-packages"],
318329
"deps": ["@pypi_bar_baz//:pkg"] + _select({

0 commit comments

Comments
 (0)