Search code examples
kotlinintellij-ideaslf4jintellij-14ktor

IntelliJ missing reference to slf4j in new Ktor project


I started a new Ktor project through their IntelliJ plugin and everything is compiling and running fine with Gradle. However in IntelliJ everything that references the slf4j logger is having a Unresolved reference: ... error.

My import and setting log leve looks like this (sorry my reputation is not high enough to post images):

Import statement

References in code

The project is generated with the following dependencies

// gradle.properties
ktor_version=1.4.0
logback_version=1.2.1

// build.gradle.kts
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
    implementation("io.ktor:ktor-server-netty:$ktor_version")
    implementation("ch.qos.logback:logback-classic:$logback_version")
    implementation("io.ktor:ktor-server-core:$ktor_version")
    implementation("io.ktor:ktor-auth:$ktor_version")
    implementation("io.ktor:ktor-auth-jwt:$ktor_version")
    implementation("io.ktor:ktor-jackson:$ktor_version")
    testImplementation("io.ktor:ktor-server-tests:$ktor_version")
}

I have tried to add org.slf4j:slf4j-nop:1.7.30 to my dependencies without any luck.

So my questions is, has anyone seen this error before and know if there is a dependency or setting i could change? It is annoying to not have any code completion and errors scattered around the IDE even though it compiles and run fine.

The full application code generated with the Ktor plugin is below if needed:

package com.example

import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.features.*
import org.slf4j.event.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.auth.*
import com.fasterxml.jackson.databind.*
import io.ktor.jackson.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    install(CallLogging) {
        level = Level.INFO
        filter { call -> call.request.path().startsWith("/") }
    }

    install(Authentication) {
    }

    install(ContentNegotiation) {
        jackson {
            enable(SerializationFeature.INDENT_OUTPUT)
        }
    }

    routing {
        get("/") {
            call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
        }

        get("/json/jackson") {
            call.respond(mapOf("hello" to "world"))
        }
    }
}

Solution

  • This was a caching issue with Gradle and IntelliJ, probably caused from other project dependencies. To IntelliJ the slf4j-api library was empty. Removing the global Gradle cache with rm -rf ~/.gradle/caches and re-download the dependencies solved the issue for me.