Repository navigation
Issue with autoFormatOnSave and yapf #124
Copy link
Copy link
Closed
Labels
area-formattingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bug
Milestone
Description
From David Cramer (@dcramer) on June 1, 2017 6:44
Environment data
VS Code version: 1.12.2 (tried Insiders as well)
Python Extension version: 0.6.4
Python Version: 2.7.13
OS and version: macOS Sierra 10.12.4
Actual behavior
Upon save it reformats the file, and does not appear to behave the same as yapf -i file.
For example, this content:
from __future__ import absolute_import
from django.db import models
from django.utils import timezone
from sentry.db.models import (BoundedBigIntegerField, FlexibleForeignKey,
Model, sane_repr)When I hit save, it generates invalid Python:
from __future__ import absolute_import
from django.db import models
from django.utils import timezone
from sentry.db.models import (
BoundedBigIntegerField, FlexibleForeignKey, Model, sane_repr
)
)Expected behavior
The expected output should be:
from __future__ import absolute_import
from django.db import models
from django.utils import timezone
from sentry.db.models import (
BoundedBigIntegerField, FlexibleForeignKey, Model, sane_repr
)Steps to reproduce:
- add
editor.formatOnSaveto workspace settings - save the file
Even when it doesnt incorrectly format the file into invalid Python, it doesnt seem to respect the configuration within setup.cfg (i still get different output from yapf -i).
Here is the changeset which we identified this behavior in:
Settings
Your launch.json (if dealing with debugger issues):
Your settings.json:
{
"files.exclude": {
"**/*.pyc": true,
"static": true,
"node_modules": true,
"src/sentry/static/dist": true,
"env": true
},
"prettier.bracketSpacing": false,
"prettier.singleQuote": true,
"prettier.printWidth": 90,
"eslint.options": {
"configFile": ".eslintrc"
},
"[javascript]": {
"editor.formatOnSave": true
},
"[json]": {
"editor.formatOnSave": true
},
"editor.formatOnSave": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "yapf",
"python.pythonPath": "${env.WORKON_HOME}/sentry/bin/python"
}Copied from original issue: DonJayamanne/pythonVSCode#992
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-formattingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bug
Activity
DonJayamanne commentedon Nov 13, 2017
From Sean Brant (@seanbrant) on August 10, 2017 2:42
David Cramer (@dcramer) I was running into this as well and might have a temporary solution.
"python.formatting.yapfArgs": ["--style", "${workspaceRoot}/setup.cfg"],DonJayamanne commentedon Nov 13, 2017
From David Cramer (@dcramer) on August 10, 2017 4:57
Sean Brant (@seanbrant) great idea! Will give this a try tomorrow and report back.
DonJayamanne commentedon Nov 13, 2017
From Yegor Ius (@yegorski) on October 16, 2017 15:57
As far as
autopep8config is concerned, I got away with this in VS Code'sUser Settings:"python.formatting.autopep8Args": ["--max-line-length=120"]Now on my Mac's VS Code, pressing Option + Shift + F formats the files I want to format.
DonJayamanne commentedon Nov 13, 2017
Yegor Ius (@yegorski), apologies for not having resolved this issue yet. Will look into this soon. I believe I know the problem (I'm creating a temporary file to perform the formatting, and in doing so, the config file isn't picked up)
sztomi commentedon Dec 27, 2017
Is there any workaround for this in the meantime?
13 remaining items
swarnendubiswas commentedon Feb 7, 2018
I have a similar issue with
yapfas the formatter. VSC withautopep8works fine for me, but not withyapf. Myyapfsettings areNote that
yapfworks for me from the command line. I am on a CentOS 7.4.UPDATE: 030918: My problem is fixed by providing the full path to the yapf style file.
osya commentedon Feb 24, 2018
There is a one file "0001_initial.py" which VS Code doesn't format with
yapfon save (for other files it worked correctly). I tried to switch off all VS Code extensions exceptPythonextension - doesn't help. Manually formattingyapf -i <filename>works. But when I make some changes in the file and save it then formatting worked. When I just save it without any changes formatting doesn't work. May be separate issue should be created? Please advisebrettcannon commentedon Feb 26, 2018
osya can you open a new issue?
osya commentedon Feb 26, 2018
Brett Cannon (@brettcannon) I tried to reproduce this issue, but I can't. This file
0001_initial.pyis a Django migration file which was generated from PyCharm, outside from VS Code. But I find another thing:yapfand open it in VS Codeyapfsettings in thesetup.cfgfile. This behaviour is not expected. Then press Ctrl-S several times again. And now formatting changes according to theyapfsettings from thesetup.cfg.Please advise whether it is issue, or may be it is a correct behavior due to may be
yapfrereading config file after every file changes?brettcannon commentedon Feb 26, 2018
Without knowing what your settings are set to I don't know. Please open a separate issue.
patrys commentedon Feb 27, 2018
I believe it's another manifestation of saving temporary files outside the workspace so the tools can't find their config files. It should be fixed on master.
MikhailArkhipov commentedon Feb 27, 2018
See #730
osya commentedon Mar 9, 2018
It seems that #730 covers my issue. Though for yapf settings I used section
[yapf]in thesetup.cfgfile rather than.style.yapffile in the #730. So this issue seems can be closed. I can't understand in which version of VS Code #730 merged so didn't test it.