microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
149 stars 35 forks source link

Black doesn't work when folder is opened as a symlink #446

Closed ischurov closed 8 months ago

ischurov commented 8 months ago

Type: Bug

I am using Remote Tunnels. On a remote system, I have a folder /vol/tcm10/ischurov/graphene-magnetic and a symlink to that folder in /home/ischurov/tcm10/graphene-magnetic. When I open the folder using a symlink, and try to format a file with black (e.g. on saving), I have the following:

2024-02-02 15:36:18.689 [info] [Trace - 3:36:18 PM] Sending request 'textDocument/formatting - (3)'.
2024-02-02 15:36:18.694 [info] [Trace - 3:36:18 PM] Received notification 'window/logMessage'.
2024-02-02 15:36:18.694 [info] /nix/store/dl6jppcsbgrx3pn05zwzh4fk1gg8bcpd-python3-3.11.7-env/bin/python3 -m black --stdin-filename /home/ischurov/tcm10/graphene-magnetic/test.py -
2024-02-02 15:36:18.695 [info] [Trace - 3:36:18 PM] Received notification 'window/logMessage'.
2024-02-02 15:36:18.695 [info] CWD formatter: /home/ischurov/tcm10/graphene-magnetic
2024-02-02 15:36:18.702 [info] [Trace - 3:36:18 PM] Received notification 'window/logMessage'.
2024-02-02 15:36:18.702 [info] No Python files are present to be formatted. Nothing to do 😴

2024-02-02 15:36:18.702 [info] [Trace - 3:36:18 PM] Received response 'textDocument/formatting - (3)' in 13ms.

When I invoke the command from the terminal, I get the following:

ischurov@cn05:~/tcm10/graphene-magnetic$ /nix/store/dl6jppcsbgrx3pn05zwzh4fk1gg8bcpd-python3-3.11.7-env/bin/python3 -m black --stdin-filename /home/ischurov/tcm10/graphene-magnetic/test.py -
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__main__.py", line 3, in <module>
    patched_main()
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 1504, in patched_main
    main()
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 600, in main
    sources = get_sources(
              ^^^^^^^^^^^^
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 687, in get_sources
    root_relative_path = path.absolute().relative_to(root).as_posix()
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/w4fvvhkzb0ssv0fw5j34pw09f0qw84w8-python3-3.11.7/lib/python3.11/pathlib.py", line 730, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/home/ischurov/tcm10/graphene-magnetic/test.py' is not in the subpath of '/vol/tcm10/ischurov/graphene-magnetic' OR one path is relative and the other is absolute.

If I open the same folder by its real path, not a symlink, Black works without any problems.

The issue can possible be specific to either Remote Tunnels or Linux because I cannot reproduce it on my local Mac machine.

I can reproduce it with the pre-release (v2024.1.10331006) version of Black extension.

Extension version: 2024.1.10331006 VS Code version: Code 1.86.0 (05047486b6df5eb8d44b2ecd70ea3bdf775fd937, 2024-01-31T10:29:15.765Z) OS version: Darwin x64 22.1.0 Modes: Remote OS version: Linux x64 5.15.0-79-generic Remote OS version: Linux x64 5.15.0-79-generic

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz (8 x 1400)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|5, 15, 23| |Memory (System)|16.00GB (2.32GB free)| |Process Argv|--crash-reporter-id 6268c79f-ba03-42d2-bf4d-cd2dfec6306c| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|cn24| |OS|Linux x64 5.15.0-79-generic| |CPUs|Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz (16 x 1200)| |Memory (System)|125.83GB (121.81GB free)| |VM|0%| |Item|Value| |---|---| |Remote|cn24| |OS|Linux x64 5.15.0-79-generic| |CPUs|Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz (16 x 1200)| |Memory (System)|125.83GB (121.81GB free)| |VM|0%|
A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805:30301674 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 2i9eh265:30646982 0bi6i642:30951795 pythongtdpath:30769146 welcomedialog:30910333 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 pythontbext0:30879054 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 dsvsc019b:30953937 3ef8e399:30949928 ```
karthiknadig commented 8 months ago

@ischurov you have to invoke it like this, and see if it works.

cat /home/ischurov/tcm10/graphene-magnetic/test.py | /nix/store/dl6jppcsbgrx3pn05zwzh4fk1gg8bcpd-python3-3.11.7-env/bin/python3 -m black --stdin-filename /home/ischurov/tcm10/graphene-magnetic/test.py -
ischurov commented 8 months ago

@karthiknadig no, it doesn't work, with the same error message:

ischurov@cn05:/vol/tcm10/ischurov/graphene-magnetic$ cat /home/ischurov/tcm10/graphene-magnetic/test.py | /nix/store/dl6jppcsbgrx3pn05zwzh4fk1gg8bcpd-python3-3.11.7-env/bin/python3 -m black --stdin-filename /home/ischurov/tcm10/graphene-magnetic/test.py -
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__main__.py", line 3, in <module>
    patched_main()
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 1504, in patched_main
    main()
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cnxd9sz13rs17ahadm39iam4cl32wriq-python3.11-click-8.1.7/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 600, in main
    sources = get_sources(
              ^^^^^^^^^^^^
  File "/nix/store/nax4lri0ymvmj18jpyvzl0cgg965ws0a-python3.11-black-23.11.0/lib/python3.11/site-packages/black/__init__.py", line 687, in get_sources
    root_relative_path = path.absolute().relative_to(root).as_posix()
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/w4fvvhkzb0ssv0fw5j34pw09f0qw84w8-python3-3.11.7/lib/python3.11/pathlib.py", line 730, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/home/ischurov/tcm10/graphene-magnetic/test.py' is not in the subpath of '/vol/tcm10/ischurov/graphene-magnetic' OR one path is relative and the other is absolute.
karthiknadig commented 8 months ago

This is a bug in black, see here:https://github.com/psf/black/issues/4077. This repo is for the VS Code extension which is a wrapper around black. Please update your black to latest to get the fix (the fix seems to have been addressed in 24.1.0: https://github.com/psf/black/blob/main/CHANGES.md#2410)