microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
87 stars 21 forks source link

`python.linting.cwd`-equivalent setting for new extensions #141

Open dmartin opened 2 years ago

dmartin commented 2 years ago

Hi, I have a monorepo workspace where the Python portion (including config files like pyproject.toml, isort.cfg, .flake8) is within a subdirectory (for example, ${workspaceRoot}/python/).

With the previous all-in-one behavior of the Python extension, I could set python.linting.cwd and all of these tools (black, flake8, isort, mypy, etc.) would pick up their configuration files appropriately.

With the new independent LSP extensions (I didn't want to clutter the repos, but this applies to the Black and Flake8 extensions as well), I can't find a way to set the cwd that linters run in. In the output panel, they always appear to run from the root of the repo.

For some of these tools, I might be able to pass an argument to override the config file to an absolute path, like "isort.args":["--settings-file", "python/pyproject.toml"], but the command-line flag to specify an explicit settings file is different for each tool, and sometimes weird behavior is observed when not run from the expected cwd (for example, a flake8 plugin I'm using doesn't work with the --config argument). Ideally, I'd like there to be a configurable cwd setting for each of these new extensions.

karthiknadig commented 2 years ago

@dmartin Can you try "isort.args":["--settings-file", "${workspaceFolder}/python/pyproject.toml"]? That should work, the isort.args settings expands ${workspaceFolder}.

I will discuss this with the team on what we should do about support a CWD setting.

karthiknadig commented 2 years ago

@luabud There are two things here to discuss: Adding a new cwd setting for each extension, and Second one is honoring python.linting.cwd

mgx0 commented 11 months ago

hi, any success here? the project is being linted from the root of the repo after I have switched to new vscode pylint extension. I seem not to be able to set something like this:

"pylint.cwd": "${workspaceFolder}/subfolder"

if I want it to get working, i need to change all imports to include "subfolder." prefix in addition to what worked before

StuartBertram commented 8 months ago

I've enabled isort-on-save and I think I'm hitting a related problem.

We have multiple sub-projects in a monorepo (library code plus Lambda functions). Running poetry run isort . locally is fine and sorts the current module as "first-party". But the built-in VSCode isort sorting puts first-party with third-party. I fixed this for the Lambdas (which all have an api/ module root) by using the --project switch set to api. But that doesn't help with the library code. And we have Lambda L depending on Library C that depends on Library B that depends on Library A, so we can't just add all of the modules to the path because then Library C will consider Library B and A's code as first-party when it should be third-party.

Unfortunately, I don't think we can use --settings-file mentioned earlier because the top-level pyproject.toml still gives the wrong behaviour and a sub-project pyproject.toml would only fix it for that project.

I've also tried the --config-root and --resolve-all-configs switches, but it appears that the stdin/stdout process means that it doesn't detect which sub-project the code was in and hence what is first-party.

[Edit] I've just tried Ruff, which is suggested in #346, but it doesn't handle the test code correctly - code in workspace/subproject/tests/ that does import module_under_test to import from workspace/subproject/module_under_test/ sorts them along with the third-party modules, whereas isort keeps your module under test as first-party even in your tests directory.

Y-Haneji commented 6 months ago

@karthiknadig Any progress? The isort extension doesn't work well with packages under a subdirectory because the extension considers the top directory as the current working directory.

[Example] When I have a module ${workspaceRoot}.python.module.py, the extension doesn't sort imports of the below script.

import module
import os
Y-Haneji commented 6 months ago

@luabud There are two things here to discuss: Adding a new cwd setting for each extension, and Second one is honoring python.linting.cwd

I think the second option is more convenient.

karthiknadig commented 6 months ago

@Y-Haneji Try the Ruff extension, it implements isort import organization. We recommend using that implementation over the pure python as it runs faster and is more configurable.

Use the following settings to ensure only Ruff is used if you want to go that way.

    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports.ruff": true
        }
    }
Y-Haneji commented 6 months ago

@karthiknadig Thank you for the recommendation! I've installed it, but how can I teach the modified current working directory to the ruff extension?

karthiknadig commented 6 months ago

Ask here for more help on this: https://github.com/astral-sh/ruff-vscode

Y-Haneji commented 6 months ago

I see. Thanks for your help.