trinhngocthuyen / cocoapods-spm

A CocoaPods plugin to add SPM dependencies to CocoaPods-based projects
MIT License
53 stars 10 forks source link

[Bug] Module 'BrazeUI' not found when importing SPM package in .m file #102

Closed kalub92 closed 1 day ago

kalub92 commented 5 months ago

What happened?

When adding Braze and several other Segment dependencies to my project, I can import then in a .swift file with no issue, but when attempting to import them in an .m file, I see an error: Module 'BrazeUI' not found

Screenshot 2024-06-20 at 10 34 05 AM

I see this same issue for SegmentBraze and SegmentBrazeUI as well.

I tinkered around with the .xcconfig and found that by adding the following lines to OTHER_CFLAGS I am able to successfully import those modules in Objective-C files as well (although I need to do this manually):

I've been able to reproduce this behavior in the example project on my fork: https://github.com/kalub92/cocoapods-spm/tree/kalub92%2Fbraze-modulemap-issue

⚠️ There is a separate issue involving a .o file being imported for a SamplePackageA dependency when it shouldn't be, so I will raise a new issue for that specifically.

CocoaPods environment

Stack

   CocoaPods : 1.14.3
        Ruby : ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [arm64-darwin21]
    RubyGems : 3.3.3
        Host : macOS 14.5 (23F79)
       Xcode : 15.4 (15F31d)
         Git : git version 2.44.0
Ruby lib dir : /Users/cstultz/.rvm/rubies/ruby-3.1.0/lib
Repositories : trunk - CDN - https://cdn.cocoapods.org/

Installation Source

Executable Path: /Users/cstultz/.rvm/gems/ruby-3.1.0/bin/pod

Plugins

cocoapods-compact-spec   : 0.0.3
cocoapods-deintegrate    : 1.0.5
cocoapods-plugins        : 1.0.0
cocoapods-search         : 1.0.1
cocoapods-spm            : 0.1.5
cocoapods-trunk          : 1.6.0
cocoapods-try            : 1.2.0
cocoapods-xcconfig-hooks : 0.0.1

Podfile

platform :ios, "16.0"
linkage = (ENV["LINKAGE"] || :static).to_sym
use_frameworks! :linkage => linkage
puts "Using linkage: #{linkage}"

@checksum = "dummy-checksum-to-prevent-merge-conflicts"

plugin "cocoapods-spm"
plugin "cocoapods-xcconfig-hooks"

def dont_use_macros?
  ENV["NO_MACROS"] == "true"
end

config_compact_spec(
  extra: ->(s) { s.ios.deployment_target = "16.0" }
)

config_cocoapods_spm(
  dont_prebuild_macros: true,
  default_macro_config: "debug"
)

def spm_pods
  spm_pkg "SamplePackageA",
          :git => "https://github.com/kalub92/SamplePackageA.git",
          :branch => "withHyphenatedDependency"
end

abstract_target 'Example' do

  spm_pods

  target "EX" do
    pod "Logger", :path => "LocalPods/Logger"
    # pod "Services", :path => "LocalPods/Services"

    spm_pkg "SnapKit",
            :url => "https://github.com/SnapKit/SnapKit.git",
            :version => "5.7.1",
            :products => ["SnapKit-Dynamic"]
    spm_pkg "SwiftUIX", :git => "https://github.com/SwiftUIX/SwiftUIX.git", :tag => "0.1.9"
    spm_pkg "SwiftyBeaver", :git => "https://github.com/SwiftyBeaver/SwiftyBeaver.git", :tag => "2.0.0"
    spm_pkg "opentelemetry-swift",
            :git => "https://github.com/open-telemetry/opentelemetry-swift.git",
            :branch => "main",
            :products => ["OpenTelemetrySdk"]
    spm_pkg "GoogleMaps",
            :git => "https://github.com/googlemaps/ios-maps-sdk.git",
            :version => "8.4.0",
            :products => ["GoogleMaps", "GoogleMapsBase", "GoogleMapsCore"]
    spm_pkg "DebugKit", :path => "LocalPackages/debug-kit"

    spm_pkg "braze-segment-swift",
        :url => "https://github.com/braze-inc/analytics-swift-braze.git",
        :version => "2.2.0",
        :products => [
          "SegmentBraze",
          "SegmentBrazeUI"
        ]

    spm_pkg "Segment",
        :url => "https://github.com/segmentio/analytics-swift.git",
        :version => "1.5.11"

    spm_pkg "SegmentFirebase",
        :url => "https://github.com/segment-integrations/analytics-swift-firebase.git",
        :version => "1.1.3"

  end

  target "EXTests" do
    pod "NetworkTestKit", :path => "LocalPods/NetworkTestKit"
    pod "AppTestKit", :path => "LocalPods/AppTestKit"
  end

end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings.delete "IPHONEOS_DEPLOYMENT_TARGET"
    end
  end

  if dont_use_macros?
    path = Pathname.new(".xcconfigs/__base__.xcconfig")
    path.parent.mkpath
    path.write("SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) NO_MACROS")
  end
end

Anything else?

No response

trinhngocthuyen commented 5 months ago

@kalub92 So everything works fine when using import BrazeUI in Swift code, I assume. Just that it doesn't work when using a Swift pkg module in Objective-C code, rite?

Actually, you were right about the modulemap in OTHER_CFLAGS. I think I need more time to make the fix this way because in some cases, Xcode does not dump the modulemap in GENERATED_MODULEMAP_DIR.

In the meantime, may I suggest adding ${GENERATED_MODULEMAP_DIR} to header search path so that those "<module>-Swift.h" are recognized. Then you can import the module with #import "<module>-Swift.h".

kalub92 commented 4 months ago

Hi @trinhngocthuyen, yes that's right that it works for Swift but not Obj-C. I was able to make a workaround for my use case, but any update on this on a plug-in level?

trinhngocthuyen commented 1 day ago

Sorry for the late update. The support for SPM package imports in ObjC has been done in #127. Kindly help check it out with the latest on main. Thank you!