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

Unable to use other Kotlin libraries with KFF on Forge/NeoForge #86

Open
Erdragh opened this issue Nov 28, 2023 · 19 comments
Open

Unable to use other Kotlin libraries with KFF on Forge/NeoForge #86

Erdragh opened this issue Nov 28, 2023 · 19 comments

Comments

@Erdragh
Copy link

Erdragh commented Nov 28, 2023

I'm writing a Minecraft mod / Discord bot combo: https://github.com/Erdragh/AstralBot
It uses JDA to communicate with Discord. JDA has a dependency on okhttp3 which over multiple corners has a dependency on the kotlin stdlib. I'm excluding the org.jetbrains.kotlin dependency from JDA.

I'm using Kotlin for the Mod itself and the build scripts. The Fabric server starts fine, the Forge server doesn't.

To reproduce:

  • Clone repo mentioned above
  • Start the Forge server with the env variable DISCORD_TOKEN set to any value, just so it's defined.

The relevant exception is the following: https://gist.github.com/Erdragh/c84543358f069f43e16e90fb55665324

I'm not sure whether this is an Architectury or a KFF issue, I've already made a support post on the Architectury Discord with no result.

What I find weird is that kotlin.reflect is apparently loading fine judging from the stack trace, but kotlin/Unit just can't be found.

@Erdragh
Copy link
Author

Erdragh commented Nov 28, 2023

Here are the dependencies for forge using Architectury's forgeRuntimeLibrary: https://gist.github.com/Erdragh/55200d17dda4511c092727654016b549

@Erdragh
Copy link
Author

Erdragh commented Nov 28, 2023

Possibly related:
Something about dependency changes in JDA: discord-jda/JDA#2129
(I'm not manually specifying dependencies tho, so probably not the exact same thing)

@thedarkcolour
Copy link
Owner

To use Kotlin For Forge in Architectury you must use "implementation" instead of "forgeImplementation"

@Erdragh
Copy link
Author

Erdragh commented Nov 28, 2023

@Erdragh
Copy link
Author

Erdragh commented Nov 28, 2023

According to the Architectury Discord this may be caused by okhttp3 not having access to kotlin because it's in the bootstrap class loader, while KFF's kotlin stdlib is in the mod class loader. I don't know enough about forge and especially forge's class loading to give any specifics tho.

@thedarkcolour
Copy link
Owner

If you compile your mod and run it in a Forge instance outside of IntelliJ, does it still crash?

@Erdragh
Copy link
Author

Erdragh commented Nov 29, 2023

It does crash but because of a different thing, it can't load the Apache collections things, but that's because from what I can tell they're just not there and I need to include it - or shadow it in.

Edit: said crash also happens when running the built fabric jar on a fabric server, so it's not a forge issue.

@thedarkcolour
Copy link
Owner

You may need to add the Kotlin dependencies to Gradle yourself. If that doesn't work, I know on ForgeGradle there is a snippet that gets shared around on the server with the command !nonmclibs which links to this gist. Not sure what the equivalent is for that in Architectury but you might be able to ask around. If that still doesn't work, then let me know.

@Erdragh
Copy link
Author

Erdragh commented Nov 29, 2023

Could you elaborate on that first part a bit? I'm kind of new to gradle and have been reading a lot of documentation, but haven't wrapped my head around everything just yet

@thedarkcolour
Copy link
Owner

Is your buildscript a build.gradle or a build.gradle.kts?

@Erdragh
Copy link
Author

Erdragh commented Nov 29, 2023

I've linked the repo, it's all completely .kts

@thedarkcolour
Copy link
Owner

Add this to your dependencies block:

    // Default classpath
    api(kotlin("stdlib"))
    api(kotlin("stdlib-common"))
    api(kotlin("stdlib-jdk8"))
    api(kotlin("stdlib-jdk7"))
    api(kotlin("reflect"))

@Erdragh
Copy link
Author

Erdragh commented Nov 29, 2023

Didn't fix it. The equivalent of minecraftLibrary for Architectury seems to be forgeRuntimeLibrary, replacing the api in your suggestion with that results in:

> Could not find org.jetbrains.kotlin:kotlin-stdlib:.
     Required by:
         project :forge

and more for each specified dependency

@thedarkcolour
Copy link
Owner

You might need to try something else besides what I gave you, since gradle cannot find kotlin for some reason

@thedarkcolour
Copy link
Owner

I've tested with a debugger and it looks like okhttp3 is being loaded on a different classloader than JDA. When stepping into the setter for Dispatcher.maxRequestsPerHost, you can use the Evaluate Expression tool to see that kotlin.Unit crashes, like in your original error message. However, Minecraft classes like Block and Entity also crash, which is strange and indicates that this is probably not a KFF issue.

@thedarkcolour thedarkcolour added this to the Kotlin for Forge shades Kotlin libraries instead of JarJar milestone Apr 29, 2024
@object-Object
Copy link

object-Object commented May 2, 2024

I encountered what I think is the same issue while trying to use Ktor on Forge with Architectury, and I came up with a slightly hacky workaround that seems to work pretty well.

As you said, I think the issue is that forgeRuntimeLibrary loads classes on the MC-BOOTSTRAP layer/classloader (via the legacyClassPath.file property), while KFF puts the Kotlin stdlib on the PLUGIN layer. Since MC-BOOTSTRAP only has the BOOT layer as a parent, Kotlin libraries loaded there are unable to access the stdlib on the PLUGIN layer. It works fine in production because JarJar libraries are loaded on the GAME layer, which has access to PLUGIN.

I'm not sure if there's a better way to fix this in dev, but my workaround was to create a Gradle artifact transform that adds FMLModType: GAMELIBRARY to the META-INF/MANIFEST.MF file of each Kotlin dependency. This tells Forge to load the jar as a library on the GAME layer. I'm only using this transform in the localRuntime configuration, so it doesn't affect the libraries included in the production jar.

Links:

I think this is also the same issue behind #94.

@thedarkcolour thedarkcolour changed the title Question: kotlin/Unit can't get loaded in Architectury multiplatform setup with okhttp3 dependency Unable to use other Kotlin libraries with KFF on Forge/NeoForge May 4, 2024
@thedarkcolour thedarkcolour removed this from the Kotlin for Forge shades Kotlin libraries instead of JarJar milestone May 4, 2024
@thedarkcolour
Copy link
Owner

This issue happens because Kotlin for Forge shadows the Kotlin standard libraries, when it should be using JarJar to include them. However, due to an issue in Forge/NeoForge this is not possible.

Worth mentioning that this issue has been fixed in Kotlin for Forge 5.0.0 on NeoForge 1.20.5.

@object-Object
Copy link

Just to confirm, are JarJar libraries loaded onto the Boot or MC-BOOTSTRAP layer? If not, I'd think this would still be an issue in the dev environment, wouldn't it?

@thedarkcolour
Copy link
Owner

JarJar libraries are loaded onto the GAME layer.

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

No branches or pull requests

3 participants