google / yapf

A formatter for Python files
Apache License 2.0
13.75k stars 887 forks source link

Executing YAPF in Windows 10 Powershell throws Permission Denied when using .STYLE.YAPF file #919

Open tommai78101 opened 3 years ago

tommai78101 commented 3 years ago

In YAPF, there is a directory that it looks up, to find a .STYLE.YAPF file:

~/.config/yapf/style/

On Windows 10, this directory is located at: C:\Users\tomma\.config\yapf\style\

I have placed my .STYLE.YAPF file in this directory.

When executing YAPF in Powershell, it would throw a "Permissions Denied" error because of an incorrectly parsed forward slash/backwards slash in the PATH it is attempting to look in for the .STYLE.YAPF file. You can easily tell by checking the last line of the stack trace (below) for this path: C:\\Users\\tomma/.config\\yapf\\style

The error should have thrown something like "FileNotFoundError" instead of a "PermissionsError", but I digress, since the .STYLE.YAPF file is placed there. I did double-check to make sure that the file permissions and folder permissions are set to FULL CONTROL under my account, tomma. Thus, I don't believe it's a permissions issue.

Stack trace:

PS D:\Documents\PythonProjects\testPython> yapf .\example.py
Traceback (most recent call last):
  File "d:\python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\Python38\Scripts\yapf.exe\__main__.py", line 9, in <module>
  File "d:\python38\lib\site-packages\yapf\__init__.py", line 344, in run_main
    sys.exit(main(sys.argv))
  File "d:\python38\lib\site-packages\yapf\__init__.py", line 216, in main
    changed = FormatFiles(
  File "d:\python38\lib\site-packages\yapf\__init__.py", line 277, in FormatFiles
    changed |= _FormatFile(filename, lines, style_config, no_local_style,
  File "d:\python38\lib\site-packages\yapf\__init__.py", line 298, in _FormatFile
    reformatted_code, encoding, has_change = yapf_api.FormatFile(
  File "d:\python38\lib\site-packages\yapf\yapflib\yapf_api.py", line 85, in FormatFile
    reformatted_source, changed = FormatCode(
  File "d:\python38\lib\site-packages\yapf\yapflib\yapf_api.py", line 124, in FormatCode
    style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
  File "d:\python38\lib\site-packages\yapf\yapflib\style.py", line 637, in CreateStyleFromConfig
    config = _CreateConfigParserFromConfigFile(style_config)
  File "d:\python38\lib\site-packages\yapf\yapflib\style.py", line 672, in _CreateConfigParserFromConfigFile
    with open(config_filename) as style_file:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\tomma/.config\\yapf\\style'

Maybe it has to do with the YAPF file, given below?

[style]
BASED_ON_STYLE = google
USE_TABS = true
INDENT_DICTIONARY_VALUE = true
DEDENT_CLOSING_BRACKETS = true
COALESCE_BRACKETS = false
EACH_DICT_ENTRY_ON_SEPARATE_LINE = false
ALLOW_SPLIT_BEFORE_DICT_VALUE = true
COLUMN_LIMIT = 999
ARITHMETIC_PRECEDENCE_INDICATION = true
CONTINUATION_ALIGN_STYLE = VALIGN-RIGHT
CONTINUATION_INDENT_WIDTH = 1
INDENT_WIDTH = 1

System information:

OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19042 N/A Build 19042
System Type:               x64-based PC
Python:                    Python 3.9.4
YAPF:                      0.29.0 Stable
tommai78101 commented 3 years ago

I uninstalled my local Python 3.8.8 and used pyenv-win, since I realized I need to be able to switch different Python environments.

After installing pyenv-win, I did all the setup:

# Python is version 3.9.4.
pyenv rehash
pyenv install 3.9.4
pyenv global 3.9.4
# Installed yapf
python -m pip3 install -U yapf
# Got yapf 0.31.0
yapf --version
# Finally, ran the yapf with the .STYLE.YAPF file in C:\Users\tomma\.config\yapf\style\ directory

PS D:\Documents\PythonProjects\testPython> python -m yapf .\example.py
Traceback (most recent call last):
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\__main__.py", line 18, in <module>
    yapf.run_main()
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\__init__.py", line 362, in run_main
    sys.exit(main(sys.argv))
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\__init__.py", line 126, in main
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\__init__.py", line 227, in _FormatFile
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\yapflib\yapf_api.py", line 94, in FormatFile   
    reformatted_source, changed = FormatCode(
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\yapflib\yapf_api.py", line 142, in FormatCode  
    style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\yapflib\style.py", line 708, in CreateStyleFromConfig
    config = _CreateConfigParserFromConfigFile(style_config)
  File "C:\Users\tomma\.pyenv\pyenv-win\versions\3.9.4\lib\site-packages\yapf\yapflib\style.py", line 743, in _CreateConfigParserFromConfigFile
    with open(config_filename) as style_file:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\tomma/.config\\yapf\\style'

By the way, I can confirm the .STYLE.YAPF file works normally, if I put the file in the parent directory of all of my Python projects: D:\Documents\PythonProjects\. Just running yapf .\testPython\example.py works flawlessly after I put the .STYLE.YAPF file in the parent directory there.