vscode-kotlin icon indicating copy to clipboard operation
vscode-kotlin copied to clipboard

Unresolved reference for standard java library in Gradle project

Open MagnusMG opened this issue 2 years ago • 6 comments

I have a Gradle project written in Kotlin (mostly), and fwcd reports error unresolved reference on everything from the standard library (see the screendump below). When building the project in the terminal, with gradlew, everything works fine.

I'm using fwcd v0.2.26 on macos 12.5.1 and VSCode 1.70.2.

The folder structure is like this (I don't know how to present this very well)):

- app/
-- src/
-- build.gradle
- (other folders)
- settings.gradle

build.gradle

import java.nio.file.Files;
import java.nio.file.Paths;

plugins {
	id 'org.jetbrains.kotlin.jvm' version '1.6.21'
	id 'org.openjfx.javafxplugin' version '0.0.13'
	id 'org.beryx.jlink' version '2.25.0'
    id 'org.jetbrains.dokka' version '1.6.10'
	id 'antlr'	
}

repositories {
    mavenCentral()
    mavenLocal()
}


// ================= JavaFX
javafx {
	version = '18'
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

// ================= Antlr
generateGrammarSource {
    maxHeapSize = "128m"
    arguments += ['-package', 'se.magnusgunnarsson.hallkoll.generatedparser', '-listener']
	outputDirectory = file("src/main/java/se/magnusgunnarsson/hallkoll/generatedparser") 
}

sourceSets {
    generated {
        java.srcDir 'src/main/java/se/magnusgunnarsson/hallkoll/generatedparser'
    }
}

clean{
	delete 'src/main/java/se/magnusgunnarsson/hallkoll/generatedparser'
}


// ================= Java
compileJava {
	dependsOn generateGrammarSource
    options.compilerArgs += ["-Aproject=${project.group}/${project.name}"]
	source sourceSets.generated.java, sourceSets.main.java
	destinationDirectory = compileKotlin.destinationDir 
}


java {
	toolchain {
		languageVersion.set(JavaLanguageVersion.of("11"))
	}
 	withSourcesJar()
}

sourcesJar {
	dependsOn generateGrammarSource
}

// ================= Kotlin
kotlin {
    jvmToolchain {
        languageVersion.set(JavaLanguageVersion.of("11")) // "8"
    }
}

compileKotlin {
	dependsOn generateGrammarSource
}

application {
	mainModule = 'hallkoll'
    mainClass = 'se.magnusgunnarsson.hallkoll.HallKoll'
	applicationName = 'HallKoll'
}

// ================ JLink
jlink {
    launcher {
        name = "hallkoll"
    }
    imageZip.set(project.file("${project.buildDir}/image-zip/hallkoll-image.zip"))	
}

// ================== Dokka

dokkaHtml.configure {
    dokkaSourceSets {
        named("main") {
            includeNonPublic.set(true)
        }
        configureEach {
            includes.from("src/main/kotlin/se/magnusgunnarsson/hallkoll/packages-documentation.md")
        }
    }
}



// ============== Test
test {
    useJUnitPlatform()
	onlyIf { project.hasProperty('runtests') }
}

// ================= Package dependencies
dependencies {
    implementation platform('org.jetbrains.kotlin:kotlin-bom')
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
   	antlr 'org.antlr:antlr4:4.10.1'
   	implementation 'org.antlr:antlr4-runtime:4.10.1'
    implementation 'se.explodera:util:1.0'
    implementation 'se.magnusgunnarsson:kutil:1.0'
	implementation 'org.apache.derby:derby:10.13.1.1'
}

settings.gradle:

rootProject.name = 'HallKoll'
include('app')

Problem

screendump

MagnusMG avatar Aug 28 '22 15:08 MagnusMG

Please share the language server output, and it will be easier to find the problem here 🙂 You can find it in Output -> Kotlin (bottom of the screen where Problems are shown in you screenshot, Kotlin is in the dropdown list when you click Output).

My first guess without looking too much into it is that your top-level directory doesn't seem to have a build.gradle or build.gradle.kts file, which is what the language servers Gradle classpath resolver checks for. We will know for sure once you share your output as described in the first paragraph 🙂 Might be a simple fix to add settings.gradle to the class path resolver, or it might not. Unsure as of now. Might also help to open the inner app-directory instead as that directory has a build.gradle.

themkat avatar Aug 28 '22 19:08 themkat

You'll find the language server output here (to long to include in the text.)

I doubt the problem is the location of the build.gradle file. I have other projects with the same basic project layout and they work fine. However, the problem does seem to disappear when I open the inner app-directory. Hard to say for certain, though, since so many other problems arise then.

MagnusMG avatar Aug 28 '22 20:08 MagnusMG

From the log file you posted:

FAILURE: Build failed with an exception.

* What went wrong:
Project directory '/Users/xgumag/privat/obackupat/MagnusPrivat/utveckling/hallkoll/app/bin/default' is not part of the build defined by settings file '/Users/xgumag/privat/obackupat/MagnusPrivat/utveckling/hallkoll/settings.gradle'. If this is an unrelated build, it must have its own settings file.

* 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 gradle setup is probably malformed, and the language server cannot fetch the dependencies through gradle. The Gradle resolver actually runs Gradle to fetch a list of dependencies, and when it fails, no dependencies will be fetched. There is not much the language server can do in this case.

themkat avatar Aug 29 '22 14:08 themkat

Thanks, removing that directory solved it. I have no idea which extension caused that directory to be created, though. Hm.

Anyway, I find it a bit strange that fwcd fails when the terminal build (running ./gradlew build) reports no problems. Is that by design...?

MagnusMG avatar Aug 29 '22 19:08 MagnusMG

Anyway, I find it a bit strange that fwcd fails when the terminal build (running ./gradlew build) reports no problems. Is that by design...?

fwcd is the user who created this repo, the application that runs is kotlin-language-server 🙂

The gradle class path resolver runs a custom gradle task that is appended to the existing Gradle goals. To my knowledge, it should usually work when ./gradlew build works, so this seems like a weird edge case in my view 😱 Unsure why it happens, can't see that we have done anything for it to fail when build works. Probably some internal Gradle stuff that interferes with the build? Maybe the custom task needs all directories to be resolved before continuing or something? To populate all properties or similar? Only guessing from my side off course 😛

themkat avatar Aug 29 '22 19:08 themkat

fwcd is the user who created this repo, the application that runs is kotlin-language-server 🙂

:-D So old, and still so much to learn.

OK, I'll leave it at that. Thanks for the help and sorry for reporting a bug that wasn't a bug, but the fact the ./gradlew builld worked really tricked me.

MagnusMG avatar Aug 29 '22 19:08 MagnusMG