Skip to content

Commit

Permalink
fix: improve callAtomic error logging (#1110)
Browse files Browse the repository at this point in the history
* Add failing test for bug

Neovim seems to be completely ignoring any changes made in insert mode,
which is causing neovim's buffer to get out of sync with the version of
the file in VSCode.

* Change Neovim version check

The issue with insert mode changes getting ignored is caused by
nvim_buf_set_text not being recognized by an old Neovim version
installed on my computer (somewhere around 0.5.0), so make this more
obvious by reporting an error if there the Neovim version than the
current minimum required version (which is 0.8.0 according to the
readme).

* Add better error reporting for callAtomic

The previous bug caused by nvim_buf_set_text not existing on older
Neovim could have been noticed if you saw the error messages returned by
Neovim, which included an "Invalid method: nvim_buf_set_text" error.

Make bugs like this easier to find in the future by correctly logging
those Neovim-returned errors.

Also silence errors due to nvim_win_close calls since there seem to be
several errors mentioning an invalid window ID being passed to it and
those errors are currently probably more annoying than helpful.

* Run CI on minimum supported Neovim version

Make bugs involving old Neovim versions more apparent by running CI on
the oldest supported version, so that we don't accidentally end up
relying on features introduced in later versions.

* chore: tweaks

---------

Co-authored-by: wongxy <xiyao.wong@foxmail.com>
  • Loading branch information
pranavrajpal and xiyaowong committed Oct 30, 2023
1 parent 5385c25 commit 91c0169
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,26 @@ export async function callAtomic(
requests: [string, unknown[]][],
logger: ILogger,
): Promise<void> {
const res = await client.callAtomic(requests);
const errors: string[] = [];
if (res && Array.isArray(res) && Array.isArray(res[0])) {
res[0].forEach((res, idx) => {
if (res) {
const call = requests[idx];
const requestName = call?.[0];
if (requestName !== "nvim_input") {
errors.push(`${call?.[0] || "Unknown"}: ${res}`);
}
}
});
// The type annotation in the Neovim node client seems to be wrong
// (see https://neovim.io/doc/user/api.html for the correct type for nvim_call_atomic)
const res = (await client.callAtomic(requests)) as unknown as [unknown[], [number, unknown, string] | null];
// Should never reach here if neovim is behaving correctly
if (!(res && Array.isArray(res) && Array.isArray(res[0]))) {
logger.error(`Unexpected result from nvim_call_atomic`);
return;
}
if (errors.length) {
logger.error(`\n${errors.join("\n")}`);
const returned_errors = res[1];
if (returned_errors) {
const [failing_call_idx, err_type, err_msg] = returned_errors;
const call = requests[failing_call_idx];
const requestName = call[0];
const errMsg = `${requestName}: ${err_msg} (Error type: ${err_type})`;
// TODO: Determine cause for errors for both of these requests
if (requestName === "nvim_input" || requestName === "nvim_win_close") {
logger.warn(errMsg);
} else {
logger.error(errMsg);
}
}
}

Expand Down

0 comments on commit 91c0169

Please sign in to comment.