From 22b151fdd000ed62c28cd6905eef296f8bc8c056 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 21:19:03 -0700 Subject: [PATCH] aider: Added try-except block to handle ClassNotFound exception in guess_lexer_for_filename function. # Context: USER: Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/Users/gauthier/Projects/aider/aider/main.py", line 74, in status = main() ^^^^^^ File "/Users/gauthier/Projects/aider/aider/main.py", line 70, in main coder.run() File "/Users/gauthier/Projects/aider/aider/coder.py", line 185, in run self.run_loop() File "/Users/gauthier/Projects/aider/aider/coder.py", line 200, in run_loop inp = get_input(self.history_file, self.abs_fnames, self.commands) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/gauthier/Projects/aider/aider/getinput.py", line 92, in get_input completer_instance = FileContentCompleter(fnames, commands) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/gauthier/Projects/aider/aider/getinput.py", line 24, in __init__ lexer = guess_lexer_for_filename(fname, content) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/pygments/lexers/__init__.py", line 282, in guess_lexer_for_filename raise ClassNotFound('no lexer for filename %r found' % fn) pygments.util.ClassNotFound: no lexer for filename '.flake8' found ASSISTANT: It seems that the `guess_lexer_for_filename` function from Pygments is raising a `ClassNotFound` exception because it cannot find a lexer for the '.flake8' file. To fix this issue, you can add a try-except block around the `guess_lexer_for_filename` function call to handle the exception and continue processing other files. Here's the change you need to make in the `aider/getinput.py` file: ```python aider/getinput.py <<<<<<< ORIGINAL lexer = guess_lexer_for_filename(fname, content) ======= try: lexer = guess_lexer_for_filename(fname, content) except ClassNotFound: continue >>>>>>> UPDATED ``` This change will catch the `ClassNotFound` exception and continue processing the remaining files without raising an error. --- aider/getinput.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aider/getinput.py b/aider/getinput.py index ca453e6fa..216d220d8 100644 --- a/aider/getinput.py +++ b/aider/getinput.py @@ -21,7 +21,10 @@ class FileContentCompleter(Completer): for fname in fnames: with open(fname, "r") as f: content = f.read() - lexer = guess_lexer_for_filename(fname, content) + try: + lexer = guess_lexer_for_filename(fname, content) + except ClassNotFound: + continue tokens = list(lexer.get_tokens(content)) self.words.update(token[1] for token in tokens if token[0] in Token.Name)