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

Unresolved reference: kotlin(UNRESOLVED_REFERENCE)

Open sbbgh opened this issue 2 years ago • 5 comments

Description

getting "Unresolved reference:" in build.gradle.kts but code working fine...

Extension version

[0.2.24]

Additional context

image image

sbbgh avatar Jun 19 '22 20:06 sbbgh

Got the same bug, but more hardcore. Throw error for everything.

Used vscode Bisect tool to find the error.

Code:

fun main() {
    val variable:String = "Hello world"
    print(variable)
}

Problems message in vscode:

Cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library
Unresolved reference: print
Cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library

JSON log error by vscode:

[{
	"resource": "/home/my_username/programming/courses/kotlin/first.kt",
	"owner": "_generated_diagnostic_collection_name_#1",
	"code": "MISSING_BUILT_IN_DECLARATION",
	"severity": 8,
	"message": "Cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library",
	"source": "kotlin",
	"startLineNumber": 2,
	"startColumn": 19,
	"endLineNumber": 2,
	"endColumn": 25
}]
[{
	"resource": "/home/my_username/programming/courses/kotlin/first.kt",
	"owner": "_generated_diagnostic_collection_name_#1",
	"code": "UNRESOLVED_REFERENCE",
	"severity": 8,
	"message": "Unresolved reference: print",
	"source": "kotlin",
	"startLineNumber": 3,
	"startColumn": 5,
	"endLineNumber": 3,
	"endColumn": 10
}]
[{
	"resource": "/home/my_username/programming/courses/kotlin/first.kt",
	"owner": "_generated_diagnostic_collection_name_#1",
	"code": "MISSING_BUILT_IN_DECLARATION",
	"severity": 8,
	"message": "Cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library",
	"source": "kotlin",
	"startLineNumber": 3,
	"startColumn": 11,
	"endLineNumber": 3,
	"endColumn": 19
}]
  • VSCode vr.: 1.70.0
  • Extension vr.: 0.2.26
  • Kotlin vr.: 1.7.0 (installed via snap)
  • System: Ubuntu 22.04.1 LTS
  • SO: 64 bits
  • Processor Unit: AMD® Ryzen 3 3200u with radeon vega mobile gfx × 4

cromega08 avatar Aug 09 '22 17:08 cromega08

@cromega08 . This is not the same issue that the original poster is getting. The original issue relates to build.gradle.kts files, while yours is for regular source files. The first one requires some special handling compared regular source files. That being out of the way...

  1. I guess this is a project that does not use Maven or Gradle? The language server works best with those kind of projects, but standard library should in many cases still be supported for more bare bones projects.

  2. Would it be possible for you to post the output of the language server? You will in most cases find this in the bottom of your view, together with Terminal and the other outputs. Seeing what happens here will help a lot in helping find to find the root cause of the issue you are having 🙂 It (VSCode output panel) looks like this on my end: image

  3. Is kotlinc in your PATH?

My main guess is that the language server doesn't find the standard library location, as it don't have any way checks for the locations where the snap installer places its files (still /snap/bin or something like that?). The current backup classpath implementation checks the local Gradle and Maven caches/repos, as well as kotlinc location if in path. Maybe there should be no issue as long as kotlinc is in path with snap? Could you post the location of kotlinc on your machine?

EDIT: Took a closer look at the backup classpath resolver. It should in theory be easy to extend it with possible locations of the lib-folder based upon the location of kotlinc. Ignore the Kotlin-language-server issue below...

Example on my machine. kotlinc in path resolves to /opt/homebrew/Cellar/kotlin/xxx/bin/kotlinc. stdlib lies in /opt/homebrew/Cellar/kotlin/xxx/libexec/lib/. The backup resolver seems to expect kotlinc to be in /opt/homebrew/Cellar/kotlin/xxx/libexec/bin/kotlinc.

Might be similar for other types of installations

themkat avatar Aug 09 '22 17:08 themkat

  1. There's no project, i'm learning kotlin and installed the extension for code completition and debugging, so until date it's me playing with the language to use it in the future.

  2. There it's no output about extensions or tasks.

  • Got this output about "Kotlin" when open the project:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[Info  - 1:29:41 p. m.] main      Adding script definitions [ScriptTemplateWithArgs]
