Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SerializationException: Serializer for class *** is not found in IR backend for Kotlin JS #2202

Closed
Coneys opened this issue Feb 20, 2023 · 10 comments

Comments

@Coneys
Copy link

Coneys commented Feb 20, 2023

Describe the bug

Any serialization done on Kotlin JS IR backend with kotlinx.serialization library fails with error:
SerializationException: Serializer for class *** is not found
Serialization works fine on Legacy backend, but we cannot use it because other thing works only on IR backend.

To Reproduce

@Serializable
class Abc(val someValue: String)

class SerializationTest {

    @Test
    fun test() {
        val abc = Abc("Test value")

        val string = Json.Default.encodeToString(abc)
        println(string)
    }
}

Expected behavior

Serialize object to string same on Legacy and IR backends

Environment

  • Kotlin version: 1.7.21
  • Library version: 1.3.2 but tested on newer, fails as well
  • Kotlin platforms: JS
  • Gradle version: 7.5
@sandwwraith
Copy link
Member

I've just checked and this code works fine (on 1.8.0/1.5.0-RC, but it should work on 1.7.21 as well, since we have a lot of tests for that). Are you sure that you have serialization plugin applied to your module? Runtime dependency is not enough.

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

Yes, I am more than sure. Also everything works on Legacy backend so there is no way anything is wrong with setup.
I am not the only one with this issue:
#1116 (comment)

@sandwwraith
Copy link
Member

Do you think it is possible for you to create a separate project with reproducer? It seems that the problem is with the project setup, not the plugin. Maybe 1.7.+ version cause problems (though unlikely)

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

Sure, but if its most likely not bug on your side, then I will try to reduce my build to minimum first. Maybe i left some weird config somewhere, which causes issues for IR backend. If I won't find anything, then I will create separate project for this

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

I did create Multiplatform and JS version of simple project and in both cases it worked in Kotlin 1.7.21. Which means I have no clue what causes issue on my project. I will paste whole error, maybe can you provide some hints where to look? This project is very big, uses multiple multiplatform libraries and multiple multiplatform modules so its really hard to just remove something and check if it works.

SerializationException: Serializer for class 'Abc' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
SerializationException: Serializer for class 'Abc' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
	at <global>.platformSpecificSerializerNotRegistered(/var/folders/55/5dq3m0_53rn618js9lk39sjh0000gn/T/_karma_webpack_222394/commons.js:38668)
	at <global>.serializer(/var/folders/55/5dq3m0_53rn618js9lk39sjh0000gn/T/_karma_webpack_222394/commons.js:32266)
	at SerializationTest.test_w6j16n(/var/folders/55/5dq3m0_53rn618js9lk39sjh0000gn/T/_karma_webpack_222394/commons.js:210)
	at <global>.test_fun$SerializationTest_test_fun$test_test_fun_1ed98j(/var/folders/55/5dq3m0_53rn618js9lk39sjh0000gn/T/_karma_webpack_222394/commons.js:227)
	at Context.<anonymous>(/var/folders/55/5dq3m0_53rn618js9lk39sjh0000gn/T/_karma_webpack_222394/commons.js:45189)

@sandwwraith
Copy link
Member

Unfortunately it's not possible to say anything from the stacktrace alone. I suggest to follow these steps:

  1. Build a single module with gradle's -i or -debug flag. (e.g. ./gradlew -i :myModule:compileKotlinJs). It will print the command line arguments passed to the Kotlin compiler. Check that kotlinx-serialization-compiler plugin jar is present in the -Xplugin section.
  2. Check the compiled files in the build folder and see if JS file contains Abc.$serializer class. (see in your sample project how it should look like)
  3. If it does contain it, that the plugin works correctly and your pipeline incorrectly strips code later (maybe due to DCE/webpack misconfiguration or something else)

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

Okey I found the issue, and its quite weird.
Issue was caused by kotlin js wrappers for react:

implementation("org.jetbrains.kotlin-wrappers:kotlin-react:18.2.0-pre.500")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:18.2.0-pre.500")

I did check it on my project and on newly created multiplatform project. Adding those lines make serialization plugin to break.

Would you like me to provide separate project with this bug?

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

Maybe it is something to do with this react version be compiled with kotlin 1.8.0 and projects uses 1.7.x?

@sandwwraith
Copy link
Member

It may be. Try lower react wrapper version.
I recall we had some problems with react wrappers, but I can not remember the details unfortunately.

@Coneys
Copy link
Author

Coneys commented Feb 21, 2023

I cannot lower react wrapper version, because older version has other bugs :( I updated project to Kotlin 1.8. It works, but probably we will have to drop Kotlin JS for a while, because its not that simple to update everything to 1.8. Anyway, problem was solved

@Coneys Coneys closed this as completed Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants