question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isort not honoring setup.cfg

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:10

github_iconTop GitHub Comments

12reactions
markedwardscommented, Apr 30, 2020

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

11reactions
mikerollcommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration options for isort
Forces line endings to the specified value. If not set, values will be guessed per-file. Type: String Default: Config default: Python & ...
Read more >
Supported Config Files - isort
Supported Config Files. isort supports various standard config formats to allow customizations to be integrated into any project quickly.
Read more >
Changelog - isort
Config files are no longer composed on-top of each-other. Instead the first config file found is used. Since there is no longer composition...
Read more >
Settings - isort
Defines how the default settings for isort should be loaded """ import configparser ... tuple( target.replace("py", "") for target in dir(stdlibs) if not...
Read more >
isort-plus - PyPI
Meaning you place any standard isort configuration parameters within a .editorconfig file under the *.py section and they will be honored.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found