diff --git a/isort/api.py b/isort/api.py index abf8bdb1..26ab8f2b 100644 --- a/isort/api.py +++ b/isort/api.py @@ -327,10 +327,11 @@ def check_file( config_trie = config_kwargs.pop("config_trie", None) if config_trie: config_info = config_trie.search(filename) - if config.verbose: + if config_info[0] and config.verbose: print(f"{config_info[0]} used for file {filename}") - file_config = Config(**config_info[1]) + if config_info[0]: + file_config = Config(**config_info[1]) with io.File.read(filename) as source_file: return check_stream( @@ -422,10 +423,11 @@ def sort_file( config_trie = config_kwargs.pop("config_trie", None) if config_trie: config_info = config_trie.search(filename) - if config.verbose: + if config_info[0] and config.verbose: print(f"{config_info[0]} used for file {filename}") - file_config = Config(**config_info[1]) + if config_info[0]: + file_config = Config(**config_info[1]) with io.File.read(filename) as source_file: actual_file_path = file_path or source_file.path diff --git a/isort/main.py b/isort/main.py index 9369ddd1..c6d649db 100644 --- a/isort/main.py +++ b/isort/main.py @@ -1141,6 +1141,7 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) show_diff=show_diff, write_to_stdout=write_to_stdout, extension=ext_format, + disregard_skip=not resolve_all_configs, config_trie=config_trie, ), file_names, @@ -1156,6 +1157,7 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) show_diff=show_diff, write_to_stdout=write_to_stdout, extension=ext_format, + disregard_skip=not resolve_all_configs, config_trie=config_trie, ) for file_name in file_names diff --git a/isort/settings.py b/isort/settings.py index 9c22fa5a..8dd07580 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -774,7 +774,7 @@ def find_all_configs(path: str) -> Trie: Parses and stores any config file encountered in a trie and returns the root of the trie """ - trie_root = Trie("default", {}) + trie_root = Trie("default", {}, root_path=path) for dirpath, _, _ in os.walk(path): for config_file_name in CONFIG_SOURCES: diff --git a/isort/utils.py b/isort/utils.py index 2c4016d0..045af400 100644 --- a/isort/utils.py +++ b/isort/utils.py @@ -20,8 +20,14 @@ class Trie: associated with each file """ - def __init__(self, config_file: str = "", config_data: dict[str, Any] | None = None) -> None: + def __init__( + self, + config_file: str = "", + config_data: dict[str, Any] | None = None, + root_path: str = "", + ) -> None: self.root: TrieNode = TrieNode(config_file, config_data) + self.root_path: Path | None = Path(root_path).resolve() if root_path else None def insert(self, config_file: str, config_data: dict[str, Any]) -> None: resolved_config_path_as_tuple = Path(config_file).parent.resolve().parts @@ -41,7 +47,14 @@ def search(self, filename: str) -> tuple[str, dict[str, Any]]: Returns the closest config relative to filename by doing a depth first search on the prefix tree. """ - resolved_file_path_as_tuple = Path(filename).resolve().parts + resolved_file_path = Path(filename).resolve() + if self.root_path: + try: + resolved_file_path.relative_to(self.root_path) + except ValueError: + return ("", {}) + + resolved_file_path_as_tuple = resolved_file_path.parts temp = self.root diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 79421169..1a1aa838 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -153,6 +153,22 @@ def test_missing_default_section(tmpdir): main.main([str(python_file)]) +def test_resolve_all_configs_honors_skip_for_direct_file(tmp_path, capsys): + config_file = tmp_path / "pyproject.toml" + config_file.write_text("[tool.isort]\nextend_skip = ['a.py']\n") + python_file = tmp_path / "a.py" + original_content = "import sys\nimport os\n" + python_file.write_text(original_content) + + main.main([str(python_file), "--resolve-all-configs", "--verbose"]) + + out, error = capsys.readouterr() + assert not error + assert "default used for file" not in out + assert "Skipped 1 files" in out + assert python_file.read_text() == original_content + + def test_ran_against_root(): with pytest.raises(SystemExit): main.main(["/"])