From cc689290cd226594de79af3bae207b693bdf5a66 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Wed, 23 Mar 2022 17:33:24 +0100 Subject: [PATCH] Autoload: Call the parser directly instead of going via "subshell" This used to call exec_subshell, which has two issues: 1. It creates a command substitution block which shows up in a stack trace 2. It does much more work than necessary This removes a useless "in command substitution" from an error message in an autoloaded file, and it speeds up autoloading a bit (not measurable in actual benchmarks, but microbenchmarks are 2x). --- src/autoload.cpp | 10 +++++++++- tests/checks/syntax-error-location.fish | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/autoload.cpp b/src/autoload.cpp index 339c052ab..d096f5331 100644 --- a/src/autoload.cpp +++ b/src/autoload.cpp @@ -205,6 +205,14 @@ maybe_t autoload_t::resolve_command(const wcstring &cmd, const wcstrin } void autoload_t::perform_autoload(const wcstring &path, parser_t &parser) { + // We do the useful part of what exec_subshell does ourselves + // - we source the file. + // We don't create a buffer or check ifs or create a read_limit + wcstring script_source = L"source " + escape_string(path, ESCAPE_ALL); - exec_subshell(script_source, parser, false /* do not apply exit status */); + auto prev_statuses = parser.get_last_statuses(); + const cleanup_t put_back([&] { + parser.set_last_statuses(prev_statuses); + }); + parser.eval(script_source, io_chain_t{}); } diff --git a/tests/checks/syntax-error-location.fish b/tests/checks/syntax-error-location.fish index bf5c9a9ce..ca483f45c 100644 --- a/tests/checks/syntax-error-location.fish +++ b/tests/checks/syntax-error-location.fish @@ -32,3 +32,15 @@ $fish -c 'echo "unfinished "$(subshell' 2>| string replace -r '.*' '<$0>' # CHECK: # CHECK: # CHECK: < ^> + +echo "function error" >$TMPDIR/error.fish +$fish -c "set -g fish_function_path $(string escape $TMPDIR); error" +# CHECKERR: ~/temp/error.fish (line 1): Missing end to balance this function definition +# CHECKERR: function error +# CHECKERR: ^ +# CHECKERR: from sourcing file ~/temp/error.fish +# CHECKERR: source: Error while reading file '{{.*}}/error.fish' +# CHECKERR: fish: Unknown command: error +# CHECKERR: fish: +# CHECKERR: set -g fish_function_path {{.*}}; error +# CHECKERR: ^