Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow spaces in whl_librarys #2334

Merged
merged 25 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ 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) Allow spaces in filenames included in `whl_library`s
([617](https://github.com/bazelbuild/rules_python/issues/617))
* (deps) Bump bazel-features to detect whether spaces are allowed
in filenames ([617](https://github.com/bazelbuild/rules_python/issues/617)

{#v0-0-0-added}
### Added
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_features", version = "1.9.1")
bazel_dep(name = "bazel_features", version = "1.20.0")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "platforms", version = "0.0.4")
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ load("//:internal_deps.bzl", "rules_python_internal_deps")

rules_python_internal_deps()

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()
Expand Down
11 changes: 5 additions & 6 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: 3 additions & 3 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ def rules_python_internal_deps():

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
sha256 = "c2596994cf63513bd44180411a4ac3ae95d32bf59148fcb6087a4642b3ffef11",
strip_prefix = "bazel_features-1.20.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.20.0/bazel_features-v1.20.0.tar.gz",
)

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

bzl_library(
name = "glob_excludes_bzl",
srcs = ["glob_excludes.bzl"],
deps = [
"@bazel_features//:features",
],
)
rickeylev marked this conversation as resolved.
Show resolved Hide resolved

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("@bazel_features//:features.bzl", "bazel_features")

def _version_dependent_exclusions():
"""Returns glob exclusions that are sensitive to Bazel version.

Returns:
a list of glob exclusion patterns
"""
if bazel_features.rules.all_characters_allowed_in_runfiles:
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
6 changes: 6 additions & 0 deletions python/private/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ def py_repositories():
strip_prefix = "protobuf-27.0",
url = "https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protobuf-27.0.tar.gz",
)
http_archive(
name = "bazel_features",
sha256 = "c2596994cf63513bd44180411a4ac3ae95d32bf59148fcb6087a4642b3ffef11",
strip_prefix = "bazel_features-1.20.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.20.0/bazel_features-v1.20.0.tar.gz",
)
rickeylev marked this conversation as resolved.
Show resolved Hide resolved
pypi_deps()
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
43 changes: 33 additions & 10 deletions python/private/pypi/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//python/private:glob_excludes.bzl", "glob_excludes")

_RULE_DEPS = [
# START: maintained by 'bazel run //tools/private/update_deps:update_pip_deps'
Expand Down Expand Up @@ -97,6 +98,31 @@ _RULE_DEPS = [
# END: maintained by 'bazel run //tools/private/update_deps:update_pip_deps'
]

_EXCLUSIONS = [
("**/*.py", None),
("**/*.pyc", None),
("**/*.pyc.*", "During pyc creation, temp files named *.pyc.NNN are created"),
("**/*.dist-info/RECORD", None),
("BUILD", None),
("WORKSPACE", None),
] + [(e, None) for e in glob_excludes.version_dependent_exclusions()]

def _to_comment(comment):
if comment:
return " #" + comment
return ""

def _format_exclusions(exclusions, indent_depth):
indent = " " * indent_depth
return "\n".join([
"{indent}\"{excl}\",{comment}".format(
indent = indent,
excl = excl,
comment = _to_comment(comment),
)
for excl, comment in exclusions
])

_GENERIC_WHEEL = """\
package(default_visibility = ["//visibility:public"])

Expand All @@ -105,22 +131,19 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "lib",
srcs = glob(["**/*.py"]),
data = glob(["**/*"], exclude=[
data = glob(
["**/*"],
# These entries include those put into user-installed dependencies by
# data_exclude to avoid non-determinism.
"**/*.py",
"**/*.pyc",
"**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
"**/* *",
"**/*.dist-info/RECORD",
"BUILD",
"WORKSPACE",
]),
exclude=[
{exclusions}
],
),
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["."],
)
"""
""".format(exclusions = _format_exclusions(_EXCLUSIONS, 12))

# Collate all the repository names so they can be easily consumed
all_repo_names = [name for (name, _, _) in _RULE_DEPS]
Expand Down