Skip to content

Commit 63f615e

Browse files
fix: Remove unused source module.modulemap files (#1085)
1 parent 0f5371f commit 63f615e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

swiftpkg/internal/repository_files.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,23 @@ def _exclude_paths(path_list, exclude_paths):
161161

162162
return results
163163

164-
def _find_and_delete_files(repository_ctx, path, name):
164+
def _find_and_delete_files(repository_ctx, path, name, exclude_paths = []):
165165
"""Finds files with the specified name under the specified path and deletes them.
166166
167167
Args:
168168
repository_ctx: A `repository_ctx` instance.
169169
path: A path `string` value.
170170
name: A file basename as a `string`.
171+
exclude_paths: Optional. A `list` of path `string` values to exclude
172+
from the search.
171173
"""
172174
find_args = ["find", path, "-type", "f", "-name", name]
175+
exclude_args = lists.flatten([
176+
["-not", "-path", path + "/" + exclude_path]
177+
for exclude_path in exclude_paths
178+
])
173179
rm_args = ["-delete"]
174-
all_args = find_args + rm_args
180+
all_args = find_args + exclude_args + rm_args
175181
exec_result = repository_ctx.execute(all_args, quiet = True)
176182
if exec_result.return_code != 0:
177183
fail("Failed to remove files named {name} under {path}. stderr:\n{stderr}".format(

swiftpkg/internal/swift_package.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ def _remove_bazel_files(repository_ctx, directory):
4444
for file in files:
4545
repository_files.find_and_delete_files(repository_ctx, directory, file)
4646

47+
def _remove_modulemaps(repository_ctx, directory, targets):
48+
repository_files.find_and_delete_files(
49+
repository_ctx,
50+
directory,
51+
"module.modulemap",
52+
exclude_paths = [
53+
# Framework modulemaps don't cause issues, and are needed
54+
"**/*.framework/*",
55+
] + [
56+
# We need to leave any modulemaps that we are passing into
57+
# `objc_library`
58+
target.clang_src_info.modulemap_path
59+
for target in targets
60+
if target.clang_src_info and target.clang_src_info.modulemap_path
61+
],
62+
)
63+
4764
def _swift_package_impl(repository_ctx):
4865
directory = str(repository_ctx.path("."))
4966
env = repo_rules.get_exec_env(repository_ctx)
@@ -68,6 +85,9 @@ def _swift_package_impl(repository_ctx):
6885
# Remove the git stuff
6986
repository_ctx.delete(repository_ctx.path(".git"))
7087

88+
# Remove unused modulemaps to prevent module redefinition errors
89+
_remove_modulemaps(repository_ctx, directory, pkg_ctx.pkg_info.targets)
90+
7191
# Return attributes that make this reproducible
7292
return _update_git_attrs(repository_ctx.attr, _ALL_ATTRS.keys(), update)
7393

0 commit comments

Comments
 (0)