Home > Net >  Kotlin throws false errors for everything in VS Code
Kotlin throws false errors for everything in VS Code

Time:08-10

This it's complicated to explain, so patience, please.

I started learning Kotlin a few weeks ago, but left it because other things and continue today, but every line of code it's highligthed with errors without reason.

Even a simple code as a "Hello World" got errors for everything.

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
}]

But this compile and work, without problems in the terminal:

cd "/home/my_username/programming/courses/kotlin/" && kotlinc first.kt -include-runtime -d first.jar && java -jar first.jar

Output:

Hello world

Use Ubuntu 22.04 and installed kotlin via snap (as indicated in the official website):

sudo snap install --classic kotlin

In vscode use the follow extensions that could be implicated in the problem:

I can run the cli compiler and every tool, but vscode/vscode-extensions error messages broke for no apparent reason, and just with this language.

IMPORTANT: The problem isn't the installation of the language itself, since the code compile and run witout problems, and don't throw error in terminal when executed.

CodePudding user response:

I was using kotlin some time ago and everything worked fine (vscode version was 1.69.2 at the time). I tried again today after seeing your question and got an error. (At this time, the vscode version has been upgraded to 1.70)

After I rolled back the vscode version to 1.69.2 it still didn't solve the problem.

So I upgraded the version to 1.70 again and the kotlin code is running normally again.

So, please uninstall your VS Code and install it again. ( The current latest version is 1.70. )

If it doesn't work once, reinstall it again.

This may be a bug, I will raise an issue on GitHub.

CodePudding user response:

Used vscode Bisect tool and dentified the problem. It's this extension:

This extension throw all the errors, disable and everything it's fine.

Alredy reported in this Github Issue

Until date (09/08/2022), the issue it's open and bug it's there.

  • Related