Skip to content

Issue with autoFormatOnSave and yapf #124

Closed

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.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

Activity

  1. DonJayamanne commented on Nov 13, 2017

    @DonJayamanne
    Author

    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"],

  2. DonJayamanne commented on Nov 13, 2017

    @DonJayamanne
    Author

    From David Cramer (@dcramer) on August 10, 2017 4:57

    Sean Brant (@seanbrant) great idea! Will give this a try tomorrow and report back.

  3. DonJayamanne commented on Nov 13, 2017

    @DonJayamanne
    Author

    From Yegor Ius (@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.

  4. DonJayamanne commented on Nov 13, 2017

    @DonJayamanne
    Author

    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)

  5. sztomi commented on Dec 27, 2017

    @sztomi

    Is there any workaround for this in the meantime?

  6. 13 remaining items

  7. swarnendubiswas commented on Feb 7, 2018

    @swarnendubiswas

    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.

  8. osya commented on Feb 24, 2018

    @osya

    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

  9. brettcannon commented on Feb 26, 2018

    @brettcannon
    Member

    osya can you open a new issue?

  10. osya commented on Feb 26, 2018

    @osya

    Brett Cannon (@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?
  11. brettcannon commented on Feb 26, 2018

    @brettcannon
    Member

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

  12. patrys commented on Feb 27, 2018

    @patrys

    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.

  13. MikhailArkhipov commented on Feb 27, 2018

    @MikhailArkhipov

    See #730

  14. osya commented on Mar 9, 2018

    @osya

    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.

  15. locked as resolved and limited conversation to collaborators on Jul 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-formattingbugIssue identified by VS Code Team member as probable bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions