From 6e039889124b1016920cf9c395c3db732917d26f Mon Sep 17 00:00:00 2001 From: Andy C Date: Sat, 21 May 2022 11:21:06 -0400 Subject: [PATCH] [builtin/shvar] Clear the PATH cache when modifying PATH So that shvar PATH='' { ... } can be used to make all binaries invisible. This mechanism is for config files #951. [doc] Plan out more of Oil as well. QTT builtins. --- core/shell.py | 8 ++++---- doc/oil-help-topics.md | 11 +++++++++-- osh/builtin_pure.py | 9 +++++++-- spec/oil-config.test.sh | 9 ++++----- test/spec.sh | 2 +- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/core/shell.py b/core/shell.py index 4d51257656..493aab8aff 100644 --- a/core/shell.py +++ b/core/shell.py @@ -183,11 +183,11 @@ def AddProcess( b[builtin_i.forkwait] = builtin_process.ForkWait(shell_ex) -def AddOil(b, mem, cmd_ev, errfmt, procs, arena): - # type: (Dict[int, vm._Builtin], state.Mem, cmd_eval.CommandEvaluator, ui.ErrorFormatter, Dict[str, Proc], alloc.Arena) -> None +def AddOil(b, mem, search_path, cmd_ev, errfmt, procs, arena): + # type: (Dict[int, vm._Builtin], state.Mem, state.SearchPath, cmd_eval.CommandEvaluator, ui.ErrorFormatter, Dict[str, Proc], alloc.Arena) -> None b[builtin_i.append] = builtin_oil.Append(mem, errfmt) - b[builtin_i.shvar] = builtin_pure.Shvar(mem, cmd_ev) + b[builtin_i.shvar] = builtin_pure.Shvar(mem, search_path, cmd_ev) b[builtin_i.push_registers] = builtin_pure.PushRegisters(mem, cmd_ev) b[builtin_i.write] = builtin_oil.Write(mem, errfmt) @@ -452,7 +452,7 @@ def Main(lang, arg_r, environ, login_shell, loader, line_input): cmd_ev = cmd_eval.CommandEvaluator(mem, exec_opts, errfmt, procs, assign_b, arena, cmd_deps) - AddOil(builtins, mem, cmd_ev, errfmt, procs, arena) + AddOil(builtins, mem, search_path, cmd_ev, errfmt, procs, arena) # PromptEvaluator rendering is needed in non-interactive shells for @P. prompt_ev = prompt.Evaluator(lang, version_str, parse_ctx, mem) diff --git a/doc/oil-help-topics.md b/doc/oil-help-topics.md index b5072d85b1..0a7fada435 100644 --- a/doc/oil-help-topics.md +++ b/doc/oil-help-topics.md @@ -111,6 +111,7 @@ feature is **unimplemented**. [Shell State] oil-cd oil-shopt compatible, and takes a block shvar Temporary modify global settings push-registers Save registers like $?, PIPESTATUS + X push-procs Limit the procs visible [Modules] runproc Run a proc; use as main entry point module guard against duplicate 'source' use change first word lookup @@ -120,7 +121,13 @@ feature is **unimplemented**. X fopen Open multiple streams, takes a block X log X die common functions (polyfill) [Data Formats] json X qtt -X [Testing] X describe Test harness +X [QTT] where pick rows; dplyr filter() + select pick columns + group-by add a column with a group ID [ext] + sort-by sort by columns; dplyr arrange() [ext] + summarize count, sum, histogram, etc. [ext] +X [Testing] describe Test harness + assert takes an expression X [External Lang] BEGIN END when (awk) rule (make) each (xargs) fs (find) ``` @@ -233,7 +240,7 @@ X [String] find() sub() join() [Arrays] X index() append() extend() [Assoc Arrays] @keys() @values() [Introspection] shvar_get() -X [Config Gen] parse_file() eval_to_dict() block_to_str() +X [Config Gen] parse_config() eval_to_dict() block_to_str() X [Better Syntax] lstrip() rstrip() lstripglob() rstripglob() upper() lower() strftime() diff --git a/osh/builtin_pure.py b/osh/builtin_pure.py index e4b8f7c3a7..580ea87723 100644 --- a/osh/builtin_pure.py +++ b/osh/builtin_pure.py @@ -619,9 +619,10 @@ def Run(self, cmd_val): class Shvar(vm._Builtin): - def __init__(self, mem, cmd_ev): - # type: (state.Mem, CommandEvaluator) -> None + def __init__(self, mem, search_path, cmd_ev): + # type: (state.Mem, SearchPath, CommandEvaluator) -> None self.mem = mem + self.search_path = search_path # to clear PATH self.cmd_ev = cmd_ev # To run blocks def Run(self, cmd_val): @@ -645,6 +646,10 @@ def Run(self, cmd_val): raise error.Usage('Expected name=value', span_id=arg_spids[i]) pairs.append((name, s)) + # Important fix: shvar PATH='' { } must make all binaries invisible + if name == 'PATH': + self.search_path.ClearCache() + with state.ctx_Shvar(self.mem, pairs): unused = self.cmd_ev.EvalBlock(block) diff --git a/spec/oil-config.test.sh b/spec/oil-config.test.sh index 6331dc5577..5ab94b8d7f 100644 --- a/spec/oil-config.test.sh +++ b/spec/oil-config.test.sh @@ -135,16 +135,14 @@ shopt --set parse_brace echo hi > file -# TODO: Fix this cache -# This CACHES the lookup. Bad. -# Do we turn this off in Oil? -cp -v file /tmp +# Note: this CACHES the lookup, so shvar has to clear cache when modifying it +cp -v file /tmp >&2 echo status=$? # TODO: implement this, and call it whenever shvar mutates PATH? # what about when PATH is mutated? No leave it out for now. -hash -d # clear the cache +# hash -r # clear the cache, no longer necessary shvar PATH='' { cp -v file /tmp @@ -160,6 +158,7 @@ cp -v file /tmp >&2 echo status=$? ## STDOUT: +status=0 status=127 status=127 status=0 diff --git a/test/spec.sh b/test/spec.sh index 80e756e88b..98f6a1ad4e 100755 --- a/test/spec.sh +++ b/test/spec.sh @@ -894,7 +894,7 @@ oil-blocks() { } oil-config() { - sh-spec spec/oil-config.test.sh --osh-failures-allowed 3 \ + sh-spec spec/oil-config.test.sh --osh-failures-allowed 2 \ $OSH_LIST "$@" }