[Info  - 1:29:41 p. m.] main      Kotlin Language Server: Version 1.3.1
[Info  - 1:29:41 p. m.] main      Connected to client
[Info  - 1:29:42 p. m.] async0    Adding workspace folder kotlin
[Info  - 1:29:42 p. m.] async0    Adding .../kotlin/first.kt under /home/cromega/programming/courses/kotlin to source path
[Info  - 1:29:42 p. m.] async0    Searching for dependencies and Java sources in workspace root /home/cromega/programming/courses/kotlin
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Gradle: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Found kotlinc at /snap/bin/kotlinc
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Gradle: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Found kotlinc at /snap/bin/kotlinc
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Gradle: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Found kotlinc at /snap/bin/kotlinc
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Gradle: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Found kotlinc at /snap/bin/kotlinc
[Info  - 1:29:42 p. m.] async0    Update build script path
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Maven: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Could not resolve kotlin-stdlib using Gradle: Cannot invoke "java.nio.file.Path.getFileSystem()" because "path" is null
[Info  - 1:29:42 p. m.] async0    Found kotlinc at /snap/bin/kotlinc
[Info  - 1:29:42 p. m.] async0    Reinstantiating compiler
[Info  - 1:29:42 p. m.] async0    Adding script definitions [ScriptTemplateWithArgs]
[Info  - 1:29:42 p. m.] client    Updated configuration: {"kotlin":{"java":{"home":"../../$JAVA_HOME"},"languageServer":{"enabled":true,"path":"","transport":"stdio","port":0,"debugAttach":{"enabled":false,"port":5005,"autoSuspend":false}},"trace":{"server":"off"},"compiler":{"jvm":{"target":"default"}},"linting":{"debounceTime":250},"indexing":{"enabled":true},"completion":{"snippets":{"enabled":true}},"debugAdapter":{"enabled":true,"path":""},"debounceTime":250,"externalSources":{"useKlsScheme":true,"autoConvertToKotlin":false},"snippetsEnabled":true}}
[Info  - 1:29:42 p. m.] debounce1 Linting .../kotlin/first.kt
[Info  - 1:29:42 p. m.] async2    Find symbols in .../kotlin/first.kt
[Info  - 1:29:43 p. m.] async2    Finished in 306 ms
[Info  - 1:29:44 p. m.] async3    Updating symbol index...
[Info  - 1:29:44 p. m.] async3    Updated symbol index in 13 ms! (0 symbol(s))
[Info  - 1:29:44 p. m.] debounce1 Reported 3 diagnostics in .../kotlin/first.kt
[Info  - 1:29:45 p. m.] async2    Ranged semantic tokens in .../kotlin/first.kt
[Info  - 1:29:45 p. m.] async2    Found 25 tokens
[Info  - 1:29:45 p. m.] async2    Finished in 10 ms
[Info  - 1:29:45 p. m.] async2    Full semantic tokens in .../kotlin/first.kt
[Info  - 1:29:45 p. m.] async2    Found 25 tokens
[Info  - 1:29:45 p. m.] async2    Finished in 3 ms
[Info  - 1:29:45 p. m.] async3    Updating symbol index...
[Info  - 1:29:45 p. m.] async3    Updated symbol index in 56 ms! (1 symbol(s))
[Info  - 1:29:45 p. m.] async3    Updating symbol index...
[Info  - 1:29:45 p. m.] async3    Updated symbol index in 1 ms! (2 symbol(s))
[Error - 1:29:45 p. m.] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate expression: KtCallExpression
File being compiled: (3,5) in //home/cromega/programming/courses/kotlin/first.kt
The root cause java.lang.AssertionError was thrown at: org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getResolvedCallWithAssert(callUtil.kt:209)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:356)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:481)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1362)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1307)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:56)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:22)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtBlockExpression.accept(KtBlockExpression.java:79)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:332)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:481)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:438)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1866)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:64)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:86)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:646)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:484)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:261)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:166)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.FunctionCodegen.gen(FunctionCodegen.java:137)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.MemberCodegen.genSimpleMember(MemberCodegen.java:200)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.PackagePartCodegen.generateBody(PackagePartCodegen.java:98)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:132)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:149)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:70)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generatePackage(CodegenFactory.kt:77)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generateModule(CodegenFactory.kt:62)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.compiler.Compiler.generateCode(Compiler.kt:570)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.SourcePath.save(SourcePath.kt:275)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.SourcePath.saveAllFiles(SourcePath.kt:287)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.KotlinTextDocumentService$lintAll$1.invoke(KotlinTextDocumentService.kt:267)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.KotlinTextDocumentService$lintAll$1.invoke(KotlinTextDocumentService.kt:265)
[Error - 1:29:45 p. m.] 	at org.javacs.kt.util.Debouncer.submitImmediately$lambda-1(Debouncer.kt:27)
[Error - 1:29:45 p. m.] 	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
[Error - 1:29:45 p. m.] 	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[Error - 1:29:45 p. m.] 	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
[Error - 1:29:45 p. m.] 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
[Error - 1:29:45 p. m.] 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
[Error - 1:29:45 p. m.] 	at java.base/java.lang.Thread.run(Thread.java:833)
[Error - 1:29:45 p. m.] Caused by: java.lang.AssertionError: No resolved call for 'print(variable)' at (3,5) in //home/cromega/programming/courses/kotlin/first.kt
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getResolvedCallWithAssert(callUtil.kt:209)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:2459)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitCallExpression(ExpressionCodegen.java:125)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtCallExpression.accept(KtCallExpression.java:35)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:31)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:22)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtVisitor.visitExpression(KtVisitor.java:182)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtVisitor.visitReferenceExpression(KtVisitor.java:198)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtVisitor.visitCallExpression(KtVisitor.java:278)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.psi.KtCallExpression.accept(KtCallExpression.java:35)
[Error - 1:29:45 p. m.] 	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:332)
[Error - 1:29:45 p. m.] 	... 37 more
[Info  - 1:29:45 p. m.] async3    Updating full symbol index...
[Info  - 1:30:12 p. m.] ..worker-1Updated full symbol index in 26009 ms! (12281 symbol(s))
  • The ouput when running the code it's:
