Open pfmoore opened 2 years ago
@pfmoore We don't do anything special in the extension that causes it to not find isort
. This might be a bug in isort
itself where running it via runpy.run_module
causes it to skip config file detection.
I will file a bug on isort
. Can you provide details on the relative location of .isort.cfg
compared to the workspace directory? We run isort
with current working directory set to the workspace root.
My setup was .isort.cfg
in the project root, alongside pyproject.toml
. If I run isort
from the command line, via pre-commit
, it successfully reformats my source code. But then, when I open the file in VS Code, it shows errors against all of the import lines, indicating that the isort extension thinks they are formatted incorrectly. Adding a config to the isort extension setting args --profile=black
causes the highlighting to go away.
Let me know if you need more than this, or if there's any way I can do any diagnosis for you.
For what it's worth, I can confirm that isort does pick up the config file when run with runpy:
>>> import runpy
>>> import sys
>>> sys.argv=["isort", "--check", "src"]
>>> runpy.run_module("isort", run_name="__main__")
ERROR: C:\Work\Projects\pkg_metadata\src\pkg_metadata\__init__.py Imports are incorrectly sorted and/or formatted.
(This is using my .isort.cfg
, but with a file formatted using the default config).
I use a pyproject.toml
in the root directory and it seems to be used by isort. At least I just added "isort.args": ["--show-config"],
to my settings and there under "sources"
the pyproject.toml setting appears.
However, after a change in pyproject.toml
I have to call isort Formatter: Restart Server
once before settings changes are applied by isort.
I can try (tomorrow) putting my isort config into pyproject.toml
rather than .isort.cfg
to see if that makes a difference. But that would only be a workaround - I want to keep my config out of pyproject.toml
if possible (for reasons that aren't particularly relevant here).
@pfmoore Can you add "isort.args": ["--show-config"],
to your settings and see if your config is under "sources"
included as one of the last. For pyproject.toml
I can confirm that the settings are used correctly.
Also could you check if the CWD in the Output window -> isort formatter is the one where your config is?
@pfmoore I just tried it myself and it also works with a .isort.cfg
:
[settings]
profile=black
line_length=110
"isort.args": ["--show-config"],
to the settingsisort Formatter: Restart Server
"sources"
:
{
"profile": "black",
"line_length": 110,
"source": "<CWD>/.isort.cfg"
}
So it doesn't seem to be a bug in isort or the extension. It works as expected.
Hmm, when I did that all I got in the isort output was
isort - --show-config --check --filename c:\Work\Projects\pkg_metadata\src\pkg_metadata\__init__.py
CWD Formatter: c:\Work\Projects\pkg_metadata
But if I re-save my settings, I get the following in the isort output:
isort - --check --filename c:\Work\Projects\pkg_metadata\src\pkg_metadata\__init__.py
CWD Formatter: c:\Work\Projects\pkg_metadata
CWD Format Server: c:\Work\Projects\pkg_metadata
sys.path used to run Formatter:
c:\Users\Gustav\.vscode\extensions\ms-python.isort-2022.3.11611002\bundled\formatter
C:\Users\Gustav\AppData\Local\Programs\Python\Python310\python310.zip
C:\Users\Gustav\AppData\Local\Programs\Python\Python310\DLLs
C:\Users\Gustav\AppData\Local\Programs\Python\Python310\lib
C:\Users\Gustav\AppData\Local\Programs\Python\Python310
c:\Work\Projects\pkg_metadata\.venv
c:\Work\Projects\pkg_metadata\.venv\lib\site-packages
c:\Users\Gustav\.vscode\extensions\ms-python.isort-2022.3.11611002\bundled\libs
Settings used to run Formatter:
[
{
"workspace": "file:///c%3A/Work/Projects/pkg_metadata",
"trace": "error",
"args": [
"--show-config"
],
"severity": {},
"path": [],
"interpreter": [
"c:\\Work\\Projects\\pkg_metadata\\.venv\\Scripts\\python.exe"
]
}
]
isort - --show-config --check --filename c:\Work\Projects\pkg_metadata\metadata_class.py
CWD Formatter: c:\Work\Projects\pkg_metadata
isort - --show-config --check --filename c:\Work\Projects\pkg_metadata\src\pkg_metadata\__init__.py
CWD Formatter: c:\Work\Projects\pkg_metadata
I see nothing in there about which config files are used, though...
Moving my config between pyproject.toml
and .isort.cfg
, and even using an invalid profile profile="bloop"
doesn't seem to make any consistent difference...
FYI the code that runs isort can be found at:
@pfmoore are you using the isort we ship in the extension or your own copy?
You must call isort Formatter: Restart Server
after each change to a config.
And the output of --show-config
appears in the python file itself and is around 700 lines long
(And i use the bundled isort.).
@brettcannon as far as I know the one shipped with the extension. How could I tell?
@alkatar21 I followed all of the steps you gave. You say "In the output under "sources"", but I don't see that at all. I can only assume I'm doing something wrong, but I have no idea what. (You say "Use a testfile and format imports" - I'm using right click -> "Sort imports", is that what you meant here?)
@pfmoore Oh, sorry about my inaccuracy, I'm not part of Microsoft or otherwise involved in development and am just trying to help. My complete settings are:
"[python]": {
"editor.codeActionsOnSave": { // Format imports with isort.
"source.organizeImports": true
},
},
"isort.args": ["--show-config"],
I have saved the file and isort runs automatically when saving with this setting. (Then there is the mentioned output)
Right click -> Sort imports: I do not know how that works.
For me there is currently no isort executed then, only with Alt+Shift+o I can choose between sort imports
and isort: Organize imports
.
Ah, that did it. Here's the --show-config
output:
So I guess that means my .isort.cfg
is being read?
I wonder if the errors I was seeing were because I'd not reloaded the extension somehow? I'll do some more experimenting, but it'll have to be tomorrow now.
Yes, your config is read. So that is not the problem.
OK, having reviewed things, it looks pretty clear to me that the problem was that I created my .isort.cfg
and then ran isort via pre-commit
, which tidied up my files, but because I didn't restart the isort server in VS Code, the isort extension thought the corrected files were wrong.
Things just went worse from there, because changing the VS Code settings file appears to restart the isort server automatically, but changing the config file doesn't, so I ended up hopelessly confused as to what was going on 🙁
I have the following suggestions for making the behaviour less surprising:
Apart from those suggestions, I think this can be closed as "user error", though. Thanks for all the help.
@karthiknadig can you change this to a feature request to watch the config file(s) and restart the server as necessary?
[removed, known but different issue]
My project has a
.isort.cfg
file specifyingprofile=black
. The extension doesn't take account of this file, so I need to add the profile to my extension settings as well (and keep the two in line).The extension should respect any of the config locations that isort supports.