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

isort not honoring setup.cfg #5840

Closed
janosh opened this issue May 30, 2019 · 10 comments
Closed

isort not honoring setup.cfg #5840

janosh opened this issue May 30, 2019 · 10 comments
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug good first issue

Comments

@janosh
Copy link

janosh commented May 30, 2019

Environment data

  • VS Code version: 1.34.0
  • Extension version (available under the Extensions sidebar): 2019.5.17059 (28 May 2019)
  • OS and version: macOS 10.14.4
  • Python version: 3.6.7 (miniconda)

Issue

I added

  "editor.codeActionsOnSave": { "source.organizeImports": true }

to my user settings to sort imports with isort. I'm also using black as a code formatter. On multiline imports these two tools format imports very differently, leading to alternating formatting and jumping code from one save to the next as shown in this gif.

back-vs-isort

I was able to prevent this by configuring isort to use black's formatting by adding

[isort]
multi_line_output = 3
include_trailing_comma = True

to my setup.cfg as indicated in the comments of this black commit.

Now, the file is stable if I repeatedly save it without changes in between. But if I modify the file and then save, the imports briefly flash to isort formatting before reverting to black formatting. So unlike before, the final result is now always black formatting but there's jumping code every time I modify and save.

This seems to suggest that isort is still being run by VS Code with its default settings when the formatOnSave callbacks are triggered. Is there any way to respect the setup.cfg settings in the isort callback?

Additional Info

Related Issue: #2301

@ghost ghost added the triage-needed Needs assignment to the proper sub-team label May 30, 2019
@janosh
Copy link
Author

janosh commented May 30, 2019

After some more research, I was able to fix this issue by adding

"python.sortImports.args": ["--settings-path=${workspaceFolder}/setup.cfg"]

to my user settings as suggested here. This feels a little bit hacky tough, since not all projects use a setup.cfg file, meaning this setting would have to be added on a per-workspace basis.

Is there a better way to handle this with less manual configuration?

@DonJayamanne DonJayamanne added area-formatting good first issue needs PR bug Issue identified by VS Code Team member as probable bug labels May 30, 2019
@ghost ghost removed triage-needed Needs assignment to the proper sub-team labels May 30, 2019
@DonJayamanne
Copy link

DonJayamanne commented May 30, 2019

Prescribed Solution

@janosh janosh changed the title isort not respecting setup.cfg isort not honoring setup.cfg Jun 11, 2019
@mikeroll
Copy link

mikeroll commented Sep 7, 2019

The real cause of this is that the bundled isort (sortImports.py) gets invoked without any directory context. At best, the cwd of isort is /, while it really should be ${workspaceFolder} -- obviously there's no relevant setup.cfg under /. This also explains why "python.sortImports.args": ["--settings-path=...] fixes things.

Formatter invocations do set cwd properly, so I guess the solution would be to copy their cwd behaviour (maybe extract common parts even?). I hope to get around trying that soon.

@DonJayamanne I get the reasoning behind the autopep8/yapf tmpfile hack, however, isort has worked perfectly as < file.py | isort --diff - for quite a long time. Why the tmpfile dance for isort then, is it still relevant?

On a related note - is there a reason you guys bundle your own isort with the extension? Why not go the way of formatters and have it under the user's control?

@iutlu
Copy link

iutlu commented Nov 18, 2019

I'm facing a related issue. I've configured isort's skip_glob and filter_files in my pyproject.toml such that files under my conda environment directory (site-packages etc.) are skipped by isort, and it works OK. However, as @janosh has indicated, if I modify some file from site-packages and then hit save, then isort gets invoked as

...\python.exe ...\.vscode\...\pythonFiles\sortImports.py ~\AppData\Local\Temp\tmp-334840Hb7lTxuklQV.py --diff --settings-path c:\...\workspace_folder --quiet

and since ~\AppData\Local\Temp\tmp-334840Hb7lTxuklQV.py does not match my glob pattern, this does not get skipped, and gets processed by isort, ending up modifying the site-packages file which I do not want. Basically since each and every file passed to isort is invoked from the same temp directory, I cannot apply any rules to skip certain files.

I would be great @DonJayamanne if like you mentioned you could generate the temp file in the directory of the file. Then I wouldn't lose the directory context and apply myskip_glob properly.

@markedwards
Copy link

Any update on a fix for this? Its still happening in 1.44.2 and this issue has been open for nearly a year.

@JacobHenner
Copy link

Also experiencing this with configuration in pyproject.toml.

@karrtikr
Copy link

karrtikr commented Aug 3, 2020

Can anyone confirm if this issue is solved with #9128? We went with using isort --diff - as @mikeroll suggested.

@danrneal
Copy link

danrneal commented Aug 6, 2020

This is NOT still an issue after #9128. I believe this PR was included since 2020.6.88468 and pyproject.toml is working for me.

EDIT: It's working, after I configured it correctly.

@karrtikr karrtikr closed this as completed Aug 6, 2020
@ghost ghost removed the needs PR label Aug 6, 2020
@janosh
Copy link
Author

janosh commented Aug 6, 2020

I'm still curious if there's an answer to @mikeroll's question:

On a related note - is there a reason you guys bundle your own isort with the extension? Why not go the way of formatters and have it under the user's control?

@pe224
Copy link

pe224 commented Aug 31, 2020

For me, this is not yet fixed.
I still need the lines suggested by janosh in my settings.json

{
    "python.sortImports.path": "${workspaceFolder}\\.venv\\Scripts\\isort.exe",
    "python.sortImports.args": [
        "--settings-path=${workspaceFolder}/setup.cfg"
    ],
}

otherwise any isort configs in setup.cfg are ignored (e.g. line_length).
"Ignored" meaning executing isort --check --diff in a shell gives me changes while executing Sort imports... in VSCode claims everything is fine.

VSCode 1.49.0 insider
Extension 2020.8.105369
Win10
Python 3.8.5
isort 5.4.2

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug good first issue
Projects
None yet
Development

No branches or pull requests

9 participants