Skip to content

Commit

Permalink
[builtin/shvar] Clear the PATH cache when modifying PATH
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Andy C committed May 21, 2022
1 parent 5351948 commit 6e03988
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions doc/oil-help-topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
```
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions osh/builtin_pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions spec/oil-config.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -160,6 +158,7 @@ cp -v file /tmp >&2
echo status=$?

## STDOUT:
status=0
status=127
status=127
status=0
Expand Down
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
}

Expand Down

0 comments on commit 6e03988

Please sign in to comment.