diff --git a/aider/main.py b/aider/main.py index afb3f836624..d07f0c62a7d 100644 --- a/aider/main.py +++ b/aider/main.py @@ -1130,6 +1130,12 @@ def get_io(pretty): coder.run(with_message=args.message) except SwitchCoder: pass + except ImportError as err: + io.tool_error(str(err)) + io.tool_output("Error loading required imports. Did you install aider properly?") + io.offer_url(urls.install_properly, "Open documentation url for more info?") + analytics.event("exit", reason="Message run import error") + return 1 analytics.event("exit", reason="Completed --message") return @@ -1146,6 +1152,12 @@ def get_io(pretty): io.tool_error(f"Error reading message file: {e}") analytics.event("exit", reason="Message file IO error") return 1 + except ImportError as err: + io.tool_error(str(err)) + io.tool_output("Error loading required imports. Did you install aider properly?") + io.offer_url(urls.install_properly, "Open documentation url for more info?") + analytics.event("exit", reason="Message file import error") + return 1 analytics.event("exit", reason="Completed --message-file") return @@ -1178,6 +1190,12 @@ def get_io(pretty): if switch.kwargs.get("show_announcements") is not False: coder.show_announcements() + except ImportError as err: + io.tool_error(str(err)) + io.tool_output("Error loading required imports. Did you install aider properly?") + io.offer_url(urls.install_properly, "Open documentation url for more info?") + analytics.event("exit", reason="Main coder.run import error") + return 1 def is_first_run_of_new_version(io, verbose=False): diff --git a/tests/basic/test_main.py b/tests/basic/test_main.py index c8966a53671..2bfb52d1706 100644 --- a/tests/basic/test_main.py +++ b/tests/basic/test_main.py @@ -382,6 +382,23 @@ def test_yes(self, mock_run, MockInputOutput): args, kwargs = MockInputOutput.call_args self.assertTrue(args[1]) + @patch("aider.main.InputOutput") + @patch("aider.coders.base_coder.Coder.run") + def test_message_import_error_is_reported(self, mock_run, MockInputOutput): + mock_run.side_effect = ImportError("cannot import name 'BaseTransport' from 'httpx'") + mock_io_instance = MockInputOutput.return_value + + result = main(["--message", "test message"], input=DummyInput(), output=DummyOutput()) + + self.assertEqual(result, 1) + mock_io_instance.tool_error.assert_called_with( + "cannot import name 'BaseTransport' from 'httpx'" + ) + mock_io_instance.tool_output.assert_any_call( + "Error loading required imports. Did you install aider properly?" + ) + mock_io_instance.offer_url.assert_called_once() + @patch("aider.main.InputOutput") @patch("aider.coders.base_coder.Coder.run") def test_default_yes(self, mock_run, MockInputOutput):