Lösungen
Märkte
Referenzen
Services
Unternehmen
Export errors and warnings from VS Code

Export errors and warnings from VS Code

28. Mai 2019

Export errors and warnings from VS Code

Have you ever tried to export the errors and warnings from VS Code? It turns out that – if I didn’t miss anything – it is not possible, but actually quite easy to implement.

The TL;DR

Install my extension in VS Code and run „ALRunner: Export diagnostics“. This will create a file called „diagnostics.csv“ which is a plain text file with the errors that you can import into e.g. Excel (click to run the clip):

The details

A bit of only searching revealed that this is a request that seems to have come up in 2017 or at least the oldest VS Code issue I found is from that time. It led to the creation of a rather simple API to get „diagnostic“ information, which contains all errors and warnings from extensions currently visible in the problem view. As I already had my VS Code extension in place, it was quite simple to extend it with an action that gets all errors and warnings and exports them to a .csv file (seperated by | as ; had to many clashes with the error messages itself):

    public exportDiagnostics() {
        let tuples = languages.getDiagnostics();
        let basePath = workspace.rootPath;
        let baseLength = basePath.length + 1;
        let diagnosticOutputs: diagnosticOutput[] = [];
        for (var [thisUri,thisDiagnostics] of tuples) {
            for (let thisDiagnostic of thisDiagnostics) {
                let myDiagnosticOutput : diagnosticOutput = new diagnosticOutput();
                if (thisUri.fsPath.startsWith(basePath)) {
                    myDiagnosticOutput.fsPath = thisUri.fsPath.substring(baseLength);
                } else {
                    myDiagnosticOutput.fsPath = thisUri.fsPath;
                }
                myDiagnosticOutput.code = thisDiagnostic.code;
                myDiagnosticOutput.message = thisDiagnostic.message;
                myDiagnosticOutput.startLine = thisDiagnostic.range.start.line;
                myDiagnosticOutput.startCharacter = thisDiagnostic.range.start.character;
                myDiagnosticOutput.endLine = thisDiagnostic.range.end.line;
                myDiagnosticOutput.endCharacter = thisDiagnostic.range.end.character;
                diagnosticOutputs.push(myDiagnosticOutput);
            }
        }
        let date  = new Date();
        let header = 'file|error code|error message|start line|start char|end line|end char\r\n';
        fs.writeFile(path.join(basePath, 'diagnostics.csv'), header + diagnosticOutputs.join('\r\n'), (err) => {
            if (err) {
                console.log(err);
                return;
            }
        });
    }

I just get all diagnostic information, iterate over it and write it an array of „diagnostic output“. That contains all from my point of view relevant aspects: The file with the problem, the error/warning code and message as well as the start and end of the problematic piece of code.

This also is completely generic and not bound to AL, so if you ever need to export your C#, Java, Python, PowerShell or whatever diagnostic information, ALRunner also is the way to go.


Ein Kommentar zu “Export errors and warnings from VS Code”

  1. Thank you for this extension. Exporting the problems pane is now easy. I can then use Excel to further filter the output to something that is significant for my team.

    You should document that the output in not strictly CSV format; it is „bang“ separated values (BSV?) and to load it into Excel properly, you start with a blank spreadsheet and import Text with | as a separator.
    (or you could change your code that it does use comma as a separator instead — or allow the user to choose the separator)


Schreibe einen Kommentar