Skip to content

Autoformat on save ignores custom style file #730

Closed

Description

Environment data

VS Code version: 1.20
Python Extension version: 2018.1.0
Python Version: 3.6
OS and version: MacOS Sierra 0.12.6

Actual behavior

First file save with python code formats it to default yapf style. Second save format code to intended, custom style specified in .style.yapf.

To fix this issue added custom argument to workspace settings:

    "python.formatting.yapfArgs": [
        "--style=.style.yapf"
    ]

After that yapf on save works as intended

Expected behavior

After changes in code and saving file, yapf should format code to style described in .style.yapf

Steps to reproduce:

  • open project
  • install and enable yapf
  • enable format on save
  • create .style.yapf file with custom settings (SPLIT_BEFORE_FIRST_ARGUMENT = true)
  • add changes to any .py file
  • save
  • now file is formatted with default style
  • add changes to same file
  • save
  • now file is formatted with custom style

Activity

  1. DonJayamanne commented on Feb 8, 2018

    @DonJayamanne

    Please try using the following format:

    "python.formatting.yapfArgs": [
            "--style", ".style.yapf"
        ]
  2. patrys commented on Feb 8, 2018

    @patrys

    I've faced a similar problem today where "Sort dependencies" would sometimes ignore the local isort configuration. Could it be that the working directory is sometimes incorrect when invoking external tools?

  3. patrys commented on Feb 8, 2018

    @patrys

    Don Jayamanne (@DonJayamanne) The problem is not with yapfArgs as it works correctly with the example provided above, the problem is that without overriding yapfArgs the tool does not always find the default config file which is in the root of the repository. The same thing happens in my isort example.

    (Disclaimer: I work with Krzysztof Wolski (@krzysztofwolski).)

  4. DonJayamanne commented on Feb 8, 2018

    @DonJayamanne

    Apologies, for the misunderstanding.

  5. patrys commented on Feb 13, 2018

    @patrys

    I've found a way to reproduce this. It turns out that when you run any of the formatters (yapf, isort etc.) on an unsaved file, they look for their config files outside of the current workspace directory.

  6. MikhailArkhipov commented on Feb 20, 2018

    @MikhailArkhipov

    This is because in unsaved case file is actually saved in temp folder and then formatted so formatter can't access style rules that are next to the actual file.

  7. DonJayamanne commented on Feb 20, 2018

    @DonJayamanne

    I think we should look for the file, and then apply it.
    We can walk up the folder structure and then use that file when formatting a temporary file.

  8. patrys commented on Feb 20, 2018

    @patrys

    Would it be possible to save the temporary file next to the real one so that things just work without VSCode having to reimplement parts of the tool logic?

  9. MikhailArkhipov commented on Feb 20, 2018

    @MikhailArkhipov

    Yes. There a small risk that if something fails temp file will remain and appear in the workspace (and possibly the git changes). We could try and locate the file too.

  10. added a commit that references this issue on Feb 21, 2018
  11. locked as resolved and limited conversation to collaborators on Jul 12, 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