prettier-plugin-kotlin
prettier-plugin-kotlin copied to clipboard
Error on all .kt files: "TypeError: Cannot read property 'toString' of null"
Background
In a project of mine, I recently converted some Java files to Kotlin and installed this plugin to format them. When I next ran prettier, though, this bug appeared. To ensure it wasn't something weird with my project specifically, I created a blank project to reproduce the issue. Below are the steps to reproduce the bug.
Reproduction steps
-
Create a blank npm project
npm init -
Install
prettierandprettier-plugin-kotlinnpm i prettier prettier-plugin-kotlin -
Create a sample Kotlin file to format, named
example.kt(I used the one below from "Example 1" at https://www.tutorialkart.com/kotlin/kotlin-create-file/)import java.io.File fun main(args: Array<String>) { val fileName = "data.txt" var file = File(fileName) // create a new file val isNewFileCreated :Boolean = file.createNewFile() if(isNewFileCreated){ println("$fileName is created successfully.") } else{ println("$fileName already exists.") } // try creating a file that already exists val isFileCreated :Boolean = file.createNewFile() if(isFileCreated){ println("$fileName is created successfully.") } else{ println("$fileName already exists.") } } -
Run
prettiernpx prettier --write *
Expected behavior
The three files in the project at the moment (package.json, package-lock.json, and example.kt) should be formatted correctly and the command should exit with code 0.
Actual behavior
example.kt is not formatted. The command exits with code 2.
The following output is produced:
$ npx prettier --write *
example.kt
[error] example.kt: TypeError: Cannot read property 'toString' of null
[error] at Object.module.exports [as parse] (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier-plugin-kotlin\src\parse.js:13:30)
[error] at Object.parse$a [as parse] (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\index.js:14892:19)
[error] at coreFormat (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\index.js:16425:16)
[error] at formatWithCursor$1 (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\index.js:16665:14)
[error] at Object.formatWithCursor (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\index.js:60922:12)
[error] at format$1 (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\bin-prettier.js:16107:14)
[error] at Object.formatFiles$1 [as formatFiles] (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\bin-prettier.js:16218:16)
[error] at main (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\bin-prettier.js:17989:10)
[error] at Object.run (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\bin-prettier.js:17932:5)
[error] at Object.<anonymous> (<REDACTED>\prettier-plugin-kotlin-bug\node_modules\prettier\bin-prettier.js:18000:20)
package.json 23ms
package-lock.json 18ms
Other remarks
The same error will be logged for all Kotlin files Prettier tries to format.
Looks like this line is a place to start looking:
https://github.com/Angry-Potato/prettier-plugin-kotlin/blob/7572ee3d19cea470ec427a79903b23905a8d1677/src/parse.js#L13
I guess child.stderr is null in my case? Not sure if that's a shell issue for me specifically, or if anyone else is having the same problem.
Any update on this issue? I have to uninstall prettier-plugin-kotlin and not auto-format *.kt files at all in the meantime.