kotlin-language-server icon indicating copy to clipboard operation
kotlin-language-server copied to clipboard

Unresolved reference: test, when importing kotlin.test.Test

Open psyill opened this issue 5 years ago • 1 comments

A file as simple as this: import kotlin.test.Test produces the error "kotlin: Unresolved reference: test".

This happens when editing outside any project, i.e. there are no e.g. Gradle files to analyze for dependencies. As mentioned in #198, this should work as of #154.

It also happens when starting to edit the file from the root of a Gradle project with the following build.gradle.kts:

plugins {
    kotlin("jvm") version "1.4.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("test"))
    implementation(kotlin("test-junit"))
}

val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
compileKotlin.kotlinOptions {
    jvmTarget = "1.8"
}

sourceSets.main {
    java.srcDir("src")
}

N.b. gradle build builds this without any complaints, given that the code is placed in the src directory.

Both kotlin-test.jar and kotlin-test-junit.jar are located in the lib directory within my Kotlin installation (thus, if the server is able to find the kotlinc binary, it should be able to find the libraries too.

For the same setups, with or without the Gradle project, the language server has no issues with import kotlin.reflect.KClass showing that resolving things located in the standard library works.

By the way, I'm not using the Snap installation of Kotlin, as I can see how that could easily confuse the language server (Snap sets up kotlinc to be a soft link to the Snap binary itself...). Instead, I've downloaded the zip and added a soft link to kotlinc in my PATH.

Tested version of the server: 0.7.0-14-gd70cf6e

Kotlin version: kotlinc-jvm 1.4.10 (JRE 11.0.8+10-post-Ubuntu-0ubuntu118.04.1) LSP client: LanguageClient-neovim

psyill avatar Oct 08 '20 08:10 psyill

I'm willing to try to fix the issue, however, I would need some guidance with how the process of resolving dependencies work. I'm not sure about the order in which the resolvers kick in: I tried patching BackupClassPathResolver.kt to add the missing dependencies (I know it's not the proper fix for this issue, I just wanted to try it out) but to no avail.

psyill avatar Oct 08 '20 09:10 psyill