diff --git a/isort/main.py b/isort/main.py index d04794008..d30052bcb 100644 --- a/isort/main.py +++ b/isort/main.py @@ -1067,7 +1067,8 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) config_trie: Trie | None = None if resolve_all_configs: - config_trie = find_all_configs(config_dict.pop("config_root", ".")) + config_root = config_dict.pop("config_root", ".") + config_trie = find_all_configs(config_root, config_dict) if "src_paths" in config_dict: # Keep CLI-provided values as-is so wildcard patterns can be expanded later diff --git a/isort/settings.py b/isort/settings.py index 021f335b0..dcc939235 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -782,13 +782,20 @@ def _find_config(path: str) -> tuple[str, dict[str, Any]]: return (path, {}) -def find_all_configs(path: str) -> Trie: +def find_all_configs(path: str, config_overrides: dict[str, Any] | None = None) -> Trie: """ Looks for config files in the path provided and in all of its sub-directories. Parses and stores any config file encountered in a trie and returns the root of - the trie + the trie. Any config_overrides take precedence over the settings of every config file. + The settings_path and settings_file overrides are ignored, so that each file keeps the + config file found nearest to it. """ - trie_root = Trie("default", {}) + config_overrides = { + key: value + for key, value in (config_overrides or {}).items() + if key not in ("settings_path", "settings_file") + } + trie_root = Trie("default", config_overrides) for dirpath, _, _ in os.walk(path): for config_file_name in CONFIG_SOURCES: @@ -807,7 +814,7 @@ def find_all_configs(path: str) -> Trie: config_data = {} if config_data: - trie_root.insert(potential_config_file, config_data) + trie_root.insert(potential_config_file, {**config_data, **config_overrides}) break return trie_root diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 7836b5413..249028c3e 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -213,6 +213,36 @@ def test_config_root_interaction_with_resolve_all_configs(tmp_path: Path) -> Non main.main([str(python_file), "--config-root", str(tmp_path), "--resolve-all-configs"]) +def test_resolve_all_configs_applies_command_line_options(tmp_path: Path) -> None: + project_a = tmp_path / "project_a" + project_b = tmp_path / "project_b" + project_a.mkdir() + project_b.mkdir() + (project_a / ".isort.cfg").write_text("[settings]\nforce_sort_within_sections = true\n") + (project_b / ".isort.cfg").write_text("[settings]\nline_length = 100\n") + file_a = project_a / "a.py" + file_b = project_b / "b.py" + file_c = tmp_path / "c.py" + for python_file in (file_a, file_b, file_c): + python_file.write_text("from os import path, sep\nimport sys\n") + + main.main( + [ + str(file_a), + str(file_b), + str(file_c), + "--force-single-line-imports", + "--resolve-all-configs", + "--config-root", + str(tmp_path), + ] + ) + + assert file_a.read_text() == "from os import path\nfrom os import sep\nimport sys\n" + assert file_b.read_text() == "import sys\nfrom os import path\nfrom os import sep\n" + assert file_c.read_text() == "import sys\nfrom os import path\nfrom os import sep\n" + + def test_main(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: base_args = [ "--sp", diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index d9e33dec6..de57d3765 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -329,6 +329,29 @@ def test_as_bool() -> None: settings._as_bool("truthy") +def test_find_all_configs_ignores_settings_path_overrides(tmp_path: Path) -> None: + project = tmp_path / "project" + project.mkdir() + (project / ".isort.cfg").write_text("[settings]\nprofile=black\n", "utf-8") + + config_trie = settings.find_all_configs( + str(tmp_path), + { + "line_length": 100, + "settings_path": str(tmp_path), + "settings_file": str(tmp_path / "other.cfg"), + }, + ) + + project_config = config_trie.search(str(project / "test.py"))[1] + assert project_config["profile"] == "black" + assert project_config["line_length"] == 100 + assert "settings_path" not in project_config + assert "settings_file" not in project_config + + assert config_trie.search(str(tmp_path / "test.py")) == ("default", {"line_length": 100}) + + def test_find_all_configs(tmp_path: Path) -> None: setup_cfg = """ [isort]