Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.41k stars 620 forks source link

Inconsistent build success/failure #152

Closed Whathecode closed 6 years ago

Whathecode commented 6 years ago

I have just upgraded from v0.5.0 to v0.5.1 (including the new IDE plugin) and am encountering some weird problems.

Gradle builds do not seem to succeed consistently. Concretely:

  1. I compile and the build succeeds.
  2. I remove a base class, compile, and the build succeeds.
  3. I add the base class again (exactly the same source code as in step 1) and I get a build error:

e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation Cause: Non-serializable parent of serializable class TaskDescriptor must have no arg constructor File being compiled at position: file://C:/src/carp.protocols.core/src/main/dk/cachet/carp/protocols/domain/tasks/TaskDescriptor.kt The root cause was thrown at: SerializableCodegenImpl.kt:182 at org.jetbrains.kotlin.codegen.CompilationErrorHandler.lambda$static$0(CompilationErrorHandler.java:24) at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:74) at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generatePackage(CodegenFactory.kt:97) at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generateModule(CodegenFactory.kt:68) at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.doGenerateFiles(KotlinCodegenFacade.java:47) at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:39) ...

What can cause such inconsistent behavior to occur? Are the generated serializers perhaps cached somewhere? Could I clear this cache to see whether it is related?

sandwwraith commented 6 years ago

There's no any special caching mechanism in plugin, since it relies on compiler infrastructure for incremental builds. I can't say anything without reproducible sample, probably that was just a glitch in incremental compilation.

Whathecode commented 6 years ago

The following is a minimally working example to replicate. Interestingly, in order to replicate the following two classes need to be distributed across two files.

A base class with a parameter in the primary constructor with a default value is defined in one file:

@Serializable
abstract class BaseClass( val baseParameter: String = "" )

An extending class is defined in another file:

@Serializable
class ConcreteClass() : BaseClass()

Compile and see whether a compilation error occurs (similar to the one mentioned above):

e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation Cause: Non-serializable parent of serializable class ConcreteClass must have no arg constructor File being compiled at position: file://C:/src/.../SerializationCompilationError.kt The root cause was thrown at: SerializableCodegenImpl.kt:182 at org.jetbrains.kotlin.codegen.CompilationErrorHandler.lambda$static$0(CompilationErrorHandler.java:24) at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:74) at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generatePackage(CodegenFactory.kt:97) at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generateModule(CodegenFactory.kt:68) ...

In case it does not, provide a parameter in the BaseClass constructor for ConcreteClass and compile again.

@Serializable
class ConcreteClass() : BaseClass( "Now it will not compile" )

After compilation fails, compiling a second time succeeds.

Whathecode commented 6 years ago

Have since converted this project to a multiplatform project and upgraded to Kotlin 1.2.61 and kotlinx.serialization 0.6.1.

This problem (as described in my previous posted steps to reproduce) no longer occurs. I will therefore abandon this issue.

serebit commented 6 years ago

Still happens to me with Kotlin 1.3.0-rc-51 and the bundled serialization plugin in the IDE. The Gradle kotlin compiler does not have this issue.

sandwwraith commented 6 years ago

@serebit What do you mean by 'Gradle kotlin compiler'? Compiler alone without serialization?

Bundled in IDE plugin can't launch build with serialization yet, build must be delegated to Gradle.

serebit commented 6 years ago

I was mistaken, I still had the old kotlinx-serialization plugin installed. Disregard.

On Fri, Sep 21, 2018 at 5:00 AM, Leonid Startsev notifications@github.com wrote:

@serebit What do you mean by 'Gradle kotlin compiler'? Compiler alone without serialization?

Bundled in IDE plugin can't launch build with serialization yet, build must be delegated to Gradle.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

mowhammadrezaa commented 6 years ago

I downgraded Kotlin from 1.3.0 to 1.2.51. IT WORKED.

pdvrieze commented 6 years ago

@mowhammadrezaa What worked? Keep in mind that there has been a lot of development on the serialization support (including the compiler bit). There are various problems with 1.2.51 that have been resolved including invalid code being compiled. The inconsistent success/failure is due to the compiler still generating the class file despite the compilation error and the incremental compilation deciding there is a class file so recompilation is not needed. The error is the fact that it succeeds the second time, not that it fails the first time. There is a bug in your code, the generated serializer needs to be able to construct your type (it will generate a constructor if needed), but to do so it needs to be able to serialize the parent type as well - it can only do this with a no-arg constructor or one generated by the plugin (as it knows the semantics).

I agree that the error messages created are not ideal and they should be compiler errors rather than exceptions in the compiler (they are bugs in your code rather than bugs in the compiler), but the end result is the same.

mowhammadrezaa commented 6 years ago

@pdvrieze my problem has solved. I was getting the error about codgen in Kotlin. And when I downgraded Kotlin the problem disappeared. However there were some problems relating to my code but at least with version 1.3.0 of Kotlin it was not pointing where is my problem in code even. Just it, if I'm wrong let me know.

pdvrieze commented 6 years ago

@mowhammadrezaa I'm more trying to get what particular code works in 1.2.51 but not in 1.3.0 and whether that is a regression in codegen/regression in error messages (seems to be) or a code pattern that was invalid and no longer compiles (but worked due to a compiler bug).

sandwwraith commented 6 years ago

@mowhammadrezaa Your problem doesn't sounds like about this ticket. If you want to share any regression while compiling the code with serialization, please create a new issue.

bangtiray commented 5 years ago

ext.kotlin_version = '1.3.0' ext.anko_version = '0.10.7-rc13'

work

tonifirnandes commented 5 years ago

ext.kotlin_version = '1.3.0' ext.anko_version = '0.10.7-rc13'

work

Actually my problem is different with this thread, but your comment give me an idea to solve my problem about error occuring after i upgraded my kotlin version from 1.2.71 to 1.3.10 and i upgrade the anko version from 0.10.5 to 0.10.8 and updated my minSdkVersion from 14 to 15.

Thanks a lot.

mochadwi commented 5 years ago

The error still occurs for me.

Kotlin v1.3.20 Koltin Serializable runtime v0.10.0 @sandwwraith

0xBADDCAFE commented 5 years ago

The error still occurs in below:

Kotlin:1.3.21-1.3.30 kotlinx-serialization-runtime:0.11.0

but below works:

Kotlin:1.3.11 kotlinx-serialization-runtime:0.11.0

0xBADDCAFE commented 5 years ago

Seems work at

Kotlin:1.3.31 kotlinx-serialization-runtime:0.11.0