vinivendra / Gryphon

The Swift to Kotlin translator.
https://vinivendra.github.io/Gryphon/
Other
609 stars 46 forks source link

Unable to convert files that reference classes defined in a private pod #115

Open dylandrones opened 2 years ago

dylandrones commented 2 years ago

How can I convert swift files that have references to classes defined in a private pod repo, using Gryphon?

What happens: I get the following error message when trying to build the Gryphon project from my Xcode workspace. SourceKit failed to get an expression's type. Try running gryphon init again.

What I expected to see: I expected the file to be able to converted. Gryphon succeeds in converting the entire file when it references CGPoint just fine.

Why I think the error is related to a private Cocoapod repo: After some investigation, it appears this error happens when I try to build the Gryphon project with a variable declaration that is defined in a private pod. When I remove the declaration, the file is able to be converted.

In my private cocoapod Framework_A, I have the following defined:

public class MyPoint{
var x : Double?
var y : Double?

public init(withX xInit:Double, withY yInit:Double){
x = xInit
y = yInit
}

In my Xcode workspace (that has Gryphon initialized in it, and that has Framework_A installed via the PodFile), I call MyPoint and the error occurs.

The --verbose output points to the variable declaration as the error:

let loc = MyPoint(withX: 0.0, withY: 0.0)
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Conclusion Do I need to add my private pod repo as an input source somehow? Any information would be greatly appreciated.

Environment Gryphon version 0.18.1, using the Swift 5.5 parser Xcode: 13.2.1 MacOS: Monterey 12.1

vinivendra commented 2 years ago

Hey @dylandrones, yeah you guessed it. SourceKit needs to know every file necessary to compile your Swift code. If a declaration that you use is in another package (like a pod repo), SourceKit doesn't have enough type information and Gryphon can't go on with the compilation.

There are a few things you can try here:

  1. Create a stub MyPoint declaration to be used only with Gryphon translations, but that's ignored in your actual app. Since SourceKit only needs the code to compile correctly, the new declaration doesn't have to work - it just has to contain the type, methods, etc that you're using. Add the file to Gryphon's xcfilelist but don't add it to Xcode, and everything should be OK.
  2. Include your pod repo's files when calling Gryphon. You technically only need to include files that are enough for everything to compile correctly, but depending on your library, that could include all of its files.

That said, this error message could be a lot better than it is. Can you give me more information about it? Did it show up as an error on the line that references MyPoint?

dylandrones commented 2 years ago

Hello,

I would like to perform option 2, but not sure how to add the "pod repo's files when calling Gryphon".

Since there will be more the one file from the framework, can I add a reference to the framework folder?

Is there a tutorial somewhere on how to do that?


The error in more detail:

  1. After building the "gryphon target" I see this error:

    InLineError
  2. In the Issue Navigator, I can select "show in log"

    IssueNavigator
  3. And in the log, I can see this:

    GryphonErrorLog

MyPoint is a class defined in a private pod repo called "MyFramework". The app builds in swift successfully.