Skip to content

Bug: Imports are not added to generated code when referencing a class in the parent package #90

@dronda-t

Description

@dronda-t

Hi! I ran into an issue where if a class references a class that is in the parent package, imports are not added.

For example:
Screenshot 2023-11-11 at 16 13 26

package me.test.data.inner

import at.kopyk.CopyExtensions
import me.test.data.Outer

@CopyExtensions
data class Inner(
    val outer: Outer
)
package me.test.data

import at.kopyk.CopyExtensions

@CopyExtensions
data class Outer(
    val test: String
)

The generated code for inner will look like this:

package me.test.`data`.`inner`

import kotlin.DslMarker
import kotlin.Unit
import me.test.`data`.`Outer$Mutable`
import me.test.`data`.`inner`.Inner
import me.test.`data`.`inner`.`Inner$DslMarker`
import me.test.`data`.`inner`.`Inner$Mutable`

private val generatedByKopyKat: Unit = Unit

@DslMarker
public annotation class `Inner$DslMarker`

@`Inner$DslMarker`
public class `Inner$Mutable`(
  public var outer: `Outer$Mutable`,
  public val old: Inner,
)

public fun `Inner$Mutable`.freeze(): Inner = me.test.`data`.`inner`.Inner(outer = outer.freeze())

public fun Inner.toMutable(): `Inner$Mutable` = me.test.`data`.`inner`.`Inner$Mutable`(old = this,
    outer = outer.toMutable())

public inline fun Inner.copy(block: `Inner$Mutable`.() -> Unit): Inner =
    toMutable().apply(block).freeze()

And fail with the error:

e: file:///kopy-kt-missing-import/build/generated/ksp/main/kotlin/me/test/data/inner/me.test.%60data%60.%60inner%60.Inner$Mutable.kt:21:89 Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun `Inner$Mutable`.freeze(): Inner defined in me.test.data.inner in file me.test.`data`.`inner`.Inner$Mutable.kt
e: file:///kopy-kt-missing-import/build/generated/ksp/main/kotlin/me/test/data/inner/me.test.%60data%60.%60inner%60.Inner$Mutable.kt:24:19 Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun Inner.toMutable(): `Inner$Mutable` defined in me.test.data.inner in file me.test.`data`.`inner`.Inner$Mutable.kt

In order to fix this the following imports need to be added to the inner generated file:

import me.test.data.freeze
import me.test.data.toMutable

I created a reproducer at https://github.com/dronda-t/kopy-kt-missing-import if you want to try for yourself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions