LaurentTreguier / vscode-uncrustify

Code format using uncrustify
66 stars 10 forks source link

Extension removes all code #66

Closed JanHildenbrandBosch closed 4 years ago

JanHildenbrandBosch commented 4 years ago

OS: Ubuntu 18.04 VSCode: 1.46.1 Ext Version: v2.20.2 Uncrustify: Uncrustify_d-0.71.0-44-7fdda9c7 (which should contain `https://github.com/uncrustify/uncrustify/pull/2798)

This issue seems related to #63, but already contains the uncrustify fix. I assume it is a different issue and posted it in a new issue.

The following code leads to an empty file:

void test(void)
{

}
}

Debug output:

Debug: config file: /workspace/uncrustify.cfg Debug: launched: uncrustify -l CPP -c /workspace/uncrustify.cfg Debug: uncrustify exited with error: Parsing: 26 bytes (26 chars) from stdin as language CPP parse_cleanup(446): pc->orig_line is 5, orig_col is 1, text() is '}', type is BRACE_CLOSE parse_cleanup(448): (frm.top().type + 1) is UNKNOWN parse_cleanup(837): Unmatched BRACE_CLOSE orig_line is 5, orig_col is 1 parse_cleanup(844): Try the option 'tok_split_gte = true' Debug: uncrustify exited with status: 1

Although the input is malformed, in my opinion it should not delete the source code. In our setup we use auto-format on save and it can be very frustrating losing all the code after saving, because an additional "}" is in the code.

It seems that the extension does only interpret return codes < 0 as error codes and reject the formatting. In my opinion all return values != 0 should be rejected. It seems the following code is the culprit:

// formatter.ts:100
            uncrustify.on('error', reject);
            uncrustify.on('exit', async code => {
                logger.dbg('uncrustify exited with status: ' + code);

                if (code < 0) {
                    vsc.window.showErrorMessage('Uncrustify exited with error code: ' + code);
                    reject(code);
                } else if (useTempFile) {
                    const result = await u.promisify(fs.readFile)(this.tempFileName);
                    resolve([new vsc.TextEdit(this.getRange(document, range), result.toString())]);
                } else if (useReplaceOption) {
                    resolve();
                }
            });
LaurentTreguier commented 4 years ago

I'll look into this. have you tried with an earlier version like Uncrustify 0.70.1 ?

JanHildenbrandBosch commented 4 years ago

I'll look into this. have you tried with an earlier version like Uncrustify 0.70.1 ?

We had previously the ubuntu 18.04 apt native Uncrustify-0.66.1_f where this problem also exists. I did not explicitly check 0.70.1. The problem seems to be existing for a longer time.

Debug: config file: /workspace/uncrustify.cfg Debug: launched: uncrustify -l C -c /workspace/uncrustify.cfg Debug: uncrustify exited with status: 1 Debug: uncrustify exited with error: Parsing: 26 bytes (26 chars) from stdin as language C Unmatched BRACE_CLOSE at orig_line=5, orig_col=1 Try the option 'tok_split_gte = true'

This is the debug result of 0.66.1 - it seems they did not change the return value from 0.66.1 to 0.71.0.

Edit: As I expected 0.70.1 is also not working:

Debug: config file: /workspace/uncrustify.cfg Debug: launched: uncrustify -l CPP -c /workspace/uncrustify.cfg Debug: uncrustify exited with error: Parsing: 22 bytes (22 chars) from stdin as language CPP parse_cleanup(444): pc->orig_line is 5, orig_col is 1, text() is '}', type is BRACE_CLOSE parse_cleanup(446): (frm.top().type + 1) is UNKNOWN parse_cleanup(780): Unmatched BRACE_CLOSE orig_line is 5, orig_col is 1 parse_cleanup(785): Try the option 'tok_split_gte = true' Debug: uncrustify exited with status: 1