https://kotlinlang.org logo
#gradle
Title
# gradle
n

Nathan Bedell

11/21/2023, 1:17 PM
Hey all. I'm currently having some issues trying to apply a gradle plugin written in Groovy from my kts gradle script (Note: I do not have the ability to tweak the groovy code). Basically it seems like the groovy plugin is configured to need to see a particular method on the root project which it tries to call. So I basically get errors like: Could not find method foo() for arguments [] on root project 'my_root_project_name' of type org.gradle.api.Project I've tried defining my own function like "fun foo() ..." various places in my kts script, but no matter what I do, Groovy can't find it. Does anyone have a workaround for issues like this?
m

mbonnin

11/21/2023, 1:18 PM
Sounds like this could help?
plugin is configured to need to see a particular method on the root project which it tries to call.
If you can get the signature of that method (whether a Groovy closure is expected or something else), that'll help
n

Nathan Bedell

11/21/2023, 1:25 PM
I'll give that a shot! I just realized that in Groovy scripts that use this plugin, foo is defined as: ext.foo = { ... } So I guess that is a groovy closure, not a function.
m

mbonnin

11/21/2023, 1:27 PM
Maybe something like so?
Copy code
extra.properties["foo"] = closureOf<Project> {
  // Do stuff here
}
Not an expert by any means but I have been bitten by this a couple of times
v

Vampire

11/21/2023, 1:28 PM
Or
Copy code
val foo by extra {
    closureOf<Project> {
        ...
    }
}
Besides that this plugin should probably be fixed of using such hacks 😄
☝️ 1
☝🏾 1
n

Nathan Bedell

11/21/2023, 1:32 PM
It did seem very hacky to me. 😂 Out of curiosity, what's a less hacky way to do something like this? Unfortunately I don't think I could convince the maintainer to move to Kotlin, but maybe they'd accept a MR.
v

Vampire

11/21/2023, 1:33 PM
What you need to assign the extra property to depends on what the plugin expects. Maybe also something like
Copy code
val foo by extra {
    KotlinClosure1<String, Unit> ...
}
Out of curiosity, what's a less hacky way to do something like this?
Basically, almost anything that uses
extra
or
ext
is hacky and just some work-around for not doing it properly.
That maintainer does not need to switch to Kotlin, he just have to do it properly. 😄
How that properly would look like depends on what that plugin needs / does. Impossible to guess without knowing what this is actually about.
But probably the plugin would instead register some extension with a method taking an
Action<...>
as parameter that is then used as callback instead of requiring to set some
extra
property to a Groovy closure.
n

Nathan Bedell

11/21/2023, 1:41 PM
Hmm... Well I've made some progress and gotten another error. It's complaining about a $receiver for Build_gradle$1$1$1.invoke is null for some reason.
Same error for either the KotlinClosure or closureOf
v

Vampire

11/21/2023, 1:42 PM
Well, as said, what you have to give there highly depends on what that plugin is expecting you to deliver. We can hardly guess.
n

Nathan Bedell

11/21/2023, 1:43 PM
Yeah, I'm just not sure how the receiver would be null. Unhelpfully the stack trace doesn't actually go down into the plugin code for some reason.
v

Vampire

11/21/2023, 1:44 PM
🤷‍♂️
n

Nathan Bedell

11/21/2023, 1:45 PM
Welcome to Gradle, lol.
v

Vampire

11/21/2023, 1:46 PM
That has nothing to do with Gradle. It's just a badly designed 3rd party plugin.
1
n

Nathan Bedell

11/21/2023, 1:58 PM
This is just my experience with Gradle in general. Maybe Groovy is the true culprit, not Gradle -- but the issue is obtuse error messages with little to no help for the developer in actually figuring out what the issue is. The plugin itself may be bad, but that's still a framework problem IMHO. The framework should provide easily accessible tools allowing the developer to debug even bad code (See Rust's error messages for instance). After all, more often than not, that's what needs debugging.
But I think after changing the type of my receiver to nullable I have solved the problem. Thank you for your help!
👌 1
💙 1