From 4414d5c8885b5cd53c7ab42f70bd2824050907f0 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 6 Mar 2018 12:35:14 -0600 Subject: [PATCH] Block custom/user completions for all invalid heads If the head is not a valid, existent command, do not load and run custom completion sources. This applies to both the autosuggestion provider and manual user completions. File-based completions will still be offered. Supersedes #4782 and #4783. Closes #4783. Closes #4782. Closes #2365. --- src/complete.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/complete.cpp b/src/complete.cpp index fadac6d99..83b9714d9 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -28,6 +28,7 @@ #include "builtin.h" #include "common.h" #include "complete.h" +#include "env.h" #include "exec.h" #include "expand.h" #include "fallback.h" // IWYU pragma: keep @@ -842,13 +843,24 @@ bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spop wcstring cmd, path; parse_cmd_string(cmd_orig, path, cmd); - if (this->type() == COMPLETE_DEFAULT) { + env_vars_snapshot_t vars; //does not need to be populated for our needs + // this has to be called before complete_load + if (!function_exists_no_autoload(cmd.c_str(), vars) && !builtin_exists(cmd) + && !path_get_path(cmd, nullptr)) { + //Do not load custom completions if the head does not exist + //This prevents errors caused during the execution of completion providers for + //tools that do not exist. Applies to both manual completions ("cm", "cmd ") + //and automatic completions ("gi" autosuggestion provider -> git) + } + else if (this->type() == COMPLETE_DEFAULT) { ASSERT_IS_MAIN_THREAD(); complete_load(cmd, true); } else if (this->type() == COMPLETE_AUTOSUGGEST && !completion_autoloader.has_tried_loading(cmd)) { // Load this command (on the main thread) - iothread_perform_on_main([&]() { complete_load(cmd, false); }); + iothread_perform_on_main([&]() { + complete_load(cmd, false); + }); } // Make a list of lists of all options that we care about.