Failed to build module from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced
Asked Answered
S

4

15

When creating an xcframework like this

xcodebuild archive -project endiosOne-iOS.xcodeproj -scheme EOFoundation -destination="iOS" -archivePath /tmp/xcf/ios.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild archive -project endiosOne-iOS.xcodeproj -scheme EOFoundation -destination="iOS Simulator" -archivePath /tmp/xcf/iossimulator.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild -create-xcframework -framework /tmp/xcf/ios.xcarchive/Products/Library/Frameworks/EOFoundation.framework -framework /tmp/xcf/iossimulator.xcarchive/Products/Library/Frameworks/EOFoundation.framework -output /tmp/xcf/EOFoundation.xcframework

Then adding the framework to an existing project or a new project we get this error where we import the framework.

Failed to build module 'EOFoundation' from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced

Using xcode 12.1, also tried with Xcode 11. Tried with new UIKit application and new SwiftUI only application

We get the following UIKit Errors UIKit Errors

I cant fix the UIkit errors as its in the UIKit: Unfortunately apple does not give us a lot of information on how to fix this.

How can I fix this so I use a xcframework that I have generated in another project?

Saccharoid answered 7/12, 2020 at 11:55 Comment(2)
Did you find any solution?Riddick
Yes I did find a solution. Will post when I’m back at the office again. It was something to do with a public extension for uitextdelegate. I made the method private and it solved the issue.Saccharoid
S
0

The solution was to remove public methods in extension UITextView: UITextViewDelegate {

} and simply add it to a subclass of UITextView that was fileprivate.

Saccharoid answered 3/1, 2021 at 14:41 Comment(2)
Hi, I am also facing the same issue but in my case, I am not getting any UIkit error or any other errors except "Failed to build module 'FrameworkName' from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced". If possible can you suggest any workaround or what i doing wrong?Orthodontia
I removed the delegate method and rather subclasses UITextView this fixed the issue for me. Going through my code and commenting out UIKit code is how I discovered the issue. If you don't get UIKit errors then this is not the solution for you.Saccharoid
M
9

I posted a feedback request to Apple for this issue and they suggested the following:

We can tell you that there is one known issue that fits your description: If your module is called “MyModuleName” and it also declares or imports a type called “MyModuleName”, names in the file that are supposed to refer to the module will be resolved to the type instead, causing errors when you try to import it. The Swift open source project’s bug tracker describes this bug and workarounds for it in a public article at https://bugs.swift.org/browse/SR-14195

Sure enough, the type I was exporting from my framework was named exactly as the module I was importing it from. Renaming the type resolved the problem for me.

Marin answered 9/3, 2021 at 16:50 Comment(1)
Yes indeed, I change the framework's product name and the error went away 👌Gestation
V
2

In my case, a precompiled XCFramework I was relying on was not recompiled when I changed the iOS deployment target. I recompiled the XCFramework and it fixed the issue.

Villareal answered 14/9, 2021 at 0:58 Comment(1)
changing the target worked for meThermic
S
0

The solution was to remove public methods in extension UITextView: UITextViewDelegate {

} and simply add it to a subclass of UITextView that was fileprivate.

Saccharoid answered 3/1, 2021 at 14:41 Comment(2)
Hi, I am also facing the same issue but in my case, I am not getting any UIkit error or any other errors except "Failed to build module 'FrameworkName' from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced". If possible can you suggest any workaround or what i doing wrong?Orthodontia
I removed the delegate method and rather subclasses UITextView this fixed the issue for me. Going through my code and commenting out UIKit code is how I discovered the issue. If you don't get UIKit errors then this is not the solution for you.Saccharoid
L
0

I created a little script that works around the issue by fixing the swiftinterface files.

  • Replace FrameworkName
  • Add or remove platforms (currently it generates ios device and ios simulator)

    #!/bin/bash
    
    FrameworkName="MyLibAndClassName"
    
    rm -rf build/
    
    xcodebuild archive -scheme "$FrameworkName" \
        -configuration Debug -destination 'generic/platform=iOS' \
        -archivePath "./build/$FrameworkName.framework-iphoneos.xcarchive" \
        SKIP_INSTALL=NO \
        BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
    
    xcodebuild archive -scheme "$FrameworkName" \
        -configuration Debug -destination 'generic/platform=iOS Simulator' \
        -archivePath "./build/$FrameworkName.framework-iphonesimulator.xcarchive" \
        SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
    
    # Fix https://bugs.swift.org/browse/SR-14195 (caused by https://bugs.swift.org/browse/SR-898)
    pattern="./build/$FrameworkName.framework-iphoneos.xcarchive/Products/Library/Frameworks/$FrameworkName.framework/Modules/$FrameworkName.swiftmodule/*.swiftinterface"
    grep -rli "$FrameworkName.$FrameworkName" $pattern \
         | xargs sed -i '' "s,$FrameworkName.$FrameworkName,$FrameworkName,g"
    # end fix
    
    xcodebuild -create-xcframework \
        -framework "./build/$FrameworkName.framework-iphoneos.xcarchive/Products/Library/Frameworks/$FrameworkName.framework" \
        -framework "./build/$FrameworkName.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/$FrameworkName.framework" \
        -output "./build/$FrameworkName.xcframework"
    
    # Wait for process completion and verify result
    pid=$!      
    wait $pid
    echo "Process with PID $pid has finished with Exit status: $?"
    [[ ! -d "./build/$FrameworkName.xcframework/" ]] && { 
        msg="[ERROR] expected ./build/$FrameworkName.xcframework/ to exist"; echo -e $msg
        exit 1
    }
Leopardi answered 20/2, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.