From 24263ef19b2fce60daca4308e79918565aa5702f Mon Sep 17 00:00:00 2001 From: Hao XIAO Date: Wed, 25 Nov 2026 06:23:49 +0100 Subject: [PATCH] fix(config): fail when the config file to load does not exist load_config() fell back to the default configuration whenever the given path did not exist, so a mistyped --config (or config= path in the API) started a full run with the default models, iteration count and evaluator settings without any warning. Raise FileNotFoundError for a path that is given but missing, and report it from the CLI like a missing program or evaluation file. --- openevolve/cli.py | 4 +++ openevolve/config.py | 11 ++++++-- tests/test_missing_config_file.py | 46 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/test_missing_config_file.py diff --git a/openevolve/cli.py b/openevolve/cli.py index 431f9781d..ad3168e02 100644 --- a/openevolve/cli.py +++ b/openevolve/cli.py @@ -123,6 +123,10 @@ async def main_async() -> int: print(f"Error: Evaluation file '{args.evaluation_file}' not found") return 1 + if args.config and not os.path.exists(args.config): + print(f"Error: Configuration file '{args.config}' not found") + return 1 + # Load base config from file or defaults config = load_config(args.config) diff --git a/openevolve/config.py b/openevolve/config.py index 0c2e78ed7..ddf62c755 100644 --- a/openevolve/config.py +++ b/openevolve/config.py @@ -624,8 +624,15 @@ def to_yaml(self, path: Union[str, Path]) -> None: def load_config(config_path: Optional[Union[str, Path]] = None) -> Config: - """Load configuration from a YAML file or use defaults""" - if config_path and os.path.exists(config_path): + """Load configuration from a YAML file or use defaults + + Raises: + FileNotFoundError: If config_path is given but does not exist + """ + if config_path: + # A mistyped path must not silently run with the default configuration + if not os.path.exists(config_path): + raise FileNotFoundError(f"Configuration file '{config_path}' not found") config = Config.from_yaml(config_path) else: config = Config() diff --git a/tests/test_missing_config_file.py b/tests/test_missing_config_file.py new file mode 100644 index 000000000..820467eef --- /dev/null +++ b/tests/test_missing_config_file.py @@ -0,0 +1,46 @@ +"""A config path that does not exist must not silently run with the defaults.""" + +import asyncio +import contextlib +import io +import os +import tempfile +import unittest +from unittest.mock import patch + +from openevolve import cli +from openevolve.config import Config, load_config + + +class TestMissingConfigFile(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.missing = os.path.join(temp.name, "confg.yaml") + self.program = os.path.join(temp.name, "program.py") + self.evaluator = os.path.join(temp.name, "evaluator.py") + for path in (self.program, self.evaluator): + open(path, "w").close() + + def test_load_config_raises_for_missing_file(self): + with self.assertRaisesRegex(FileNotFoundError, "confg.yaml"): + load_config(self.missing) + + def test_load_config_without_path_uses_defaults(self): + config = load_config(None) + self.assertEqual(config.max_iterations, Config().max_iterations) + + def test_cli_reports_missing_config_and_does_not_run(self): + argv = ["openevolve-run", self.program, self.evaluator, "--config", self.missing] + output = io.StringIO() + with patch("sys.argv", argv), patch.object(cli, "OpenEvolve") as engine_class: + with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output): + exit_code = asyncio.run(cli.main_async()) + self.assertEqual(exit_code, 1, output.getvalue()) + self.assertIn(f"Error: Configuration file '{self.missing}' not found", output.getvalue()) + self.assertNotIn("Traceback", output.getvalue()) + engine_class.assert_not_called() + + +if __name__ == "__main__": + unittest.main()