Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize error output format? #865

Closed
rogual opened this issue Feb 21, 2021 · 3 comments
Closed

Customize error output format? #865

rogual opened this issue Feb 21, 2021 · 3 comments

Comments

@rogual
Copy link

rogual commented Feb 21, 2021

Would you be amenable to having the output format for errors be customizable?

In Emacs, when you run a compilation command, it parses the output and if it sees something that looks like a filename and line number, it uses that info to let you jump to the location of the error.

You can customize the regex that it uses to look for these, but you can't easily use a multiline regex. This means it's hard to get this to work with ESBuild, which spreads the file and line number info over several lines:

 > app.jsx: error: Unexpected "}"
    4 │ }
      ╵ ^

It would be good to get a one-line, "machine-readable" error output option. For example:

app.jsx:4:Unexpected "}"

I have hacked my personal copy of esbuild thus:

func msgString(options OutputOptions, terminalInfo TerminalInfo, kind MsgKind, data MsgData, maxMargin int) string {
	return fmt.Sprintf("%s:%d:%s", data.Location.File, data.Location.Line, data.Text)
}

If I made this nice and put it behind a command-line option like "--error-format=compact" or something, would you be likely to accept it?

@evanw evanw closed this as completed in c55f01f Feb 21, 2021
@evanw
Copy link
Owner

evanw commented Feb 21, 2021

I'll add the line and column number after the file name like this:

 > src/structs/RTree.js:469:4: warning: Duplicate key "compareMinX" in object literal
    469 │     compareMinX: function (a, b)
        ╵     ~~~~~~~~~~~
   src/structs/RTree.js:206:4: note: The original "compareMinX" is here
    206 │     compareMinX: compareNodeMinX,
        ╵     ~~~~~~~~~~~

I'd really rather not add another command-line flag. If this approach is still troublesome, I recommend using esbuild's API instead which returns log messages as an array of objects. You can easily use the API to write a wrapper script that formats the log messages however you like.

@rogual
Copy link
Author

rogual commented Feb 21, 2021

This is great, thank you!

@rtpg
Copy link

rtpg commented Jul 25, 2022

For anyone who gets here after googling for "Emacs esbuild compilation regex", here's what I ended up setting up (to ignore the whitespace at the beginning of errors and have the right path be captured).

(For esbuild people: Emacs' compilation mode scans the output for errors. Most tools will put filename:line:col, without any whitespace in the front of the error. the whitespace in front of the default esbuild output ends up getting interpreted as the start of the line, which messes with the file lookup)

(require 'rx)
(setq esbuild-error-regex (rx
    line-start
    (+ whitespace)
    (group (1+ (not (or "\n" ":"))))
    ":"
    (group (1+ digit))
    ":"
    (group (1+ digit))
    ":"
    line-end))

(add-to-list
 'compilation-error-regexp-alist
 `(,esbuild-error-regex
   1 2 3
   )
 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants