dprint / dprint-intellij

A dprint plugin for Intellij.
https://plugins.jetbrains.com/plugin/18192-dprint
MIT License
12 stars 2 forks source link

The plugin doesn't work when using the reformat action, pressing "Reload" fails with an error #33

Closed mvlabat closed 2 years ago

mvlabat commented 2 years ago

Hi! I'm using the 0.3.5 version.

I have dprint enabled in the WebStorm settings, but it doesn't work when I trigger the "Reformat code" action (Cmd+Alt+L). It doesn't show any errors, it just does nothing when I press the hotkey combination.

If I enable "Run dprint formatter on save" though, it does reformat my code on save.

Also, I noticed this issue when pressing the reload button (maybe I should've created a separate one for it, but I guess I'll just leave it here for now):

Failed to read stdout of the editor service

java.nio.BufferUnderflowException
    at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:651)
    at java.base/java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:402)
    at com.dprint.services.editorservice.EditorProcess.readInt(EditorProcess.kt:147)
    at com.dprint.services.editorservice.v5.StdoutListener.handleStdout(StdoutListener.kt:34)
    at com.dprint.services.editorservice.v5.StdoutListener.run(StdoutListener.kt:22)
    at com.dprint.services.editorservice.v5.EditorServiceV5$createStdoutListener$1.invoke(EditorServiceV5.kt:33)
    at com.dprint.services.editorservice.v5.EditorServiceV5$createStdoutListener$1.invoke(EditorServiceV5.kt:32)
    at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)

I hope this helps, but let me know if there are any logs or debug info I can fetch as well.

Thank you for maintaining this project!

ryan-rushton commented 2 years ago

Hi @mvlabat and thanks for the report, sorry it has taken me a bit to get to this.

What kind of file are you trying to format?

I have added a console tab with the label dprint that should appear at the bottom of the IDE. What is the output in that after running Cmd+Alt+L?

Also, there is something I have noticed with Cmd+Alt+L in that a file needs to be saved before the format will work. If you run Cmd+Alt+L twice you may see the file get formatted as IntelliJ will save the file under the hood after attempting to run the first format (where it doesn't have your editor changes). Another way to test this is to disable dprints run on save functionality, save the file then run Cmd+Alt+L, if it formats this will be the issue.

If this is the case I will chase JetBrains about whether this is intended behaviour and if I can change it.

"Run dprint formatter on save" will always work as it runs after the file has been saved so it is assured to be the latest changes in the editor.

ryan-rushton commented 2 years ago

Also, I noticed this issue when pressing the reload button (maybe I should've created a separate one for it, but I guess I'll just leave it here for now):

Failed to read stdout of the editor service

java.nio.BufferUnderflowException
  at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:651)
  at java.base/java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:402)
  at com.dprint.services.editorservice.EditorProcess.readInt(EditorProcess.kt:147)
  at com.dprint.services.editorservice.v5.StdoutListener.handleStdout(StdoutListener.kt:34)
  at com.dprint.services.editorservice.v5.StdoutListener.run(StdoutListener.kt:22)
  at com.dprint.services.editorservice.v5.EditorServiceV5$createStdoutListener$1.invoke(EditorServiceV5.kt:33)
  at com.dprint.services.editorservice.v5.EditorServiceV5$createStdoutListener$1.invoke(EditorServiceV5.kt:32)
  at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)

Ah thanks, yeah this is an error on my part but it causes no harm outside of some error spam in the UI. I just need to ensure things are shut down in the correct order on restart, or I just need to silence that error.

ryan-rushton commented 2 years ago

I have asked about the odd save/format functionality in an IntelliJ plugin slack workspace. I will report back what I find out.

mvlabat commented 2 years ago

@ryan-rushton thank you for the response!

Oh, I can actually confirm that it works for me in some of the files, and pressing Cmd+Alt+L doesn't require me to save a file before triggering the action - it works correctly anyway.

Turns out, it doesn't work for me in .tsx files. Does the plugin respect the includes directive of dprint.json?

Here's my config:

{
  "typescript": {
    "quoteStyle": "preferSingle"
  },
  "json": {
  },
  "markdown": {
  },
  "includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md}"],
  "excludes": [
    "**/node_modules",
    "**/*-lock.json"
  ],
  "plugins": [
    "https://plugins.dprint.dev/typescript-0.69.1.wasm",
    "https://plugins.dprint.dev/json-0.15.3.wasm",
    "https://plugins.dprint.dev/markdown-0.13.3.wasm"
  ]
}

It works for those .txs files I was editing when I run dprint from CLI.

ryan-rushton commented 2 years ago

