Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark receiver parameter type as referenced. #807

Merged
merged 1 commit into from
Aug 12, 2022

Conversation

aeremin
Copy link
Contributor

@aeremin aeremin commented Aug 1, 2022

At the moment, jdeps generator will miss some dependencies for this code:

package com.example

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main() {
    // embeddedServer signature:
    // public fun <TEngine : io.ktor.server.engine.ApplicationEngine, TConfiguration : io.ktor.server.engine.ApplicationEngine.Configuration>
    //   embeddedServer(factory: io.ktor.server.engine.ApplicationEngineFactory<TEngine, TConfiguration>,
    //                  port: kotlin.Int /* = compiled code */,
    //                  host: kotlin.String /* = compiled code */,
    //                  watchPaths: kotlin.collections.List<kotlin.String> /* = compiled code */,
    //                  configure: TConfiguration.() -> kotlin.Unit /* = compiled code */,
    //                  module: io.ktor.server.application.Application.() -> kotlin.Unit): TEngine { /* compiled code */ }
    embeddedServer(Netty, port = 8081, host = "0.0.0.0") {
        // `this` is `io.ktor.server.application.Application` here - we depend on it.
        log.info("Hello world!")
        routing {
            get("/") {
                call.respondText("Hello Cloud World!")
            }
        }
    }.start(wait = true)
}

In particular, io.ktor.server.application.Application is not referenced anywhere directly, but we still depend on it to understand what are log and routing.

To do that - we need to collect types representing receiver arguments.

@aeremin aeremin marked this pull request as ready for review August 3, 2022 15:50
@aeremin
Copy link
Contributor Author

aeremin commented Aug 3, 2022

Why this is somewhat important: jdeps files are used by e.g. https://github.com/bazelbuild/intellij, which will wrongly report this code as "broken" (i.e. won't add jars providing Application and it parent classes to external libraries, consequently - won't resolve the references and will report the code as broken).

@Bencodes
Copy link
Collaborator

Bencodes commented Aug 3, 2022

Thanks for the PR @aeremin! Are you able to add a test case to KotlinBuilderJvmJdepsTest for receiver parameter type?

@aeremin
Copy link
Contributor Author

aeremin commented Aug 6, 2022

@Bencodes I have added a test, but while working on it understood that my initial fix was nonviable - it will only work if receiver parameter's this is explicitly referenced (by literally writing this which nobody really does).
In reality, we need to do a better type expansion by calling collectTypeArguments instead of just addImplicitDep. I've updated the PR accordingly.

BTW, I've noticed that this file can benefit from some cleanups (e.g. collectSuperTypes is unused, many of casts aren't needed - e.g. (resolvedCall.resultingDescriptor as PropertyImportedFromObject)). Are you interested in clean-up PR's?

@Bencodes
Copy link
Collaborator

Bencodes commented Aug 6, 2022

it will only work if receiver parameter's this is explicitly referenced

This might be a test case worth adding as well if it potentially changes how jdeps are interpreted.

Are you interested in clean-up PR's?

Absolutely! The person that was originally working on jdeps support inside of rules_kotlin isn't able to work on this anymore, so this code has largely been unmaintained since then. We'd be happy to see PRs for any other cases that aren't yet supported by jdeps as well.

@aeremin
Copy link
Contributor Author

aeremin commented Aug 6, 2022

This might be a test case worth adding as well if it potentially changes how jdeps are interpreted.

Now that I reverted the addition of ParameterDescriptor special case it actually doesn't really matter - we will get all dependencies via FunctionDescriptor anyway.

We'd be happy to see PRs for any other cases that aren't yet supported by jdeps as well.

Sounds good! Let's finish with this one then and then I'll do some cleanups and try to find other cases where jdeps are currently insufficient (I can think of some potential cases, but not yet 100% sure).

@Bencodes Bencodes merged commit a132456 into bazelbuild:master Aug 12, 2022
@Bencodes
Copy link
Collaborator

@aeremin just landed this. Thanks for the contribution!

@aeremin
Copy link
Contributor Author

aeremin commented Aug 13, 2022

Thanks folks! #819 is a cleanup I promised above.

@aryeh-looker
Copy link

I was bummed to see that I'm still having issues with ktor symbols resolving when using the latest release candidate, 1.7.0-RC-3.

e.g. this compiles and works fine, but reports errors in the IDE

image

Any thoughts? Can file another ticket.

@aeremin
Copy link
Contributor Author

aeremin commented Aug 18, 2022

@aryeh-looker could you file a separate ticket (with a full code snippet) and ping me there? I can take a look.

@Bencodes
Copy link
Collaborator

@aryeh-looker what's the actual error being shown here when you hover over the red? Is it just complaining that the types aren't available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants