emacs-python-black icon indicating copy to clipboard operation
emacs-python-black copied to clipboard

Run black in specific project directory

Open ayys opened this issue 2 years ago • 5 comments

I want to run black in the root dir of projectile project. I am trying to do this because right now, black will not pick up the pyproject.toml configurations.

ayys avatar Nov 28 '22 16:11 ayys

i think you're jumping to conclusions too quickly 🙃

i think your problem is not caused by this emacs package because black already takes care of what your describe:

https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#where-black-looks-for-the-file

wbolster avatar Nov 28 '22 17:11 wbolster

@wbolster thanks for responding. I don't think this is an invalid issue. From reformatter docs,

When called, the reformatter will use PROGRAM and any ARGS to reformat the current buffer. The contents of the buffer will be passed as standard input to the reformatter, which should output them to standard output.

This means black is parsing stdin. So, from black docs,

If you’re formatting standard input, Black will look for configuration starting from the current working directory.

But since the configuration is not in the cwd, black doesn't properly use pyproject.toml.

ayys avatar Nov 29 '22 14:11 ayys

black also has a --stdin-filename flag:

  --stdin-filename TEXT           The name of the file when passing it through
                                  stdin. Useful to make sure Black will
                                  respect --force-exclude option on some
                                  editors that rely on using stdin.

it seems this file is also used to find the project root, as this call to find_project_root() shows: https://github.com/psf/black/blob/d4a85643a465f5fae2113d07d22d021d4af4795a/src/black/init.py#L469

wbolster avatar Nov 29 '22 15:11 wbolster

@ayys can you try out #14 please?

wbolster avatar Nov 29 '22 16:11 wbolster

i'm not actually sure it's needed though. i read this:

If you’re formatting standard input, Black will look for configuration starting from the current working directory.

… as ‘will start looking from the current working directory upwards’, which is the desired behaviour

wbolster avatar Nov 29 '22 16:11 wbolster