Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values
https://flight.school/books/codable
MIT License
1.29k stars 133 forks source link

Getting error "Undefined symbols for architecture arm64" on production, Xcode 12.2.1 #23

Closed neerajkhede16 closed 5 years ago

neerajkhede16 commented 5 years ago

I am getting following error while using library:

Undefined symbols for architecture arm64: "protocol witness table for MyTestFramework.AnyCodable : MyTestFramework._AnyEncodable in MyTestFramework", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o "(extension in MyTestFramework):MyTestFramework._AnyEncodable.init(floatLiteral: Swift.Double) -> A", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o "(extension in MyTestFramework):MyTestFramework._AnyEncodable.init(stringLiteral: Swift.String) -> A", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o "(extension in MyTestFramework):MyTestFramework._AnyEncodable.init(booleanLiteral: Swift.Bool) -> A", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o "(extension in MyTestFramework):MyTestFramework._AnyEncodable.init(integerLiteral: Swift.Int) -> A", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o "(extension in MyTestFramework):MyTestFramework._AnyEncodable.init(nilLiteral: ()) -> A", referenced from: function signature specialization <Arg[0] = Dead> of TestAnyEncodable.ViewController.test() -> () in ViewController.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Issue is reproducible

  1. when write framework or cocoapod library and include AnyCodable, AnyDecodable and AnyEncodable into it. and use the same in another test app.
  2. on Xcode 10.2 and above (No issue on on Xcode 10.1.).
  3. on production environment only (on dev it works fine).

You can download the Sample framework and test app where the issue is reproducible from here

mattt commented 5 years ago

I'm sorry you're encountering this issue, @neerajkhede16. I looked at your sample, but there are too many external factors in the complex project configuration to determine whether AnyCodable is the cause of any problems you're having.

Have you tried deleting ~/Library/Developer/Xcode/DerivedData or removing Pods and re-running pod install?

neerajkhede16 commented 5 years ago

Yes, I tried already tried deleting DerivedData and/or removing Pods before reporting the issue, It is working on Xcode 12.1 version but not on 12.2.1. Other developers in team also facing the same issue.

mattt commented 5 years ago

Thanks for following up. Which version of the library are you using and how are you installing it?

neerajkhede16 commented 5 years ago

I was using version number 0.2.2, and I added 3 files AnyCodable, AnyDecodable and AnyEncodable into my framework.

mattt commented 5 years ago

Searching for that error message, it looks like one possible resolution is to add an import statement: https://github.com/ReactiveX/RxSwift/issues/1583

Looking through the attached sample, I think that might work. Can you give that a try?

neerajkhede16 commented 5 years ago

Sorry, not getting your suggestion. we can import module only in the app, how can we import any structure file?

neerajkhede16 commented 5 years ago

could you please try that fix in my sample code and share it with me?

mattt commented 5 years ago

The error you're seeing has to do with the linker not being able to find those referenced types. Whichever module you're including AnyCodable files, you're very likely missing an import statement for that module somewhere.

At this point, I don't have any reason to believe that the problem you're seeing is specific to AnyCodable; if you continue to have issues, I'd recommend looking for answers on Stack Overflow or another community forum.

neeraj110600 commented 5 years ago

Steps are very easy to reproduce, you can also do it, To believe on me you just need to follow below steps:

  1. Use Xcode 10.2.1 (latest).
  2. Create a framework and include AnyCodable, AnyDecodable and AnyEncodable files into it.
  3. Create an app and integrate that framework into it.
  4. Instantiate AnyCodable structure in your sample app.
  5. Create an archive of the sample app.

You will find error as mentioned above. In debug mode it works fine.

minacle commented 5 years ago

Which package manager did you use?

neerajkhede16 commented 5 years ago

To reproduce the issue I created framework from Xcode, by selecting "Cocoa touch framework" and added to sample app directly by going to Target >> Build Phases >> Link binary with library. Instantiate AnyCodable structure in the sample app and make archive will show you errors.

Initially I was facing issue while creating framework and creating cocoapods library, and consumed the library in production created issue for me, so I tried the same on sample app (mentioned in first line) and found same issue found.

Issue can be seen in both the cases.

minacle commented 5 years ago

This is a little bit weird but understandable.

I think that Swift 5.0 toolchain is removing (or hiding) internal names for some reason.

_AnyEncodable is an internal protocol and the mangled names are removed (or hided) because of the above reason.

You can avoid this issue by patch like this:

--- A/Sources/AnyCodable/AnyEncodable.swift
+++ B/Sources/AnyCodable/AnyEncodable.swift
@@ -205,7 +205,41 @@
 extension AnyEncodable: ExpressibleByArrayLiteral {}
 extension AnyEncodable: ExpressibleByDictionaryLiteral {}

-extension _AnyEncodable {
+extension AnyEncodable {
+    public init(nilLiteral _: ()) {
+        self.init(nil as Any?)
+    }
+
+    public init(booleanLiteral value: Bool) {
+        self.init(value)
+    }
+
+    public init(integerLiteral value: Int) {
+        self.init(value)
+    }
+
+    public init(floatLiteral value: Double) {
+        self.init(value)
+    }
+
+    public init(extendedGraphemeClusterLiteral value: String) {
+        self.init(value)
+    }
+
+    public init(stringLiteral value: String) {
+        self.init(value)
+    }
+
+    public init(arrayLiteral elements: Any...) {
+        self.init(elements)
+    }
+
+    public init(dictionaryLiteral elements: (AnyHashable, Any)...) {
+        self.init([AnyHashable: Any](elements, uniquingKeysWith: { first, _ in first }))
+    }
+}
+
+extension AnyCodable {
     public init(nilLiteral _: ()) {
         self.init(nil as Any?)
     }

Good luck.

neerajkhede16 commented 5 years ago

This patch works. Are you not going to release another version after including these changes.

minacle commented 5 years ago

Are you not going to release another version after including these changes.

I will open the pull request which associated with this issue, but Iā€™m currently finding a real reason to resolve this issue perfectly.

So, please wait for a few days! šŸ™‡ā€ā™€ļø

neeraj110600 commented 5 years ago

@minacle Do you have any update on this topic?

minacle commented 5 years ago

I wrote a pull request (#24) and currently waiting to be merged.

minacle commented 5 years ago

@neeraj110600 @neerajkhede16 Good news! The changes are merged and you can try v0.2.3 now!