Kotlin for Forge

Kotlin for Forge

70M Downloads

Modules thedarkcolour.kotlinforforge and kotlin.stdlib export package kotlin to module fml_language_lowcode

forpleuvoir opened this issue ยท 12 comments

commented

My English is not good, so I use AI for translation.
I am developing Forge and Fabric mods using Architectury Loom.
I have dependent my own utility library, but when debugging on the Forge side, I cannot start the game normally within IntelliJ IDEA. However, when I package the mod as a JAR and place it in the actual game, it works fine.

Exception in thread "main" java.lang.module.ResolutionException: Modules thedarkcolour.kotlinforforge and kotlin.stdlib export package kotlin to module fml_language_lowcode
	at java.base/java.lang.module.Resolver.resolveFail(Resolver.java:901)
	at java.base/java.lang.module.Resolver.failTwoSuppliers(Resolver.java:815)
	at java.base/java.lang.module.Resolver.checkExportSuppliers(Resolver.java:736)
	at java.base/java.lang.module.Resolver.finish(Resolver.java:380)
	at java.base/java.lang.module.Configuration.<init>(Configuration.java:140)
	at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:494)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.ModuleLayerHandler.buildLayer(ModuleLayerHandler.java:75)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.ModuleLayerHandler.buildLayer(ModuleLayerHandler.java:87)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.Launcher.run(Launcher.java:94)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.Launcher.main(Launcher.java:78)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26)
	at MC-BOOTSTRAP/[email protected]/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23)
	at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141)
	at net.fabricmc.devlaunchinjector.Main.main(Main.java:86)
	at dev.architectury.transformer.TransformerRuntime.main(TransformerRuntime.java:219)

This is the source code of the problematic MOD.
This is the source code of the Kotlin library being referenced.

commented

A while ago I tried to investigate this issue and found that something weird was going on with classloading. The main classloader is set up correctly, but when a method from a library was called, it would use a different classloader all of a sudden and try to load Kotlin libs in that classloader which was not set up correctly. I'll look into it again later this week.

If I forcibly make the stdlib compileOnly, it seems to fix it so that it uses the correct classloader

commented

How can you do this in a Groovy buildscript?

commented

You may need to add a version, unsure about that

Edit: the kotlin DSL kotlin() function defaults to whatever your kotlin Gradle plugin version is, in my case 1.9.20

commented

compileOnly(kotlin("stdlib")) translates to compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:<kotlin version>'

commented

I just added that dependency to my dependencies block in the forge subproject and it more or less fixed it. I did run into a strange issue where forge sometimes can't find my mod object, it might be a race condition because sometimes it works sometimes it doesn't. It probably only happens in a dev environment though

commented

I just added that dependency to my dependencies block in the forge subproject and it more or less fixed it. I did run into a strange issue where forge sometimes can't find my mod object, it might be a race condition because sometimes it works sometimes it doesn't. It probably only happens in a dev environment though

I have tried this before, but it didn't work

commented

@thedarkcolour bump please I am also having this issue. It seems to work fine with the neoforge version on Architectury, but regular Minecraft forge causes this error.

commented

The specific module and package seem to change too, (race condition on which one gets displayed in the error message maybe?) For example I just got this one: Modules thedarkcolour.kotlinforforge and kotlin.stdlib export package kotlin.text to module net.minecraftforge.javafmlmod

commented

A while ago I tried to investigate this issue and found that something weird was going on with classloading. The main classloader is set up correctly, but when a method from a library was called, it would use a different classloader all of a sudden and try to load Kotlin libs in that classloader which was not set up correctly. I'll look into it again later this week.

commented

I found a workaround until you get it fixed.
If I add this to the top of the forge dependencies block, it seems to work

    compileOnly(kotlin("stdlib"))
commented

I do get this sometimes on the forge project:

The following classes are missing, but are reported in the mods.toml: [...]

It doesn't happen on fabric or NeoForge, it's definitely set up correctly and I hope it doesn't happen in production

commented

Same root cause as #86 so I'm marking as duplicate