Skip to content

Commit 76459da

Browse files
committed
[core] Clear PATH cache when mutating PATH
Mem.SetNamed sets self.clear_path_cache every time 'PATH' is modified. SearchPath checks for self.mem.clear_path_cache on every CachedLookup, clearing the cache if necessary. Fixes: #2445 Signed-off-by: Andrii Sultanov <[email protected]>
1 parent 63ac5b5 commit 76459da

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

core/executor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _RewriteExternToBuiltin(argv):
128128
129129
Why hidden in OSH? Because regex engines can have MINOR syntax
130130
differences, like []] for char classes. But it could be ON in YSH,
131-
specifically so you can AVOID those differences!
131+
specifically so you can AVOID those differences!
132132
133133
Meh: explicit builtin grep / builtin sed is better. Make a note about
134134
performance in doc/ref.
@@ -246,6 +246,10 @@ def LookupReflect(self, name, do_all):
246246
def CachedLookup(self, name):
247247
# type: (str) -> Optional[str]
248248
#log('name %r', name)
249+
if self.mem.clear_path_cache:
250+
self.ClearCache()
251+
self.mem.clear_path_cache = False
252+
249253
if name in self.cache:
250254
return self.cache[name]
251255

core/state.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,8 @@ def __init__(
13851385
from core import sh_init
13861386
self.env_config = sh_init.EnvConfig(self, defaults)
13871387

1388+
self.clear_path_cache = False # a hack. Read and modified by SearchPath
1389+
13881390
def __repr__(self):
13891391
# type: () -> str
13901392
parts = [] # type: List[str]
@@ -2023,6 +2025,12 @@ def SetNamed(self, lval, val, which_scopes, flags=0):
20232025
This can be used to FLIP flags, while NOT changing the variable. Or it
20242026
can also be used to initialize?
20252027
"""
2028+
2029+
# Hack: let SearchPath know it needs to reset the path
2030+
# cache before looking up a command
2031+
if lval.name == 'PATH':
2032+
self.clear_path_cache = True
2033+
20262034
if flags & SetNameref or flags & ClearNameref:
20272035
# declare -n ref=x # refers to the ref itself
20282036
cell, var_frame = self._ResolveNameOnly(lval.name, which_scopes)

0 commit comments

Comments
 (0)