Info  - 1:16:22 p. m.] async2    Hovering at .../kotlin/first.kt 2:40
[Info  - 1:16:22 p. m.] async2    Re-parsing first.kt 0:1-4:2
[Info  - 1:16:22 p. m.] async2    Hovering BLOCK
[Info  - 1:16:22 p. m.] async2    Re-parsing first.kt 0:1-4:2
[Info  - 1:16:23 p. m.] async2    Finished in 1120 ms
[Info  - 1:18:41 p. m.] async2    Full semantic tokens in .../kotlin/first.kt
[Info  - 1:18:41 p. m.] async2    Found 25 tokens
[Info  - 1:18:41 p. m.] async2    Finished in 18 ms
  1. No, isn't in $PATH. Installed via snap and didn't add anything about kotlin to my $PATH, kotlinc and kotlin are located at "/snap/bin".
  • NOTE: The code run perfect, doesn't throw errors when compiled and the output it's the expected "Hello world"

cromega08 avatar Aug 09 '22 18:08 cromega08

Do you know where it puts the stdlib jar file? My guess is that /snap/bin/kotlinc is a symlink, though I might be wrong. Is there a /snap/libexec/lib or something similar? Do readlink /snap/bin/kotlinc give any indication that it is a symlink? (or a similar program you use to see if it is a symlink). Just want to see if a possible fix I'm thinking of relating to my EDIT above would work in your case...

themkat avatar Aug 09 '22 18:08 themkat

  1. There's no /snap/libexec/lib, not even a /snap/libexec. Neither in my root or local snap dirs
  2. I run the command:
readlink /snap/bin/kotlinc

This give as output:

kotlin.kotlinc
  1. I don't know where stdlib.jar it's, not even a single idea. I'm less than junior in kotlin and doesn't used so much java.

cromega08 avatar Aug 09 '22 19:08 cromega08