Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for project_root #2293

Closed
JulienPalard opened this issue Sep 18, 2024 · 2 comments
Closed

Searching for project_root #2293

JulienPalard opened this issue Sep 18, 2024 · 2 comments

Comments

@JulienPalard
Copy link

I found strange that having a pyproject.toml with:

[tool.isort]
some = "config"

behaved differently than just having:

[tool.isort]

So I dug settings.py to try to understand the logic.

The _find_config function tries hard (and succeed) at finding the project root in my case, I typically have a .git/, it's found, nice.

The caller of _find_config have a nice project_root variable, initialized with the right value for me.

But a few lines later I see:

        if "directory" not in combined_config:
            combined_config["directory"] = (
                os.path.dirname(config_settings["source"])
                if config_settings.get("source", None)
                else os.getcwd()
            )

        path_root = Path(combined_config.get("directory", project_root)).resolve()

To simplify I see:

        if "directory" not in combined_config:
            combined_config["directory"] = something

        path_root = Path(combined_config.get("directory", project_root)).resolve()

So directory is always set, so the .get("directory" is superfluous, it can be simplified as:

        if "directory" not in combined_config:
            combined_config["directory"] = something

        path_root = Path(combined_config["directory"]).resolve()

But doing so the only place where project_root was used is lost. So currently, the project_root variable is initialized but never used.

Instead, if there's a config source (being the directory in which the configuration file is found) the config source is used, that explains why when I set a non empty [tool.isort] in my pyproject.toml, my project root is correctly determined.

So my question is: what is the expected behavior for project root finding?

I see two alternatives:

  • Either the current behavior is expected, finding a .git or a .hg just make the search for a config file stop without determining the project root.
  • Either isort should try hard to find the project root and determine it's found when a .git, .hg, pyproject.toml with or without isort config, etc etc is found.

But I have not found a documentation about this, I found #1239 that is related though.

@kurtmckee
Copy link
Contributor

what is the expected behavior for project root finding?

This appears to be documented on the Supported Config Files page:

isort will traverse up to 25 parent directories until it finds a suitable config file. Note that isort will not leave a git or Mercurial repository (checking for a .git or .hg directory). As soon as it finds a file, it stops looking.

If this answers your question, please close this issue to reduce the issue backlog. Thanks!

@kurtmckee
Copy link
Contributor

@DanielNoord I recommend closing this issue. I believe that poster's question has been answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants