diff --git a/news/path_warn.rst b/news/path_warn.rst new file mode 100644 index 000000000..039a9df88 --- /dev/null +++ b/news/path_warn.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added warning about huge amount of commands in CommandsCache. + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/xonsh/commands_cache.py b/xonsh/commands_cache.py index 3f7b14b1f..b4aa0f832 100644 --- a/xonsh/commands_cache.py +++ b/xonsh/commands_cache.py @@ -7,6 +7,7 @@ and returns whether or not the process can be run in the background (returns True) or must be run the foreground (returns False). """ import os +import sys import time import builtins import argparse @@ -106,6 +107,16 @@ class CommandsCache(cabc.Mapping): for cmd in executables_in(path): key = cmd.upper() if ON_WINDOWS else cmd allcmds[key] = (os.path.join(path, cmd), alss.get(key, None)) + + warn_cnt = builtins.__xonsh__.env.get("COMMANDS_CACHE_SIZE_WARNING") + if warn_cnt: + cnt = len(allcmds) + if cnt > warn_cnt: + print( + f"Warning! Found {cnt:,} executable files in the PATH directories!", + file=sys.stderr, + ) + for cmd in alss: if cmd not in allcmds: key = cmd.upper() if ON_WINDOWS else cmd diff --git a/xonsh/environ.py b/xonsh/environ.py index 8eb80a935..715a8be09 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -1725,6 +1725,13 @@ def DEFAULT_VARS(): "or None / the empty string if traceback logging is not desired. " "Logging to a file is not enabled by default.", ), + "COMMANDS_CACHE_SIZE_WARNING": Var( + is_int, + int, + str, + 10000, + "Number of files on the PATH above which a warning is shown.", + ), } if hasattr(locale, "LC_MESSAGES"):