diff --git a/aider/commands.py b/aider/commands.py index a0aacedbd..559690011 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -272,13 +272,12 @@ class Commands: git_files = self.coder.repo.get_tracked_files() if self.coder.repo else [] all_matched_files = set() - for word in args.split(): + for word in shlex.split(args): fname = Path(self.coder.root) / word - if fname.exists(): - if fname.is_file(): - all_matched_files.add(str(fname)) - continue - # else we fall through and glob will pickup all files within a dir + if fname.exists() and fname.is_file(): + all_matched_files.add(str(fname)) + continue + # an existing dir will fall through and get recursed by glob matched_files = self.glob_filtered_to_repo(word) if matched_files: @@ -342,7 +341,7 @@ class Commands: self.io.tool_output("Dropping all files from the chat session.") self.coder.abs_fnames = set() - for word in args.split(): + for word in shlex.split(args): matched_files = self.glob_filtered_to_repo(word) if not matched_files: diff --git a/tests/test_commands.py b/tests/test_commands.py index 29976a77b..89524e73b 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -372,3 +372,33 @@ class TestCommands(TestCase): commands.cmd_add(str(fname)) self.assertIn(str(fname.resolve()), coder.abs_fnames) + + def test_cmd_add_abs_filename(self): + with ChdirTemporaryDirectory(): + io = InputOutput(pretty=False, yes=False) + from aider.coders import Coder + + coder = Coder.create(models.GPT35, None, io) + commands = Commands(io, coder) + + fname = Path("file.txt") + fname.touch() + + commands.cmd_add(str(fname.resolve())) + + self.assertIn(str(fname.resolve()), coder.abs_fnames) + + def test_cmd_add_quoted_filename(self): + with ChdirTemporaryDirectory(): + io = InputOutput(pretty=False, yes=False) + from aider.coders import Coder + + coder = Coder.create(models.GPT35, None, io) + commands = Commands(io, coder) + + fname = Path("file with spaces.txt") + fname.touch() + + commands.cmd_add(f'"{fname}"') + + self.assertIn(str(fname.resolve()), coder.abs_fnames)