Yeah so the plugin basically says that it can format those files when IntelliJ asks it if it can. Doing so will mean our "formatter" will override the default IntelliJ formatter. My hypothesis is that the default IntelliJ formatter gets run after a save, but external formatters (such as our dprint one) run before a save.

I have asked in an IntelliJ plugin developer slack channel for confirmation but haven't heard anything back. I am hoping the JetBrains representatives in there will confirm whether this is intended behaviour or a bug. If I don't hear anything back in the next few days I will raise a bug directly on their YouTrack and see what happens.

ryan-rushton commented 2 years ago

OOI, what other files were you comparing to. Just having a look at other plugins that implement this same interface and the rust plugin does something very similar to what I am doing.

mvlabat commented 2 years ago

Another file (for which the formatter worked) had the .ts extension. I'm not sure that WebStorm was running its default one afterwards, as it didn't fix any obvious places where formatting was off (like redundant tabs).

What is weird though, is that the plugin started working with .txs files as well. :D I think it might have happened after I reenabled dprint formatting in its own settings ("in its own settings" - as opposed to reenabling the whole plugin). Not really sure..

Also, I've run another test that probably confirms that the default formatter doesn't interfere with the dprint one:

  1. I set indentation to 4 spaces for the default formatter and 2 spaces for dprint.
  2. I enabled dprint in its settings, formatted a file, it correctly formatted it to have 2 spaces indentation.
  3. I disabled dprint in its settings, formatted a file, the default formatter made the indentation to have 4 spaces as expected.

If WebStorm was running its own formatter after dprint, I guess I'd get 4 spaces indentation at the second step as well.

ryan-rushton commented 2 years ago

Had a bit of a dig into this and it appears the issue is with the underlying dprint process it seems.

@dsherret I have a that I am triggering a format on and the first format is always coming back as the file cannot be formatted. Example logs are here. I have confirmed that the text I am sending in is correct in that it needs to be formatted.

2022-07-05 19:36:58,637 [ 202676]   INFO - #c.d.f.DprintExternalFormatter - Sending format with id 7
2022-07-05 19:36:58,699 [ 202738]   INFO - #c.d.s.e.EditorServiceManager - Creating formatting task for /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx
2022-07-05 19:36:58,699 [ 202738]   INFO - #c.d.s.e.v.EditorServiceV5 - Formatting /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx
2022-07-05 19:36:58,699 [ 202738]   INFO - #c.d.s.e.v.EditorServiceV5 - Created formatting task for /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx with id 7
2022-07-05 19:36:58,745 [ 202784]   INFO - #c.d.s.e.v.StdoutListener - Responding to message 7: Has changed is 0
2022-07-05 19:36:58,745 [ 202784]   INFO - #c.d.s.e.v.EditorServiceV5 - No need to format /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx
2022-07-05 19:37:12,571 [ 216610]   INFO - #c.d.f.DprintExternalFormatter - Sending format with id 8
2022-07-05 19:37:12,577 [ 216616]   INFO - #c.d.s.e.EditorServiceManager - Creating formatting task for /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx
2022-07-05 19:37:12,578 [ 216617]   INFO - #c.d.s.e.v.EditorServiceV5 - Formatting /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx
2022-07-05 19:37:12,578 [ 216617]   INFO - #c.d.s.e.v.EditorServiceV5 - Created formatting task for /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx with id 8
2022-07-05 19:37:12,580 [ 216619]   INFO - #c.d.s.e.v.StdoutListener - Responding to message 8: Has changed is 1
2022-07-05 19:37:12,580 [ 216619]   INFO - #c.d.s.e.v.EditorServiceV5 - Successfully formatted /Users/ryanrushton/work/dprint-test/my-app/src/App.tsx

https://user-images.githubusercontent.com/7344652/177301632-a1bf88c2-2bf3-4e8b-9483-1f2b7a2cc855.mp4

dsherret commented 2 years ago

@ryan-rushton hmmm that's really strange. Does this reproduce each time? Have you been able to reproduce it with the vscode plugin?

What's the output of dprint when running with --verbose?

I have confirmed that the text I am sending in is correct in that it needs to be formatted.

This was the only thing I could think of (it initially sending the wrong file text to be formatted). I looked through the code and didn't see anything obvious.

ryan-rushton commented 2 years ago

Just a follow up as @dsherret and I figured this out. IntelliJ is sending a range that is larger than the actual content of the file. This was causing a no-op formatting result. For whatever reason it didn't happen on second format. I have raised this in the IntelliJ plugin slack and will see if anything comes up around it.

ryan-rushton commented 2 years ago

@mvlabat a new version (0.3.6) should be up on the store in the next day or so. I think it should fix both of those issues you encountered.