kotlin-hands-on / jvm-js-fullstack

https://play.kotlinlang.org/hands-on/Full%20Stack%20Web%20App%20with%20Kotlin%20Multiplatform/
121 stars 148 forks source link

`Unresolved reference` error compiling `Server.kt` #22

Closed bobhy closed 1 year ago

bobhy commented 2 years ago

I could not get the source code to compile from https://play.kotlinlang.org/hands-on/Full%20Stack%20Web%20App%20with%20Kotlin%20Multiplatform/03_A_Simple_API_Server.

I'm a total n00b on kotlin, gradle and jvm for that matter, and I'm climbing the steep side of the mountain trying to run the tutorial in VS Code rather than IDEA. So I'm not claiming the tutorial code has a bug, when used as instructed, but when I:

  1. clone the sample repo on the master branch.
  2. open project in VS Code (with java, gradle and kotlin extensions)
  3. run gradle 'application:run' task

... I get compilation errors

> Task :compileKotlinJvm FAILED
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (1, 16): Unresolved reference: application
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (2, 16): Unresolved reference: features
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (5, 16): Unresolved reference: request
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (6, 16): Unresolved reference: response
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (7, 16): Unresolved reference: routing
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (18, 9): Unresolved reference: routing
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server.kt: (19, 13): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text
e: /home/bobhy/src/kot/jvm-js-fullstack/src/jvmMain/kotlin/Server-old.kt: (20, 17): Unresolved reference: call

I fixed it by adjusting several of the ktor import paths, inserting .server (except for features, which I don't need yet and haven't figured out).

Here's the updated file, working for me, FWIW:

import io.ktor.server.application.*
//import io.ktor.features.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.serialization.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import org.litote.kmongo.*
import org.litote.kmongo.coroutine.*
import org.litote.kmongo.reactivestreams.KMongo
import com.mongodb.ConnectionString

fun main() {
    embeddedServer(Netty, port = 9090, host = "0.0.0.0") {
        routing {
            get("/hello") {
                call.respondText("Hello, API!")
            }
        }
    }.start(wait = true)

It compiles and even responds "Hello, API!".

Proceed on through this very informative tutorial...

mellamokb commented 2 years ago

I'm in the same boat - never used Kotlin before, decided to check it out and getting a first time user experience of a broken flagship demo app. And I'm using IntelliJ IDEA to boot.

When you get to step 3 you have additional problems with method needs to be allowMethod, and multiple more missing imports. Seems as though this whole project needs updated.

Edit: Interestingly, there seems to be another version of the Multiplatform tutorial here which has updated code that seems to fix the above issues. https://kotlinlang.org/docs/multiplatform-full-stack-app.html

SebastianAigner commented 1 year ago

Unfortunately, you hit us in the awkward phase of migrating different parts of the project a) to new Kotlin versions and b) to a new learning platform that was more aligned with the rest of the Kotlin documentation – apologies for the inconvenience. The tutorial text and project have since been updated and made consistent, so these issues should no longer be affecting you. If there are any other follow-up problems that occur as things are evolving, do let me know! Apologies again for any confusion that may have been caused by this!