From 70d8a16399ca6df759d20bea302d6dfd710a1df8 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Sun, 3 Mar 2024 19:27:26 +0200 Subject: [PATCH] fix(deps): update how error is detected in CLI jobs Previously a job was declared as having an error if *either* `stderr` was written or exit code was not 0. This proved to be a wrong approach as `stderr` can be used to write purely diagnostic/warning messages. For example, cloning from 'https://git.sr.ht' does not support `--fillter=blob:none`. This results into the `filtering not recognized by server, ignoring` being written to `stderr` all while cloning is successful. The more robust approach is to infer that job had an error *only* if the exit code is not zero. In this case `stderr` should be used as additional information about the error. Case of not trivial `stderr` and zero exit code should be treated as CLI tool decided to print diagnostic/warning messages. --- lua/mini/deps.lua | 51 +++++++----- tests/dir-deps/mocks/spawn.lua | 9 ++- .../tests-test_deps.lua---update()---works | 68 ++++++++-------- tests/test_deps.lua | 81 ++++++++++++------- 4 files changed, 126 insertions(+), 83 deletions(-) diff --git a/lua/mini/deps.lua b/lua/mini/deps.lua index ef930e13..6b57544b 100644 --- a/lua/mini/deps.lua +++ b/lua/mini/deps.lua @@ -565,8 +565,8 @@ MiniDeps.update = function(names, opts) local feedback = opts.force and H.update_feedback_log or H.update_feedback_confirm feedback(lines) - -- Show job errors - H.plugs_show_job_errors(plugs, 'update') + -- Show job warnings and errors + H.plugs_show_job_notifications(plugs, 'update') end --- Clean plugins @@ -606,7 +606,7 @@ MiniDeps.snap_get = function() local plugs = H.plugs_from_names() H.ensure_git_exec() H.plugs_infer_head(plugs) - H.plugs_show_job_errors(plugs, 'computing snapshot') + H.plugs_show_job_notifications(plugs, 'computing snapshot') local snap = {} for _, p in ipairs(plugs) do @@ -640,7 +640,7 @@ MiniDeps.snap_set = function(snap) -- Checkout H.ensure_git_exec() H.plugs_checkout(plugs) - H.plugs_show_job_errors(plugs, 'applying snapshot') + H.plugs_show_job_notifications(plugs, 'applying snapshot') end --- Save snapshot @@ -1028,8 +1028,8 @@ H.plugs_install = function(plugs) vim.tbl_map(function(p) p.job.cwd = p.path end, plugs) H.plugs_checkout(plugs, { exec_hooks = false, all_helptags = true }) - -- Show errors - H.plugs_show_job_errors(plugs, 'installing plugin') + -- Show warnings and errors + H.plugs_show_job_notifications(plugs, 'installing plugin') end H.plugs_download_updates = function(plugs) @@ -1118,8 +1118,13 @@ H.plugs_run_jobs = function(plugs, prepare, process) end end -H.plugs_show_job_errors = function(plugs, action_name) +H.plugs_show_job_notifications = function(plugs, action_name) for _, p in ipairs(plugs) do + local warn = H.cli_stream_tostring(p.job.warn) + if warn ~= '' then + local msg = string.format('Warnings in `%s` during %s\n%s', p.name, action_name, warn) + H.notify(msg, 'WARN') + end local err = H.cli_stream_tostring(p.job.err) if err ~= '' then local msg = string.format('Error in `%s` during %s\n%s', p.name, action_name, err) @@ -1467,7 +1472,6 @@ H.cli_run = function(jobs) local n_total, id_started, n_finished = #jobs, 0, 0 if n_total == 0 then return end - local is_finished = function() return n_total <= n_finished end local run_next run_next = function() @@ -1477,21 +1481,32 @@ H.cli_run = function(jobs) local job = jobs[id_started] local command, cwd, exit_msg = job.command or {}, job.cwd, job.exit_msg - if vim.fn.isdirectory(cwd) == 0 and #job.err == 0 then job.err = { vim.inspect(cwd) .. ' is not a directory.' } end - -- Prepare data for `vim.loop.spawn` local executable, args = command[1], vim.list_slice(command, 2, #command) local process, stdout, stderr = nil, vim.loop.new_pipe(), vim.loop.new_pipe() local spawn_opts = { args = args, cwd = cwd, stdio = { nil, stdout, stderr } } - -- Register job finish and start a new one from the queue local on_exit = function(code) - if code ~= 0 then table.insert(job.err, 1, 'PROCESS EXITED WITH ERROR CODE ' .. code .. '\n') end - if not process:is_closing() then process:close() end + -- Process only not already closing job + if process:is_closing() then return end + process:close() + + -- Process exit code: if 0 treat `stderr` as warning; error otherwise + if code == 0 then + vim.list_extend(job.warn, job.err) + -- NOTE: This is valid as only jobs with `err = {}` are filtered to run + job.err = {} + else + table.insert(job.err, 1, 'ERROR CODE ' .. code .. '\n') + end + + -- Finalize job n_finished = n_finished + 1 if type(exit_msg) == 'string' and #job.err == 0 then H.notify(string.format('(%d/%d) %s', n_finished, n_total, exit_msg)) end + + -- Start next parallel job run_next() end @@ -1499,9 +1514,9 @@ H.cli_run = function(jobs) H.cli_read_stream(stdout, job.out) H.cli_read_stream(stderr, job.err) vim.defer_fn(function() - if is_finished() then return end - pcall(vim.loop.process_kill, process, 15) - if #job.err == 0 then job.err = { 'PROCESS REACHED TIMEOUT.' } end + if not process:is_active() then return end + table.insert(job.err, 'PROCESS REACHED TIMEOUT.') + on_exit(1) end, timeout) end @@ -1509,7 +1524,7 @@ H.cli_run = function(jobs) run_next() end - vim.wait(timeout * n_total, is_finished, 1) + vim.wait(timeout * n_total, function() return n_total <= n_finished end, 1) end H.cli_read_stream = function(stream, feed) @@ -1524,7 +1539,7 @@ end H.cli_stream_tostring = function(stream) return (table.concat(stream):gsub('\n+$', '')) end H.cli_new_job = function(command, cwd, exit_msg) - return { command = command, cwd = cwd, exit_msg = exit_msg, out = {}, err = {} } + return { command = command, cwd = cwd, exit_msg = exit_msg, out = {}, warn = {}, err = {} } end -- Two-stage execution -------------------------------------------------------- diff --git a/tests/dir-deps/mocks/spawn.lua b/tests/dir-deps/mocks/spawn.lua index bbd5879b..f95917c0 100644 --- a/tests/dir-deps/mocks/spawn.lua +++ b/tests/dir-deps/mocks/spawn.lua @@ -2,10 +2,15 @@ _G.process_log = {} local process_id = 1 local new_process = function(pid) + local is_active, is_closing = true, false return { pid = pid, - close = function(_) table.insert(_G.process_log, 'Process ' .. pid .. ' was closed.') end, - is_closing = function(_) return false end, + close = function(_) + table.insert(_G.process_log, 'Process ' .. pid .. ' was closed.') + is_active, is_closing = false, true + end, + is_closing = function(_) return is_closing end, + is_active = function(_) return is_active end, } end diff --git a/tests/screenshots/tests-test_deps.lua---update()---works b/tests/screenshots/tests-test_deps.lua---update()---works index e9ae1a76..92707c9f 100644 --- a/tests/screenshots/tests-test_deps.lua---update()---works +++ b/tests/screenshots/tests-test_deps.lua---update()---works @@ -18,25 +18,25 @@ 17| 18|!!! plugin_3 !!! 19| -20|Error computing origin -21| -22|+++ plugin_2 +++ -23|Path: MOCKDIR/pack/deps/opt/plugin_2 -24|Source: https://new_source/plugin_2 -25|State before: sha2head -26|State after: new2head (master) -27| -28|Pending updates from `master`: -29|< sha2head | 2024-01-02 01:01:01 +0200 | Neo McVim -30| Removed commit in plugin_2. -31|> new2head | 2024-01-02 02:02:02 +0200 | Neo McVim -32| Added commit in plugin_2. -33| -34|--- plugin_1 --- -35|Path: MOCKDIR/pack/deps/opt/plugin_1 -36|Source: https://github.com/user/plugin_1 -37|State: sha1head (main) -38|~ +20|ERROR CODE 1 +21|Error computing origin +22| +23|+++ plugin_2 +++ +24|Path: MOCKDIR/pack/deps/opt/plugin_2 +25|Source: https://new_source/plugin_2 +26|State before: sha2head +27|State after: new2head (master) +28| +29|Pending updates from `master`: +30|< sha2head | 2024-01-02 01:01:01 +0200 | Neo McVim +31| Removed commit in plugin_2. +32|> new2head | 2024-01-02 02:02:02 +0200 | Neo McVim +33| Added commit in plugin_2. +34| +35|--- plugin_1 --- +36|Path: MOCKDIR/pack/deps/opt/plugin_1 +37|Source: https://github.com/user/plugin_1 +38|State: sha1head (main) 39|~ 40| @@ -62,22 +62,22 @@ 19|33333333333333333333333333333333333333333333333333333333333333333333333333333333 20|33333333333333333333333333333333333333333333333333333333333333333333333333333333 21|33333333333333333333333333333333333333333333333333333333333333333333333333333333 -22|55555555555555553333333333333333333333333333333333333333333333333333333333333333 -23|33333333333333666666666666666666666666666666333333333333333333333333333333333333 -24|33333333333333666666666666666666666666666333333333333333333333333333333333333333 -25|33333333333333666666663333333333333333333333333333333333333333333333333333333333 -26|33333333333333666666663222222223333333333333333333333333333333333333333333333333 -27|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +22|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +23|55555555555555553333333333333333333333333333333333333333333333333333333333333333 +24|33333333333333666666666666666666666666666666333333333333333333333333333333333333 +25|33333333333333666666666666666666666666666333333333333333333333333333333333333333 +26|33333333333333666666663333333333333333333333333333333333333333333333333333333333 +27|33333333333333666666663222222223333333333333333333333333333333333333333333333333 28|33333333333333333333333333333333333333333333333333333333333333333333333333333333 -29|77777777777777777777777777777777777777777777777777333333333333333333333333333333 -30|33333333333333333333333333333333333333333333333333333333333333333333333333333333 -31|88888888888888888888888888888888888888888888888888333333333333333333333333333333 -32|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +29|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +30|77777777777777777777777777777777777777777777777777333333333333333333333333333333 +31|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +32|88888888888888888888888888888888888888888888888888333333333333333333333333333333 33|33333333333333333333333333333333333333333333333333333333333333333333333333333333 -34|99999999999999993333333333333333333333333333333333333333333333333333333333333333 -35|33333333666666666666666666666666666666333333333333333333333333333333333333333333 -36|33333333666666666666666666666666666666663333333333333333333333333333333333333333 -37|33333333666666663222222333333333333333333333333333333333333333333333333333333333 -38|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +34|33333333333333333333333333333333333333333333333333333333333333333333333333333333 +35|99999999999999993333333333333333333333333333333333333333333333333333333333333333 +36|33333333666666666666666666666666666666333333333333333333333333333333333333333333 +37|33333333666666666666666666666666666666663333333333333333333333333333333333333333 +38|33333333666666663222222333333333333333333333333333333333333333333333333333333333 39|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 40|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/tests/test_deps.lua b/tests/test_deps.lua index 51593af9..b9de265d 100644 --- a/tests/test_deps.lua +++ b/tests/test_deps.lua @@ -572,7 +572,10 @@ T['add()']['Install']['works'] = function() end T['add()']['Install']['checks for executable Git'] = function() - child.lua([[_G.stdio_queue = { { err = 'No Git'} }]]) + child.lua([[ + _G.stdio_queue = { { err = 'No Git'} } + _G.process_mock_data = { { exit_code = 1 } } + ]]) expect.error(function() add('user/new_plugin') end, 'Could not find executable `git` CLI tool') end @@ -1014,23 +1017,24 @@ T['add()']['Install']['generates help tags'] = function() eq(help_tags, { 'depstest_dep_1_tag', 'depstest_dep_2_tag', 'depstest_new_tag' }) end -T['add()']['Install']['handles process errors'] = function() +T['add()']['Install']['handles process errors and warnings'] = function() child.lua([[ _G.stdio_queue = { - { out = 'git version 2.43.0'}, -- Check Git executable - {}, -- Clone dep_plugin - { err = 'Could not clone' }, -- Clone new_plugin - { out = 'sha2head' }, -- Get `HEAD` in dep_plugin + { out = 'git version 2.43.0'}, -- Check Git executable + { err = 'filtering not recognized by server, ignoring' }, -- Clone dep_plugin + { err = 'Could not clone' }, -- Clone new_plugin + { out = 'sha2head' }, -- Get `HEAD` in dep_plugin } -- Mock non-zero exit code in getting dep_plugin's head - _G.process_mock_data = { [4] = { exit_code = 128 } } + _G.process_mock_data = { [3] = { exit_code = 1 }, [4] = { exit_code = 128 } } ]]) add({ source = 'user/new_plugin', depends = { 'user/dep_plugin' } }) - -- If any error (from `stderr` or exit code) is encountered, all CLI jobs for - -- that particular plugin should not be done + -- Errors should be treated as follows: + -- - If exit code is non-zero, it should error notify it with `stderr` output + -- - If exit code is zero, then process did not error and `stderr` is warning --stylua: ignore local ref_git_spawn_log = { { args = { 'version' }, cwd = child.fn.getcwd() }, @@ -1048,10 +1052,11 @@ T['add()']['Install']['handles process errors'] = function() { '(mini.deps) Installing `new_plugin`', 'INFO' }, { '(mini.deps) (1/2) Installed `dep_plugin`', 'INFO' }, { - '(mini.deps) Error in `dep_plugin` during installing plugin\nPROCESS EXITED WITH ERROR CODE 128', - 'ERROR', + '(mini.deps) Warnings in `dep_plugin` during installing plugin\nfiltering not recognized by server, ignoring', + 'WARN', }, - { '(mini.deps) Error in `new_plugin` during installing plugin\nCould not clone', 'ERROR' }, + { '(mini.deps) Error in `dep_plugin` during installing plugin\nERROR CODE 128', 'ERROR' }, + { '(mini.deps) Error in `new_plugin` during installing plugin\nERROR CODE 1\nCould not clone', 'ERROR' }, } validate_notifications(ref_notify_log) end @@ -1116,8 +1121,8 @@ T['add()']['Install']['respects `config.job.timeout`'] = function() local ref_notify_log = { { '(mini.deps) Installing `new_plugin`', 'INFO' }, { '(mini.deps) (1/2) Installed `new_plugin`', 'INFO' }, - { '(mini.deps) Error in `dep_plugin` during installing plugin\nPROCESS REACHED TIMEOUT.', 'ERROR' }, - { '(mini.deps) Error in `new_plugin` during installing plugin\nPROCESS REACHED TIMEOUT.', 'ERROR' }, + { '(mini.deps) Error in `dep_plugin` during installing plugin\nERROR CODE 1\nPROCESS REACHED TIMEOUT.', 'ERROR' }, + { '(mini.deps) Error in `new_plugin` during installing plugin\nERROR CODE 1\nPROCESS REACHED TIMEOUT.', 'ERROR' }, } validate_notifications(ref_notify_log) end @@ -1190,7 +1195,7 @@ T['update()']['works'] = function() _G.stdio_queue = { { out = 'git version 2.43.0'}, -- Check Git executable { out = 'https://github.com/user/plugin_1' }, -- Get source from `origin` in plugin_1 - {}, -- Set `origin` to source in plugin_2 + { err = 'Some warning' }, -- Set `origin` to source in plugin_2 { err = 'Error computing origin' }, -- Get source from `origin` in plugin_3 { out = 'sha1head' }, -- Get `HEAD` in plugin_1 { out = 'sha2head' }, -- Get `HEAD` in plugin_2 @@ -1206,7 +1211,7 @@ T['update()']['works'] = function() } -- Mock non-trivial fetch duration - _G.process_mock_data = { [9] = { duration = 50 }, [10] = { duration = 40 } } + _G.process_mock_data = { [4] = { exit_code = 1 }, [9] = { duration = 50 }, [10] = { duration = 40 } } ]]) -- Update should be done in parallel @@ -1250,7 +1255,8 @@ T['update()']['works'] = function() { '(mini.deps) Downloading 2 updates', 'INFO' }, { '(mini.deps) (1/2) Downloaded update for `plugin_2`', 'INFO' }, { '(mini.deps) (2/2) Downloaded update for `plugin_1`', 'INFO' }, - { '(mini.deps) Error in `plugin_3` during update\nError computing origin', 'ERROR' }, + { '(mini.deps) Warnings in `plugin_2` during update\nSome warning', 'WARN' }, + { '(mini.deps) Error in `plugin_3` during update\nERROR CODE 1\nError computing origin', 'ERROR' }, } validate_notifications(ref_notify_log) @@ -1263,7 +1269,10 @@ end T['update()']['checks for executable Git'] = function() add('plugin_1') - child.lua([[_G.stdio_queue = { { err = 'No Git'} }]]) + child.lua([[ + _G.stdio_queue = { { err = 'No Git'} } + _G.process_mock_data = { { exit_code = 1 } } + ]]) expect.error(function() update() end, 'Could not find executable `git` CLI tool') end @@ -1380,6 +1389,8 @@ T['update()']['can fold in cofirm buffer'] = function() { out = 'new2head' }, -- Get commit of `checkout` in plugin_2 { out = _G.plugin_2_log }, -- Get log of `checkout` changes in plugin_2 } + + _G.process_mock_data = { [4] = { exit_code = 1 } } ]]) update() @@ -1816,8 +1827,9 @@ T['update()']['respects `config.job.timeout`'] = function() update() local ref_notify_log = { - { '(mini.deps) Error in `plugin_1` during update\nPROCESS REACHED TIMEOUT.', 'ERROR' }, - { '(mini.deps) Error in `plugin_2` during update\nPROCESS REACHED TIMEOUT.', 'ERROR' }, + { '(mini.deps) Downloading 1 update', 'INFO' }, + { '(mini.deps) Error in `plugin_1` during update\nERROR CODE 1\nPROCESS REACHED TIMEOUT.', 'ERROR' }, + { '(mini.deps) Error in `plugin_2` during update\nERROR CODE 1\nPROCESS REACHED TIMEOUT.', 'ERROR' }, } validate_notifications(ref_notify_log) end @@ -1847,20 +1859,20 @@ T['update()']['respects `config.silent`'] = function() validate_notifications({}) end -T['update()']['handles process errors'] = function() +T['update()']['handles process errors and warnings'] = function() add('user/plugin_1') add('user/plugin_2') child.lua([[ _G.stdio_queue = { { out = 'git version 2.43.0'}, -- Check Git executable - {}, -- Set `origin` to source in plugin_1 + { err = 'Some warning' }, -- Set `origin` to source in plugin_1 { err = 'Bad `origin`'}, -- Set `origin` to source in plugin_2 { out = 'sha2head' }, -- Get `HEAD` in plugin_1 } -- Mock non-zero exit code in getting plugin_1's head - _G.process_mock_data = { [4] = { exit_code = 128 } } + _G.process_mock_data = { [3] = { exit_code = 1 }, [4] = { exit_code = 128 } } ]]) update() @@ -1877,8 +1889,9 @@ T['update()']['handles process errors'] = function() -- Should produce notifications local ref_notify_log = { - { '(mini.deps) Error in `plugin_1` during update\nPROCESS EXITED WITH ERROR CODE 128', 'ERROR' }, - { '(mini.deps) Error in `plugin_2` during update\nBad `origin`', 'ERROR' }, + { '(mini.deps) Warnings in `plugin_1` during update\nSome warning', 'WARN' }, + { '(mini.deps) Error in `plugin_1` during update\nERROR CODE 128', 'ERROR' }, + { '(mini.deps) Error in `plugin_2` during update\nERROR CODE 1\nBad `origin`', 'ERROR' }, } validate_notifications(ref_notify_log) end @@ -2029,7 +2042,10 @@ end T['snap_get()']['checks for executable Git'] = function() add('plugin_1') - child.lua([[_G.stdio_queue = { { err = 'No Git'} }]]) + child.lua([[ + _G.stdio_queue = { { err = 'No Git'} } + _G.process_mock_data = { { exit_code = 1 } } + ]]) expect.error(snap_get, 'Could not find executable `git` CLI tool') end @@ -2040,6 +2056,7 @@ T['snap_get()']['handles process errors'] = function() { err = 'Some error' }, -- Get `HEAD` in plugin_1 { out = 'sha2head' }, -- Get `HEAD` in start_plugin } + _G.process_mock_data = { [2] = { exit_code = 1 } } ]]) -- Should still return snapshot but without data for errored plugins @@ -2047,7 +2064,9 @@ T['snap_get()']['handles process errors'] = function() eq(snap, { start_plugin = 'sha2head' }) -- Should show error notifications - validate_notifications({ { '(mini.deps) Error in `plugin_1` during computing snapshot\nSome error', 'ERROR' } }) + validate_notifications({ + { '(mini.deps) Error in `plugin_1` during computing snapshot\nERROR CODE 1\nSome error', 'ERROR' }, + }) end T['snap_set()'] = new_set({ @@ -2086,6 +2105,7 @@ T['snap_set()']['works'] = function() {}, -- Stash in start_plugin {}, -- Checkout in start_plugin } + _G.process_mock_data = { [3] = { exit_code = 1 } } ]]) -- Should apply only to plugins inside current session @@ -2120,7 +2140,7 @@ T['snap_set()']['works'] = function() -- Should produce notifications local ref_notify_log = { { '(mini.deps) (1/1) Checked out `new1head` in `plugin_1`', 'INFO' }, - { '(mini.deps) Error in `plugin_2` during applying snapshot\nError getting HEAD', 'ERROR' }, + { '(mini.deps) Error in `plugin_2` during applying snapshot\nERROR CODE 1\nError getting HEAD', 'ERROR' }, } validate_notifications(ref_notify_log) @@ -2140,7 +2160,10 @@ end T['snap_set()']['checks for executable Git'] = function() add('plugin_1') - child.lua([[_G.stdio_queue = { { err = 'No Git'} }]]) + child.lua([[ + _G.stdio_queue = { { err = 'No Git'} } + _G.process_mock_data = { { exit_code = 1 } } + ]]) expect.error(function() snap_set({}) end, 'Could not find executable `git` CLI tool') end -- knotmirror 0.0.1