Solutions
Markets
References
Services
Company
Export errors and warnings from VS Code

Export errors and warnings from VS Code

28. May 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.


Leave a Reply

Your email address will not be published. Required fields are marked *