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

Machine readable errors #1026

Closed
wants to merge 2 commits into from
Closed

Machine readable errors #1026

wants to merge 2 commits into from

Conversation

tiehuis
Copy link
Member

@tiehuis tiehuis commented May 29, 2018

This adds an option to the compiler for emitting machine-parseable error messages. The intended use is for editors or any command line processing. I need this currently for better error messages from zig fmt when putting itnegrating as part of an editor plugin.


pub fn main() !void {
    a = b;
}

Normal error messages:

$ zig build-exe t.zig
/tmp/t.zig:2:5: error: use of undeclared identifier 'a'
    a = b;
    ^
/tmp/t.zig:2:9: error: use of undeclared identifier 'b'
    a = b;
        ^

Machine error messages:

$ zig build-exe t.zig --machine-errors
/tmp/t.zig:2:5: use of undeclared identifier 'a'
/tmp/t.zig:2:9: use of undeclared identifier 'b'

This option forces the compiler to emit single line errors without notes
intended to be parsed by external programs (i.e. an editor).
@thejoshwolfe
Copy link
Sponsor Contributor

If we're going to have an option for one machine talking to another, we should solve all ambiguity problems, like use json output or something. could you parse json output in your editor plugin?

one concern with ambiguity is that file names can contain any byte sequence (in Linux, for example) except '\x00', even non-utf8 byte sequences, so it is fundamentally incorrect to use '\n' and ':' for delimiters.

if course, this is obscure enough that the human friendly output doesn't need to worry about it. but if we're going to make something machine friendly, let's actually do it right.

@andrewrk
Copy link
Member

Another thing that we already have that might solve this is --color off. The C++ compiler supports this already and will output errors in the same format as your example above. I intend to implement this in self-hosted as well. In fact, if you don't supply this option, the default is auto which will give the alternate error message format when stderr is not a TTY. I believe this would activate when using zig fmt in a vim plugin.

@tiehuis
Copy link
Member Author

tiehuis commented May 30, 2018

I realised when testing the vim plugin I was getting a stack-trace due to a bad command invocation that was tripping me up. It is probably the case already that you get something moderately okay as is. The main difference in this PR then is the dropping of the notes in the output, which isn't as relevant in an editor when you have access to the line/col.

@thejoshwolfe Those are good points. I think given the outstanding questions/edge cases here I'll close this PR and make do on the editor side until we have a more robust solution. I think providing a the output as JSON would be suitable.

@tiehuis tiehuis closed this May 30, 2018
andrewrk added a commit that referenced this pull request May 30, 2018
It doesn't actually do terminal color yet because we need to add
cross platform terminal color abstractions. But it toggles between
the single line error reporting and the multiline error reporting.

See #1026
@andrewrk
Copy link
Member

Just pushed this:

screenshot_2018-05-30_18-24-22

I think that should be everything we need for the vim plugin:

  • errors that the plugin can consume (should be the same format as the go vim plugin code that you forked from)
  • robust implementation of zig fmt

@tiehuis tiehuis deleted the machine-readable-errors branch June 10, 2018 08:28
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 this pull request may close these issues.

None yet

3 participants