elm / compiler

Compiler for Elm, a functional language for reliable webapps.
https://elm-lang.org/
BSD 3-Clause "New" or "Revised" License
7.48k stars 659 forks source link

`--report=json` emits a non-JSON nicely formatted error #2264

Open Janiczek opened 2 years ago

Janiczek commented 2 years ago

Quick Summary: elm make --report=json sometimes emits errors in a non-JSON format, breaking tools like @parcel/transformer-elm: (EDIT: they pick a second line and JSON.parse it)

Compilation failed
+-------------------------------------------------------------------------------
|  Corrupt File: /home/runner/work/frontend-gcp/frontend-gcp/elm/elm-stuff/0.19.1/i.dat
|   Byte Offset: 1183122
|       Message: not enough bytes
|
| Please report this to https://github.com/elm/compiler/issues
| Trying to continue anyway.
+-------------------------------------------------------------------------------
{"type":"error","path":null,"title":"CORRUPT CACHE","message":["It looks like some of the information cached in elm-stuff/ has been corrupted.\nTry deleting your elm-stuff/ directory to get unstuck.\n\n",{"bold":false,"underline":true,"color":null,"string":"Note"},": This almost certainly means that a 3rd party tool (or editor plugin) is\ncausing problems your the elm-stuff/ directory. Try disabling 3rd party tools\none by one until you figure out which it is!"]}
github-actions[bot] commented 2 years ago

Thanks for reporting this! To set expectations:

Finally, please be patient with the core team. They are trying their best with limited resources.

ChristophP commented 2 years ago

Are your sure both of those messages (the readable part and the JSON) are written to the same output stream? Usually the message part could be written to stdout while the json part is written to stderr (or vice versa).

lydell commented 2 years ago

Yes, both are written to stderr:

~/stuff
❯ mkdir  elm-error

~/stuff
❯ cd elm-error

~/stuff/elm-error
❯ yes | elm init >/dev/null

~/stuff/elm-error
❯ printf 'module Dummy exposing (dummy)\ndummy = ()' > src/Dummy.elm

~/stuff/elm-error
❯ elm make --report=json src/Dummy.elm

~/stuff/elm-error
❯ truncate -s 1024 elm-stuff/0.19.1/i.dat

~/stuff/elm-error
❯ elm make --report=json src/Dummy.elm > stdout.txt 2> stderr.txt

~/stuff/elm-error
❯ bat stdout.txt stderr.txt
───────┬─────────────────────────────────────────────────────────────────────────────────
       │ File: stdout.txt   <EMPTY>
       │ Size: 0 B
───────┴─────────────────────────────────────────────────────────────────────────────────
───────┬─────────────────────────────────────────────────────────────────────────────────
       │ File: stderr.txt
       │ Size: 852 B
───────┼─────────────────────────────────────────────────────────────────────────────────
   1   │ +-------------------------------------------------------------------------------
   2   │ |  Corrupt File: /Users/lydell/stuff/elm-error/elm-stuff/0.19.1/i.dat
   3   │ |   Byte Offset: 1024
   4   │ |       Message: not enough bytes
   5   │ |
   6   │ | Please report this to https://github.com/elm/compiler/issues
   7   │ | Trying to continue anyway.
   8   │ +-------------------------------------------------------------------------------
   9   │
  10   │ {"type":"error","path":null,"title":"CORRUPT CACHE","message":["It looks like some of the information cached in elm-stuff/ has been corrupted.\n\nTry deleting your elm
       │ -stuff/ directory to get unstuck.\n\n",{"bold":false,"underline":true,"color":null,"string":"Note"},": This almost certainly means that a 3rd party tool (or editor plu
       │ gin) is\ncausing problems your the elm-stuff/ directory. Try disabling 3rd party tools\none by one until you figure out which it is!"]}
───────┴─────────────────────────────────────────────────────────────────────────────────

In my experience, this happens whenever elm prints one of those “boxes”:

+---------------
| something
+---------------
ChristophP commented 2 years ago

Ah, I see 😢

unsafe-andrew-old commented 2 years ago

So what is a proposed solution? Print the error in the json format to the stdout? Or somehow remove those additional messages? The first one is solveable in changing literally 3 symbols in the source code

Janiczek commented 2 years ago

I imagine if --report=json is active, all the output should be those JSON lines. The +-boxed message shouldn't be shown.

An alternative is to send some of it to stdout and some of it to stderr, so that one of the streams still is purely JSON.