diff --git a/isort/main.py b/isort/main.py index 5a6447d3..db5ebfe5 100644 --- a/isort/main.py +++ b/isort/main.py @@ -1117,9 +1117,8 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = config_trie = find_all_configs(config_dict.pop("config_root", ".")) if "src_paths" in config_dict: - config_dict["src_paths"] = { - Path(src_path).resolve() for src_path in config_dict.get("src_paths", ()) - } + config_dict["src_paths"] = set(config_dict.get("src_paths", ())) + config_dict.setdefault("directory", os.getcwd()) config = Config(**config_dict) if show_config: diff --git a/isort/settings.py b/isort/settings.py index b8281a41..71d912cb 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -458,9 +458,8 @@ def __init__( path_root = Path(combined_config.get("directory", project_root)).resolve() path_root = path_root if path_root.is_dir() else path_root.parent - if "src_paths" not in combined_config: - combined_config["src_paths"] = (path_root / "src", path_root) - else: + + if "src_paths" in combined_config: src_paths: List[Path] = [] for src_path in combined_config.get("src_paths", ()): full_paths = ( @@ -471,6 +470,8 @@ def __init__( src_paths.append(path) combined_config["src_paths"] = tuple(src_paths) + else: + combined_config["src_paths"] = (path_root / "src", path_root) if "formatter" in combined_config: import pkg_resources diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 4180f81c..330fc4a4 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1,10 +1,11 @@ """A growing set of tests designed to ensure isort doesn't have regressions in new versions""" +import os from io import StringIO -import pytest - import isort +import isort.main +import pytest def test_isort_duplicating_comments_issue_1264(): @@ -1900,3 +1901,31 @@ def test_isort_should_produce_the_same_code_on_subsequent_runs_issue_1799(tmpdir assert isort.code(code, config=settings) == isort.code( isort.code(code, config=settings), config=settings ) + + +def test_isort_should_include_all_src_paths_in_config_issue_2001(tmp_path): + code = """import external +import local +""" + + sorted_code = """import external + +import local +""" + + # "src" is used by default, so it needs a different name + src_dir = tmp_path.joinpath("code") + src_dir.mkdir() + + src_file = src_dir.joinpath("needs_changes.py") + src_file.write_text(code) + local_module = src_dir.joinpath("local.py") + local_module.write_text("# Comment") + + # Necessary to test CLI behavior, only affects this test + os.chdir(tmp_path) + + isort.main.main(["."]) + assert src_file.read_text() == code + isort.main.main(["--src", src_dir.name, "."]) + assert src_file.read_text() == sorted_code