Ktor: Module function cannot be found in 1.2.0

Created on 15 May 2019  路  19Comments  路  Source: ktorio/ktor

Ktor Version

1.2.0

Ktor Engine Used(client or server and name)

Ktor server

JVM Version, Operating System and Relevant Context

java version "1.8.0_151"
MacOS

Feedback

I've migrated mi running project from 1.1.5 to 1.2.0 and I've the following error.

Exception in thread "main" java.lang.ClassNotFoundException: Module function cannot be found for the fully qualified name 'cat.helm.catformacio.ApplicationKt.module'

Has the configuration changed?

bug

Most helpful comment

Same problem. It is not good that users cannot run projects generated by KTor website :(

In ApplicationEngineEnvironmentReloading.kt at last line of isApplicableFunction for default boolean parameter we have it.kind == KParameter.Kind.VALUE, not KParameter.Kind.INSTANCE.

        private fun KFunction<*>.isApplicableFunction(): Boolean {
            if (isOperator || isInfix || isInline || isAbstract) return false
            if (isSuspend) return false // not supported yet

            extensionReceiverParameter?.let {
                if (!isApplication(it) && !isApplicationEnvironment(it)) return false
            }

            javaMethod?.let {
                if (it.isSynthetic) return false

                // static no-arg function is useless as a module function since no application instance available
                // so nothing could be configured
                if (Modifier.isStatic(it.modifiers) && parameters.isEmpty()) {
                    return false
                }
            }

            return parameters.all { isApplication(it) || isApplicationEnvironment(it) || it.kind == KParameter.Kind.INSTANCE }
        }

All 19 comments

I face the same problem. Migrating from 1.1.5 to 1.2.0
On windows with CIO server engine.

My problem was that i whas ussing:

fun Application.module(testing: Boolean = false) {}
It seems that now the parameter is not allowed, so I've had to add a wrapper function without parameter.

So for me you can close the issue.

Can confirm. This way it works:

@Suppress("unused") // Referenced in application.conf
fun Application.module(testing: Boolean) {

}

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module() {
    //...
}

However, with a default parameter and @JvmOverloads, it does not.

Same problem. It is not good that users cannot run projects generated by KTor website :(

In ApplicationEngineEnvironmentReloading.kt at last line of isApplicableFunction for default boolean parameter we have it.kind == KParameter.Kind.VALUE, not KParameter.Kind.INSTANCE.

        private fun KFunction<*>.isApplicableFunction(): Boolean {
            if (isOperator || isInfix || isInline || isAbstract) return false
            if (isSuspend) return false // not supported yet

            extensionReceiverParameter?.let {
                if (!isApplication(it) && !isApplicationEnvironment(it)) return false
            }

            javaMethod?.let {
                if (it.isSynthetic) return false

                // static no-arg function is useless as a module function since no application instance available
                // so nothing could be configured
                if (Modifier.isStatic(it.modifiers) && parameters.isEmpty()) {
                    return false
                }
            }

            return parameters.all { isApplication(it) || isApplicationEnvironment(it) || it.kind == KParameter.Kind.INSTANCE }
        }

I have the same problem. Also macOS, JDK8, ktor 1.2.

any updates here? I am having the same error, tried the trick with removing the testing boolean, does not work

The solution for we was removing both the testing parameter and the JvmOverloads annotation so that now it's just
fun Application.module() {
But waiting for a real fix because the testing boolean was useful.

Same issue here...kind of a big deal that the default generated Ktor project from IntelliJ fails to run

Fixed in 1.2.1

It's Not fixed yet.
I'm using 1.2.1 and getting the same error.
Only works using davidepianca98 solution.

I'm using 1.2.1 too. And get the same error.

The solution for we was removing both the testing parameter and the JvmOverloads annotation so that now it's just
fun Application.module() {
But waiting for a real fix because the testing boolean was useful.

Works fine for me ! Thanks !!

This is reproducible on 1.2.0 for me too, on a clean project, just created through IntellIJ IDEA. So creating and running a new empty project is broken, this should be a showstopper. I followed Ktor Getting Started docs.

With 1.2.1 works fine for me -https://github.com/ktorio/ktor/issues/1132#issuecomment-499356678-

Verified fixed in 1.2.1

Check the package of your kt file

The name must match what you have in application.conf

    application {
        modules = [ geofflangenderfer.ApplicationKt.module ]
    }

Fail

fun Application.mainModule() {
...
}

Success
` fun Application.module() { ... }

Try marking resources directory(the directory which contains the application.conf) as resources root. i.e. right-click on resources directory->Mark directory as-> Resources Root.
And src as sources root.

This worked for me ONLY if my file name was exactly Application.kt (I had chosen server.kt), and my function as: fun Application.module() {} (no parameter). This differs from the ktor docs https://ktor.io/docs/create-server.html#engine-main.

The design decision to wrap "main" and force the user to conform to your custom "main" format is never a good one because your lib is not the heart of my program. Let me be in control and pass you a callback function, if you absolutely MUST schedule the run of "main" yourself.

The decision to define what is "main" in a conf file is even crazier. What web app needs entry point A today, but tomorrow somehow there's a compelling reason to stop the server (which should never happen) and restart at entry point B?

This whole .conf file is just one more thing that can go wrong, and is massive overkill to define the port and IP, as is clear by all the search hits (should be optional).

The "embeddedServer" interface is nice clean and kotlin-like. But based on the docs, etc, it seems like it may be meant for debug not deploy? Does it have perf or other limitations? If it does not, it would be nice to make that clear.

Was this page helpful?
0 / 5 - 0 ratings