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

Autoformat on save ignores custom style file #730

Closed
krzysztofwolski opened this issue Feb 8, 2018 · 9 comments · Fixed by #845
Closed

Autoformat on save ignores custom style file #730

krzysztofwolski opened this issue Feb 8, 2018 · 9 comments · Fixed by #845
Assignees
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@krzysztofwolski
Copy link

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
@DonJayamanne
Copy link

Please try using the following format:

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

@patrys
Copy link

patrys commented Feb 8, 2018

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?

@DonJayamanne DonJayamanne added bug Issue identified by VS Code Team member as probable bug area-formatting info-needed Issue requires more information from poster labels Feb 8, 2018
@patrys
Copy link

patrys commented Feb 8, 2018

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

@DonJayamanne
Copy link

Apologies, for the misunderstanding.

@DonJayamanne DonJayamanne added awaiting 1-verification and removed info-needed Issue requires more information from poster labels Feb 8, 2018
@MikhailArkhipov MikhailArkhipov self-assigned this Feb 9, 2018
@patrys
Copy link

patrys commented Feb 13, 2018

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.

@MikhailArkhipov
Copy link

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.

@DonJayamanne
Copy link

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.

@patrys
Copy link

patrys commented Feb 20, 2018

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?

@MikhailArkhipov
Copy link

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.

MikhailArkhipov pushed a commit that referenced this issue Feb 21, 2018
* Fix pylint search

* Handle quote escapes in strings

* Escapes in strings

* CR feedback

* Missing pip

* Test

* Tests

* Tests

* Mac python path

* Tests

* Tests

* Test

* "Go To Python object" doesn't work

* Proper detection and type population in virtual env

* Test fixes

* Simplify venv check

* Remove duplicates

* Test

* Discover pylintrc better + tests

* Undo change

* CR feedback

* Set interprereter before checking install

* Fix typo and path compare on Windows

* Rename method

* #815 - 'F' in flake8 means warning

* 730 - same folder temp

* Properly resolve ~

* Test

* Test
@lock lock bot locked as resolved and limited conversation to collaborators Jul 12, 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

Successfully merging a pull request may close this issue.

5 participants