From 4318ed4e564986c27d24aa51fbfd5b9bb58dae51 Mon Sep 17 00:00:00 2001 From: Brentley Jones Date: Fri, 24 May 2024 10:33:32 -0500 Subject: [PATCH] Remove unused source `module.modulemap` files These can cause module redefinition errors when sandboxing is disabled. Since we never want to use them, we remove them from the repository. We keep around modulemaps that we pass to `objc_library` and any inside frameworks. --- swiftpkg/internal/repository_files.bzl | 10 ++++++++-- swiftpkg/internal/swift_package.bzl | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/swiftpkg/internal/repository_files.bzl b/swiftpkg/internal/repository_files.bzl index fc19c12d9..a907f83bd 100644 --- a/swiftpkg/internal/repository_files.bzl +++ b/swiftpkg/internal/repository_files.bzl @@ -161,17 +161,23 @@ def _exclude_paths(path_list, exclude_paths): return results -def _find_and_delete_files(repository_ctx, path, name): +def _find_and_delete_files(repository_ctx, path, name, exclude_paths = []): """Finds files with the specified name under the specified path and deletes them. Args: repository_ctx: A `repository_ctx` instance. path: A path `string` value. name: A file basename as a `string`. + exclude_paths: Optional. A `list` of path `string` values to exclude + from the search. """ find_args = ["find", path, "-type", "f", "-name", name] + exclude_args = lists.flatten([ + ["-not", "-path", path + "/" + exclude_path] + for exclude_path in exclude_paths + ]) rm_args = ["-delete"] - all_args = find_args + rm_args + all_args = find_args + exclude_args + rm_args exec_result = repository_ctx.execute(all_args, quiet = True) if exec_result.return_code != 0: fail("Failed to remove files named {name} under {path}. stderr:\n{stderr}".format( diff --git a/swiftpkg/internal/swift_package.bzl b/swiftpkg/internal/swift_package.bzl index b89ff6eba..1446dae8f 100644 --- a/swiftpkg/internal/swift_package.bzl +++ b/swiftpkg/internal/swift_package.bzl @@ -44,6 +44,23 @@ def _remove_bazel_files(repository_ctx, directory): for file in files: repository_files.find_and_delete_files(repository_ctx, directory, file) +def _remove_modulemaps(repository_ctx, directory, targets): + repository_files.find_and_delete_files( + repository_ctx, + directory, + "module.modulemap", + exclude_paths = [ + # Framework modulemaps don't cause issues, and are needed + "**/*.framework/*", + ] + [ + # We need to leave any modulemaps that we are passing into + # `objc_library` + target.clang_src_info.modulemap_path + for target in targets + if target.clang_src_info and target.clang_src_info.modulemap_path + ], + ) + def _swift_package_impl(repository_ctx): directory = str(repository_ctx.path(".")) env = repo_rules.get_exec_env(repository_ctx) @@ -68,6 +85,9 @@ def _swift_package_impl(repository_ctx): # Remove the git stuff repository_ctx.delete(repository_ctx.path(".git")) + # Remove unused modulemaps to prevent module redefinition errors + _remove_modulemaps(repository_ctx, directory, pkg_ctx.pkg_info.targets) + # Return attributes that make this reproducible return _update_git_attrs(repository_ctx.attr, _ALL_ATTRS.keys(), update)