From 107d466fadc962678aed2bc2e1b2299798ba9020 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Wed, 15 Nov 2023 08:13:24 +0100 Subject: [PATCH] tools: use shared contexts in vpm's update, to resolve dependencies during threaded module updates (#19878) --- cmd/tools/vpm/update.v | 54 ++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/cmd/tools/vpm/update.v b/cmd/tools/vpm/update.v index eeb79df1c4..0db1007360 100644 --- a/cmd/tools/vpm/update.v +++ b/cmd/tools/vpm/update.v @@ -4,53 +4,55 @@ import os import sync.pool import v.help -pub struct ModUpdateInfo { +struct UpdateSession { + idents []string +} + +pub struct UpdateResult { mut: - name string - final_path string - has_err bool + has_err bool } fn vpm_update(query []string) { if settings.is_help { help.print_and_exit('update') } - modules := if query.len == 0 { + idents := if query.len == 0 { get_installed_modules() } else { query.clone() } if settings.is_verbose { - vpm_update_verbose(modules) + vpm_update_verbose(idents) return } mut pp := pool.new_pool_processor(callback: update_module) - pp.work_on_items(modules) + ctx := UpdateSession{idents} + pp.set_shared_context(ctx) + pp.work_on_items(idents) mut errors := 0 - for res in pp.get_results[ModUpdateInfo]() { + for res in pp.get_results[UpdateResult]() { if res.has_err { errors++ continue } - resolve_dependencies(get_manifest(res.final_path), modules) } if errors > 0 { exit(1) } } -fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo { - mut result := &ModUpdateInfo{ - name: pp.get_item[string](idx) - } - name := get_name_from_url(result.name) or { result.name } - result.final_path = get_path_of_existing_module(result.name) or { +fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &UpdateResult { + mut result := &UpdateResult{} + ident := pp.get_item[string](idx) + name := get_name_from_url(ident) or { ident } + install_path := get_path_of_existing_module(ident) or { vpm_error('failed to find path for `${name}`.', verbose: true) result.has_err = true return result } - println('Updating module `${name}` in `${fmt_mod_path(result.final_path)}` ...') - vcs := vcs_used_in_dir(result.final_path) or { + println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...') + vcs := vcs_used_in_dir(install_path) or { vpm_error('failed to find version control system for `${name}`.', verbose: true) result.has_err = true return result @@ -60,24 +62,24 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo { vpm_error(err.msg()) return result } - cmd := '${vcs.cmd} ${vcs.args.path} "${result.final_path}" ${vcs.args.update}' + cmd := '${vcs.cmd} ${vcs.args.path} "${install_path}" ${vcs.args.update}' vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}') res := os.execute_opt(cmd) or { result.has_err = true - vpm_error('failed to update module `${name}` in `${result.final_path}`.', details: err.msg()) + vpm_error('failed to update module `${name}` in `${install_path}`.', details: err.msg()) return result } vpm_log(@FILE_LINE, @FN, 'cmd output: ${res.output.trim_space()}') - increment_module_download_count(name) or { - result.has_err = true - vpm_error(err.msg(), verbose: true) - } + // Don't bail if the download count increment has failed. + increment_module_download_count(name) or { vpm_error(err.msg(), verbose: true) } + ctx := unsafe { &UpdateSession(pp.get_shared_context()) } + resolve_dependencies(get_manifest(install_path), ctx.idents) return result } -fn vpm_update_verbose(modules []string) { +fn vpm_update_verbose(idents []string) { mut errors := 0 - for mod in modules { + for mod in idents { name := get_name_from_url(mod) or { mod } install_path := get_path_of_existing_module(mod) or { continue } println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...') @@ -101,7 +103,7 @@ fn vpm_update_verbose(modules []string) { errors++ vpm_error(err.msg(), verbose: true) } - resolve_dependencies(get_manifest(install_path), modules) + resolve_dependencies(get_manifest(install_path), idents) } if errors > 0 { exit(1)