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

Issue with autoFormatOnSave and yapf #124

Closed
DonJayamanne opened this issue Nov 13, 2017 · 19 comments
Closed

Issue with autoFormatOnSave and yapf #124

DonJayamanne opened this issue Nov 13, 2017 · 19 comments
Assignees
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@DonJayamanne
Copy link

From @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.formatOnSave to 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:

getsentry/sentry@a2587aa

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

@DonJayamanne
Copy link
Author

From @seanbrant on August 10, 2017 2:42

@dcramer I was running into this as well and might have a temporary solution.

"python.formatting.yapfArgs": ["--style", "${workspaceRoot}/setup.cfg"],

@DonJayamanne
Copy link
Author

From @dcramer on August 10, 2017 4:57

@seanbrant great idea! Will give this a try tomorrow and report back.

@DonJayamanne
Copy link
Author

From @yegorski on October 16, 2017 15:57

As far as autopep8 config is concerned, I got away with this in VS Code's User 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
Copy link
Author

@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)

@brettcannon brettcannon added awaiting 2-PR area-formatting bug Issue identified by VS Code Team member as probable bug labels Nov 14, 2017
@MikhailArkhipov MikhailArkhipov self-assigned this Dec 14, 2017
@sztomi
Copy link

sztomi commented Dec 27, 2017

Is there any workaround for this in the meantime?

@DonJayamanne
Copy link
Author

DonJayamanne commented Dec 27, 2017

Yes, the work around is as follows:

As pointed out by @seanbrant (#124 (comment))
"python.formatting.yapfArgs": ["--style", "${workspaceRoot}/setup.cfg"],

@sztomi
Copy link

sztomi commented Dec 27, 2017

Unfortunately that doesn't seem to work (I did use .style.yapf instead of setup.cfg, but I don't think that should make a difference).

@brettcannon
Copy link
Member

Does it make a difference if you use workspaceFolder instead of workspaceRoot?

@DonJayamanne
Copy link
Author

@sztomi Please can you confirm it works when you run yapf from the command line

@brettcannon brettcannon added info-needed Issue requires more information from poster and removed awaiting 2-PR labels Jan 3, 2018
@MikhailArkhipov
Copy link

Same as #590

@MikhailArkhipov MikhailArkhipov added awaiting 2-PR and removed info-needed Issue requires more information from poster labels Jan 24, 2018
@MikhailArkhipov MikhailArkhipov added this to the January 2018 milestone Jan 29, 2018
@MikhailArkhipov
Copy link

94cd778

@swarnendubiswas
Copy link

swarnendubiswas commented Feb 7, 2018

I have a similar issue with yapf as the formatter. VSC with autopep8 works fine for me, but not with yapf. My yapf settings are

"python.formatting.provider": "yapf",
"python.formatting.yapfArgs": [
        "-p",
        "--style",
        "~/.config/yapf/style"
  ]

Note that yapf works 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
Copy link

osya commented Feb 24, 2018

There is a one file "0001_initial.py" which VS Code doesn't format with yapf on save (for other files it worked correctly). I tried to switch off all VS Code extensions except Python extension - doesn't help. Manually formatting yapf -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 advise

@brettcannon
Copy link
Member

@osya can you open a new issue?

@osya
Copy link

osya commented Feb 26, 2018

@brettcannon I tried to reproduce this issue, but I can't. This file 0001_initial.py is a Django migration file which was generated from PyCharm, outside from VS Code. But I find another thing:

  1. Take some file which already formatted with yapf and open it in VS Code
  2. Press Ctrl-S several times. Nothing changes in formatting. It is as expected
  3. Then make some changes in the file. And save it. Formatting changes - not according to the yapf settings in the setup.cfg file. This behaviour is not expected. Then press Ctrl-S several times again. And now formatting changes according to the yapf settings from the setup.cfg.
    Please advise whether it is issue, or may be it is a correct behavior due to may be yapf rereading config file after every file changes?

@brettcannon
Copy link
Member

Without knowing what your settings are set to I don't know. Please open a separate issue.

@patrys
Copy link

patrys commented 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
Copy link

See #730

@osya
Copy link

osya commented Mar 9, 2018

It seems that #730 covers my issue. Though for yapf settings I used section [yapf] in the setup.cfg file rather than .style.yapf file 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.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 11, 2018
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
Projects
None yet
Development

No branches or pull requests

7 participants