From 1e90580c01216d71beb9581d33ee61deecbb67b8 Mon Sep 17 00:00:00 2001 From: Kevin Gao Date: Tue, 6 Jun 2023 16:21:00 -0700 Subject: [PATCH] Make src_paths behave as expected when using --resolve-all-configs When using `--resolve-all-configs`, there is unexpected behavior in that `src_paths` ends up resolving relative to the project root, which defaults to the current working directory. This results in first-party modules being marked as third-party modules in the default case. Under the previous implementation, one possible workaround would be to specify the relative path to config directory (e.g. `relative/path/to/configdir/src`). However, assuming that the most common use of `--resolve-all-configs` is to support multiple sub-projects in the same repository/overall directory, this workaround would now require each sub-project to understand where it lives in the filesystem. This change proposes a fix that sets `directory` on the `config_data` to be the directory containing the used configuration file if not already set. Downstream, this directory is then used to resolve the absolute paths specified by `src_paths`. Fixes #2045 --- docs/configuration/options.md | 2 +- isort/settings.py | 2 ++ tests/unit/test_settings.py | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/configuration/options.md b/docs/configuration/options.md index c9065a6a..946aa5ad 100644 --- a/docs/configuration/options.md +++ b/docs/configuration/options.md @@ -1758,7 +1758,7 @@ Explicitly set the config root for resolving all configs. When used with the --r ## Resolve All Configs -Tells isort to resolve the configs for all sub-directories and sort files in terms of its closest config files. +Tells isort to resolve the configs for all sub-directories and sort files in terms of its closest config files. When using this option, `src_paths` will resolve relative to the directory that contains the config that was used by default. **Type:** Bool **Default:** `False` diff --git a/isort/settings.py b/isort/settings.py index 2c6b94bc..4b32454c 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -820,6 +820,8 @@ def find_all_configs(path: str) -> Trie: config_data = {} if config_data: + if "directory" not in config_data: + config_data["directory"] = dirpath trie_root.insert(potential_config_file, config_data) break diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index 295b30c1..02eddd09 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -241,6 +241,7 @@ def test_find_all_configs(tmpdir): pyproject_toml = """ [tool.isort] profile = "hug" +src_paths = ["src"] """ isort_cfg = """ @@ -280,14 +281,18 @@ def test_find_all_configs(tmpdir): config_info_1 = config_trie.search(str(dir1 / "test1.py")) assert config_info_1[0] == str(setup_cfg_file) assert config_info_1[0] == str(setup_cfg_file) and config_info_1[1]["profile"] == "django" + assert set(Config(**config_info_1[1]).src_paths) == {Path(dir1), Path(dir1, "src")} config_info_2 = config_trie.search(str(dir2 / "test2.py")) assert config_info_2[0] == str(pyproject_toml_file) assert config_info_2[0] == str(pyproject_toml_file) and config_info_2[1]["profile"] == "hug" + assert set(Config(**config_info_2[1]).src_paths) == {Path(dir2, "src")} config_info_3 = config_trie.search(str(dir3 / "test3.py")) assert config_info_3[0] == str(isort_cfg_file) assert config_info_3[0] == str(isort_cfg_file) and config_info_3[1]["profile"] == "black" + assert set(Config(**config_info_3[1]).src_paths) == {Path(dir3), Path(dir3, "src")} config_info_4 = config_trie.search(str(tmpdir / "file4.py")) assert config_info_4[0] == "default" + assert set(Config(**config_info_4[1]).src_paths) == {Path.cwd(), Path.cwd().joinpath("src")}