Skip to content

Option to set fallback working directory to nearest pyproject.toml dir #760

@avdstaaij

Description

@avdstaaij

Consider the following circumstances:

  • The Python file that is being edited is in a "Python project directory" (a directory with a pyproject.toml).
  • The Python project directory does not include Ruff configuration; user-wide Ruff configuration is active instead.
  • The Python project directory is not the VS Code workspace root.

Visualization:

VS Code workspace root
└── Python project
    ├── src
    │   └── file_being_edited.py
    └── pyproject.toml

In these circumstances, it appears that the extension runs Ruff with the VS Code workspace root as the working directory. This has some negative effects on a few linter rules that may be enabled in the user-wide configuration. The ones I've noticed:

  • FA, UP: These rules rely on the inferred Python version. Although a Python version may be specified in the project's pyproject.toml, Ruff won't detect it, and will instead fall back on the user-wide configuration. (If I understand https://docs.astral.sh/ruff/configuration/#inferring-the-python-version correctly, this is the expected behavior when Ruff's working directory is set to the VS Code root.)

  • I001: First-party imports may not be detected correctly, leading to different formatting of the import list.

It would be nice if there was an option to make the extension set Ruff's working directory to the "nearest" python project directory instead, so that these linting rules work better when using user-wide Ruff configuration.

For example, the Mypy extension (ms-python.mypy-type-checker) has a setting mypy-type-checker.cwd which can be set to ${nearestConfig} to achieve this behavior.

(If there's already a way to make the rules I mentioned work in this situation, I'd love to hear it!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-infoMore information is needed from the issue author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions