Skip to content

Commit

Permalink
Make src_paths behave as expected when using --resolve-all-configs
Browse files Browse the repository at this point in the history
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 PyCQA#2045
  • Loading branch information
sudowork committed Jun 6, 2023
1 parent 8f2cd6b commit 1e90580
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/configuration/options.md
Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions isort/settings.py
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_settings.py
Expand Up @@ -241,6 +241,7 @@ def test_find_all_configs(tmpdir):
pyproject_toml = """
[tool.isort]
profile = "hug"
src_paths = ["src"]
"""

isort_cfg = """
Expand Down Expand Up @@ -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")}

0 comments on commit 1e90580

Please sign in to comment.