ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
13.09k stars 1.07k forks source link

Code autoreload not working for me #975

Closed realmhamdy closed 3 years ago

realmhamdy commented 5 years ago

Ktor Version

1.1.3

Ktor Engine Used(client or server and name)

EngineMain I guess?

JVM Version, Operating System and Relevant Context

JDK 1.8. Windows 10

Feedback

Okay I have a new project generated using the Ktor IDEA plugin. I'm just trying to make autoreload work (which should by default?). I have only one Kotlin file in src/Application.kt. My application.conf looks like this:

ktor {
    deployment {
        port = 8080
        port = ${?PORT}
        autoreload = true
        watch = [ MyProjectFolder ]
    }
    application {
        modules = [ com.company.ApplicationKt.module ]
    }
}

The issue is, when I change Application.kt the project doesn't reload and the changes are not reflected in the browser. I see this in the run output:

2019-02-25 11:01:46.894 [main] DEBUG Application - Watching F:\Projects\MyProjectFolder\out\production\classes for changes.
2019-02-25 11:01:46.895 [main] DEBUG Application - Watching F:\Projects\MyProjectFolder\out\production\classes\com for changes.
...

I also tried com.company instead of MyProjectFolder in the watch list but that one is even worse. The run output shows autoreload is disabled.

realmhamdy commented 5 years ago

I also think that having to use the project folder name as an autoreload setting is not reliable because it can differ from a developer to the next as it's not checked out in source control.

Aberrantfox commented 5 years ago

Same here, I'd also say the documentation on this is just very not clear unfortunately, so it's very hard to know where to go with it... especially on maven - I had to swap to gradle to get it working at all but now it seems to have just stopped working altogether

Ykid commented 5 years ago

I believe I found the cause of this issue.

if I simply run with Intellij, compiled classes go to <ProjectFolder>/out/production/. reference.

if you watch source changes by running gradle -t watch, the sources are compiled to <ProjectFolder>/build/classes/. These two paths are different and ktor application is not watching the class files <ProjectFolder>/build/classes/.

To solve this issue, one should avoid using intellij to run the ktor application. Use gradle -t watch to watch file changes and use gradle run to start the application.

Hope this helps

Reizinixc commented 5 years ago

Thank you very much @Ykid for the suggestion.

In case you chose Gradle and build the application from IntelliJ, this is what I did to make autoreload work.

IntelliJ Gradle settings screen accessed from File, Settings, choose 'Build, Execution, Deployment', 'Build Tools' and Gradle

Not sure about the others, but my default value is 'IntelliJ IDEA', so it didn't work before changing the configuration.

lindenquan commented 5 years ago

I set the Gradle JVM from JDK 11 to JDK 8, then it works fine for me now. I'm using Jetty

image

Aditya94A commented 4 years ago

How to use gradle run? The default project gives me no gradle executable (though there is a gradle.bat but I'm on MacOS)

Using gradlew does not work

@Ykid how did you get this to work?

Aditya94A commented 4 years ago

@cy6erGn0m @orangy @e5l This should be super high priority. Since autoreload never works, it really worsens the development experience 😿

iamdev42 commented 4 years ago

@AdityaAnand1 from intelliJ simply create new run configuration and select gradle. For task put buildand for arguments put -t. After that, run this task and while running it, also run your Ktor project. Make sure you are using Java 8.

Terkwood commented 4 years ago

This is a tough one! I used a combination of the advice posted above in order to get mine working. Thanks to the various contributors who helped in this thread.

Configured IntelliJ with a Run/Debug config for this project. Note the use of installDist task (not build):

intellij_run_debug_config

I started that task in IntelliJ. Then in the command line did the following:

gradle run

I am using JDK 8, which seems important based on the advice given above, and the docs provided by the team:

postbill_sdk

Incidentally I have the gradle watch config in my gradle.properties. Might not be related, but at this point I'm afraid to change it :wink:

org.gradle.unsafe.watch-fs=true

I'm using Gradle 6.5.

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

red-avtovo commented 4 years ago

To the person, who will go through this thread and wouldn't understand how to fix the subj, official documentation describes it clearly now: https://ktor.io/docs/autoreload.html?topic=open-source#recompiling-automatically-on-source-changes

joaoeudes7 commented 3 years ago

It needs a lot of settings to allow this auto reload, because? (And after all it still didn't work here :/) Because not works with jdk11?

Vert.x for example does not need to run with a parameter to hot reload, and is defined in initial start, this implementation would take a long time?

e5l commented 3 years ago

Hey @joaoeudes7, could you check https://ktor.io/docs/development-mode.html?

baughmann commented 2 years ago

Those of us running the Flatpack version do not have access to the JAVA_HOME and thus cannot run the terminal commands through the integrated terminal.

The (mildly annoying) way around this is two have two different run configurations. One is the standard Ktor run/debug config, the other is one that watches the directory. Here's what I did:

Screenshot from 2022-12-04 19-35-14

I will say, I love Intellij and I am loving Ktor so far, but it is kinda odd that auto reload doesn't just work "automagically" when the language, IDE, and framework are are "controlled" by the same company (at least to some extent). I'm sure it'll get there with time, I know Jetbrains has a lot of projects in the oven.

alexoooo commented 1 year ago

example of auto-reload on backend/frontend with Ktor+React with Kotlin 1.8.0 and new IR: https://github.com/alexoooo/sample-ktor-react