rules_swift
rules_swift copied to clipboard
Repro changes for Swift/Objc Interop
This PR contains changes that reproduce the Swift Objc interop issue we observe in our codebase. I was able to make a repro within rules_swift example directory.
Notes :
- We pass "-fmodules" copt to all
objc_library
targets in our codebase. - I added a
Renderer
(swift_library
) which defines a public protocol that conforms toNSObjectProtocol
- Imported
Renderer
from existing swift_libraryPrinter
. - Ran command to build
main.m
:bazelisk build examples/apple/objc_interop:main -s --disk_cache=""
Expected outcome : Compilation succeeds
Actual outcome : Compilation failure
ERROR: /Users/ycho/Snapchat/Dev/rules_swift/examples/apple/objc_interop/BUILD:36:13: Compiling examples/apple/objc_interop/main.m failed: (Exit 1): wrapped_clang failed: error executing ObjcCompile command (from target //examples/apple/objc_interop:main) external/apple_support~1.13.0~apple_cc_configure_extension~local_config_apple_cc/wrapped_clang -target arm64-apple-macosx13.0 '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall ... (remaining 44 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
In file included from examples/apple/objc_interop/main.m:20:
bazel-out/darwin_arm64-fastbuild/bin/examples/apple/objc_interop/generated_header/Printer-Swift.h:279:9: fatal error: module 'Renderer' not found
@import Renderer;
~~~~~~~^~~~~~~~
1 error generated.
If I remove -fmodules
copt from objc_library, I get the following error :
ERROR: /Users/ycho/Snapchat/Dev/rules_swift/examples/apple/objc_interop/BUILD:36:13: Compiling examples/apple/objc_interop/main.m failed: (Exit 1): wrapped_clang failed: error executing ObjcCompile command (from target //examples/apple/objc_interop:main) external/apple_support~1.13.0~apple_cc_configure_extension~local_config_apple_cc/wrapped_clang -target arm64-apple-macosx13.0 '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall ... (remaining 42 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
In file included from examples/apple/objc_interop/main.m:20:
bazel-out/darwin_arm64-fastbuild/bin/examples/apple/objc_interop/generated_header/Printer-Swift.h:313:78: error: cannot find protocol declaration for 'SnapRenderProtocol'
@interface OIPrinter (SWIFT_EXTENSION(examples_apple_objc_interop_Printer)) <SnapRenderProtocol>
^
1 error generated.
@keith this is a repro I found while working on swift objc interop in our codebase. I wonder if this is a bug on swift generated header logic (-Swift.h) or we're using the interop in an unexpected way.
hrm. Unless something major changed I'm surprised others haven't reported this too. Seems to me like we need to be generating a pcm here that can be used by the objc compile.
I think folks are running into this today and it'd be great to document or fix if possible
From the Bazel slack I see this:
- https://bazelbuild.slack.com/archives/CD3QY5C2X/p1707922028801469
I also know rules_swift_package_manager had to work around this issue by creating custom modulemaps
If this is the same problem I'm thinking of, I was also running into this last week. I worked around it with a modified implementation of @thii's objc_module_map_config.bzl from rules_apple_line. I needed to customize it heavily to make it work for me, but this approach did help objc_library
with module map discovery.
Have we checked if the newly merged aspect_hint feature helps here?