executablebooks / mdformat

CommonMark compliant Markdown formatter
https://mdformat.rtfd.io
MIT License
440 stars 46 forks source link

mdformat._util.atomic_write fails if dir isn't writeable #383

Closed guykisel closed 1 year ago

guykisel commented 1 year ago

Describe the bug

context I'm running mdformat on a directory of markdown docs in a perforce repo. In perforce, everything in a repo defaults to read-only, and only becomes writeable when checked out. Normally this should be fine, because I'm running mdformat on files I've checked out. However, the atomic_write function creates a temp dir in the current path, which in this case is not writeable unless I've explicitly checked it out.

expectation I expect atomic_write to fall back to creating the temp dir in one of the default paths used by tempfile.gettempdir() as described at https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir or perhaps alternatively to give up on atomic writes and just do a best-effort?

bug atomic_write fails with a permission error :(

> mdformat .
Traceback (most recent call last):
  File "D:\prj\python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\docs\Scripts\mdformat.exe\__main__.py", line 7, in <module>
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\docs\lib\site-packages\mdformat\__main__.py", line 8, in run
    exit_code = mdformat._cli.run(sys.argv[1:])
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\docs\lib\site-packages\mdformat\_cli.py", line 114, in run
    atomic_write(path, formatted_str, newline)
  File "C:\Users\user\AppData\Local\pypoetry\Cache\virtualenvs\docs\lib\site-packages\mdformat\_util.py", line 115, in atomic_write
    os.replace(tmp_path, path)
PermissionError: [WinError 5] Access is denied: 'D:\\prj\\docs\\tmpyw2fgxhp' -> 'D:\\prj\\docs\\README.md'
Error: Sequence aborted after failed subtask 'mdformat'

problem This makes using mdformat with perforce slightly more difficult. It's not a blocker, just mildly annoying.

Reproduce the bug

  1. Set up a perforce repo
  2. Create a dir with markdown files in the repo
  3. Check out some of the files in a changelist, but not the parent dir
  4. Run mdformat on the files

List your environment

Python 3.9.1

> python -m pip freeze
attrs==22.2.0
CacheControl==0.12.11
certifi==2022.12.7
charset-normalizer==3.0.1
cleo==2.0.1
crashtest==0.4.1
distlib==0.3.6
dulwich==0.20.50
filelock==3.9.0
html5lib==1.1
idna==3.4
importlib-metadata==4.13.0
jaraco.classes==3.2.3
jsonschema==4.17.3
keyring==23.13.1
lockfile==0.12.2
more-itertools==9.0.0
msgpack==1.0.4
packaging==23.0
pexpect==4.8.0
pkginfo==1.9.6
platformdirs==2.6.2
poetry==1.3.2
poetry-core==1.4.0
poetry-plugin-export==1.2.0
ptyprocess==0.7.0
pyrsistent==0.19.3
pywin32-ctypes==0.2.0
rapidfuzz==2.13.7
requests==2.28.2
requests-toolbelt==0.10.1
shellingham==1.5.0.post1
six==1.16.0
tomli==2.0.1
tomlkit==0.11.6
trove-classifiers==2023.1.12
urllib3==1.26.14
virtualenv==20.16.5
webencodings==0.5.1
zipp==3.11.0
welcome[bot] commented 1 year ago

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

hukkin commented 1 year ago

Thanks, and sorry for taking so long to respond. https://github.com/executablebooks/mdformat/pull/409 fixes this and a couple other issues.

guykisel commented 1 year ago

oh, thank you! <3