Kotlin and VSCode Problems

Hello CD!

Because I am a bored wackjob, I am investigating the great language of Kotlin and its capabilities in FRC. After implementing the VSCode extensions for Kotlin, and rewriting the java files (based off of the java command template), I received the following error on the first build, ran with the VSCode command.

> Executing task: ./gradlew test   <


FAILURE: Build failed with an exception.

* What went wrong:
Could not initialize class org.jetbrains.kotlin.gradle.internal.KotlinSourceSetProviderImplKt

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Any ideas on how to fix that would be great!

Also receiving these problems:
1.

Build path specifies execution environment JavaSE-11. There are no compatible JREs installed in the workspace.
The project cannot be built until build path errors are resolved
Unbound classpath container: 'JRE System Library [JavaSE-11]' in project 'TheCoolKobot'
Could not run phased build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-6.0.1-bin.zip'.
Could not initialize class org.jetbrains.kotlin.gradle.internal.KotlinSourceSetProviderImplKt

I am capable of providing much more information and specifications upon request! Once again, thanks for any help!

Forwarding to our programmer. We are using kotlin this year.

Edit: he has never seen that error but will look into it

Thanks!

Could you share your build.gradle?

1 Like

We’ve been using Kotlin for a couple years now, but unfortunately I don’t recognize that error. It seems like your JAVA_HOME might be set incorrectly.

Also, here’s our code from this year, which might help if you’re looking for some sample code: https://github.com/team4099/InfiniteRecharge-2020

It’s not the most useful if you’re looking for WPILib examples since we don’t use command-based, but hopefully it gives some ideas for how you could use the language features. I do think that most teams who use Kotlin use IntelliJ, since it has better support for the language. If you’re interested in trying that out, you can open the existing project directory in IntelliJ and answer yes to importing the Gradle files, and everything will pretty much work.

Have you tried the Kotlin For FRC vs code extension? Just create a project from a template, run the extension, and it should work without manually needing to rewrite anything.

Actually been looking at your code for this lol!

Yes I used the extension by zPaw.

Here is my build.gradle. Hope this helps!

plugins {
    id "java"
    id "idea"
    id "edu.wpi.first.GradleRIO" version "2020.3.2"
    id "org.jetbrains.kotlin.jvm" version "1.1.51"
}

apply plugin: 'kotlin'

subprojects {
    apply(plugin = "org.jlleitschuh.gradle.ktlint")

    ktlint {
        debug.set(true)
    }
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

def ROBOT_MAIN_CLASS = "frc.robot.Main"

// Define my targets (RoboRIO) and artifacts (deployable files)
// This is added by GradleRIO's backing project EmbeddedTools.
deploy {
    targets {
        roboRIO("roborio") {
            // Team number is loaded either from the .wpilib/wpilib_preferences.json
            // or from command line. If not found an exception will be thrown.
            // You can use getTeamOrDefault(team) instead of getTeamNumber if you
            // want to store a team number in this file.
            team = frc.getTeamNumberOrDefault(2539)
        }
    }
    artifacts {
        frcJavaArtifact('frcJava') {
            targets << "roborio"
            // Debug can be overridden by command line, for use with VSCode
            debug = frc.getDebugOrDefault(false)
        }
        // Built in artifact to deploy arbitrary files to the roboRIO.
        fileTreeArtifact('frcStaticFileDeploy') {
            // The directory below is the local directory to deploy
            files = fileTree(dir: 'src/main/deploy')
            // Deploy to RoboRIO target, into /home/lvuser/deploy
            targets << "roborio"
            directory = '/home/lvuser/deploy'
        }
    }
}

// Set this to true to enable desktop support.
def includeDesktopSupport = true

// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries.
// Also defines JUnit 4.

repositories {
    mavenCentral()
}

dependencies {
    implementation wpi.deps.wpilib()
    nativeZip wpi.deps.wpilibJni(wpi.platforms.roborio)
    nativeDesktopZip wpi.deps.wpilibJni(wpi.platforms.desktop)


    implementation wpi.deps.vendor.java()
    nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
    nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)

    testImplementation 'junit:junit:4.12'

    // Enable simulation gui support. Must check the box in vscode to enable support
    // upon debugging
    simulation wpi.deps.sim.gui(wpi.platforms.desktop, false)
}

// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar')
// in order to make them all available at runtime. Also adding the manifest so WPILib
// knows where to look for our Robot Class.
jar {
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS)
}

So update: created an entirely new project with the Kotlin extension enabled, and it built. Now how should I go about implementing Kotlin?

Not sure what you mean. I used Kotlin last year and this year to program our Robot. I use Intellij IDEA with the FRC plugin that I wrote rather than VS Code. But either way, you can just write all the classes in Kotlin, still extending TimedRobot or whichever base class normally use.

I’ll give that a shot. Thanks!

So I did as recommended, and am now receiving this error. Ideas?

Could not initialize class org.jetbrains.kotlin.gradle.internal.KotlinSourceSetProviderImplKt

Also, on a different note, should I be creating this project off of a gradle base or a FRC base?
Thanks!

Ahh, I seem to be receiving a plugin error, and thus it disables it. Hmm.
The error blandly states:

Plugin 'org.jetbrains.kotlin' failed to initialize and will be disabled. 

Thoughts?

Another question about Kotlin/VSCode: has anyone been able to run the wpilib desktop simulation via intellij or on a Kotlin project? I am not able to run a kotlin simulation on VSCode (either Kotlin-Java compilation issues or “main class not found”, I am able to run Java sim on VSCode), or any simulation via intellij (it sometimes opens up the cmd, but no commands).

Are you able to build the project? What is the output of the Gradle tasks simulateExternalJava and simulateJava?

When I am running Kotlin in VSC or Java in intellij?

I am able to build everything. If I had access to a RIO I would be able to try deploy to see if it is a specifically Sim problem, but idk…

Do neither of those work? It shouldn’t matter what IDE you’re using. Did any of these projects work on a roboRIO before?

Neither were run on a RIO, but the java one is an empty project and the kotlin one is an empty one after the kotlinfrc convert function.

Interesting. Can you post the output of running gradlew simulateJava for both projects from the command line in the same folder as the project.