rules_swift icon indicating copy to clipboard operation
rules_swift copied to clipboard

Repro changes for Swift/Objc Interop

Open yongjincho92 opened this issue 11 months ago • 5 comments

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 to NSObjectProtocol
  • Imported Renderer from existing swift_library Printer.
  • 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.

yongjincho92 avatar Feb 26 '24 21:02 yongjincho92

@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.

yongjincho92 avatar Feb 26 '24 21:02 yongjincho92

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.

keith avatar Mar 04 '24 18:03 keith

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

luispadron avatar Mar 04 '24 22:03 luispadron

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.

aaronsky avatar Mar 04 '24 22:03 aaronsky

Have we checked if the newly merged aspect_hint feature helps here?

luispadron avatar Apr 15 '24 17:04 luispadron