From 07d3545d4019842596c44978729c8b1c4c29682c Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Thu, 22 May 2025 20:49:36 +0800 Subject: [PATCH 1/2] fix(cli): make sure global json5 config is loaded --- src/vectorcode/cli_utils.py | 16 +++++++++++----- src/vectorcode/subcommands/init.py | 4 ++-- tests/conftest.py | 8 ++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/vectorcode/cli_utils.py b/src/vectorcode/cli_utils.py index 5957fe80..c6b3cb5a 100644 --- a/src/vectorcode/cli_utils.py +++ b/src/vectorcode/cli_utils.py @@ -18,8 +18,10 @@ logger = logging.getLogger(name=__name__) -GLOBAL_CONFIG_PATH = os.path.join( - os.path.expanduser("~"), ".config", "vectorcode", "config.json" +GLOBAL_CONFIG_DIR = os.path.join( + os.path.expanduser("~"), + ".config", + "vectorcode", ) GLOBAL_INCLUDE_SPEC = os.path.join( os.path.expanduser("~"), ".config", "vectorcode", "vectorcode.include" @@ -442,10 +444,14 @@ def expand_envs_in_dict(d: dict): async def load_config_file(path: Optional[Union[str, Path]] = None): - """Load config file from ~/.config/vectorcode/config.json""" + """Load config file from ~/.config/vectorcode/config.json(5)""" if path is None: - path = GLOBAL_CONFIG_PATH - if os.path.isfile(path): + for name in ("config.json5", "config.json"): + p = os.path.join(GLOBAL_CONFIG_DIR, name) + if os.path.isfile(p): + path = str(p) + break + if path and os.path.isfile(path): logger.debug(f"Loading config from {path}") with open(path) as fin: content = fin.read() diff --git a/src/vectorcode/subcommands/init.py b/src/vectorcode/subcommands/init.py index a1b61067..7314f73d 100644 --- a/src/vectorcode/subcommands/init.py +++ b/src/vectorcode/subcommands/init.py @@ -8,11 +8,11 @@ from pathlib import Path from typing import Optional -from vectorcode.cli_utils import GLOBAL_CONFIG_PATH, Config, find_project_root +from vectorcode.cli_utils import GLOBAL_CONFIG_DIR, Config, find_project_root logger = logging.getLogger(name=__name__) -__GLOBAL_HOOKS_PATH = Path(GLOBAL_CONFIG_PATH).parent / "hooks" +__GLOBAL_HOOKS_PATH = Path(GLOBAL_CONFIG_DIR) / "hooks" # Keys: name of the hooks, ie. `pre-commit` diff --git a/tests/conftest.py b/tests/conftest.py index d956d1d5..45323514 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,11 @@ import pytest -from vectorcode.cli_utils import GLOBAL_CONFIG_PATH +from vectorcode.cli_utils import GLOBAL_CONFIG_DIR @pytest.fixture(autouse=True) def restore_global_config_path(): - global GLOBAL_CONFIG_PATH - original_global_config_path = GLOBAL_CONFIG_PATH + global GLOBAL_CONFIG_DIR + original_global_config_path = GLOBAL_CONFIG_DIR yield - GLOBAL_CONFIG_PATH = original_global_config_path + GLOBAL_CONFIG_DIR = original_global_config_path From 4681428348cc2239466be5ba20611ec74b8019c7 Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Thu, 22 May 2025 21:36:33 +0800 Subject: [PATCH 2/2] tests for global config --- tests/test_cli_utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_cli_utils.py b/tests/test_cli_utils.py index 4df02a6f..1d9054b9 100644 --- a/tests/test_cli_utils.py +++ b/tests/test_cli_utils.py @@ -5,6 +5,7 @@ import pytest +from vectorcode import cli_utils from vectorcode.cli_utils import ( CliAction, Config, @@ -207,6 +208,29 @@ async def test_load_config_file_invalid_json(): await load_config_file(config_path) +@pytest.mark.asyncio +async def test_load_from_default_config(): + for name in ("config.json5", "config.json"): + with ( + tempfile.TemporaryDirectory() as fake_home, + ): + os.environ.update({"HOME": fake_home}) + config_path = os.path.join(fake_home, ".config", "vectorcode", name) + config_dir = os.path.join(fake_home, ".config", "vectorcode") + setattr( + cli_utils, + "GLOBAL_CONFIG_DIR", + config_dir, + ) + os.makedirs(config_dir, exist_ok=True) + config_content = '{"db_url": "http://default.url:8000"}' + with open(config_path, "w") as fin: + fin.write(config_content) + + config = await load_config_file() + assert config.db_url == "http://default.url:8000" + + @pytest.mark.asyncio async def test_load_config_file_invalid_config(): with tempfile.TemporaryDirectory() as temp_dir: