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

Trying to read from stdin dumps no content when stdin-filename is excluded #4609

Open
sacereda opened this issue Mar 11, 2025 · 4 comments · May be fixed by #4610
Open

Trying to read from stdin dumps no content when stdin-filename is excluded #4609

sacereda opened this issue Mar 11, 2025 · 4 comments · May be fixed by #4610
Labels
T: bug Something isn't working

Comments

@sacereda
Copy link

When working in a project where some paths are not blacked yet, you may configure file discovery and run something like:

black .

Once you have this, you may need to integrate black in your editor, and some plugins end up piping from stdin:

stdin | black - --stdin-filename "$FILEPATH"

The problem is the integration will fail when trying to format a file being excluded by force-exclude directive.

You can check other tools like ruff output unformatted stdin in this case:

astral-sh/ruff#8596

So they can have editor integrations such as:

https://github.com/JoshHayes/emacs-ruff-format/blob/main/ruff-format.el#L43

Where they just pipe the content of the buffer to their cli

@sacereda sacereda added the T: bug Something isn't working label Mar 11, 2025
@MeGaGiGaGon
Copy link
Collaborator

MeGaGiGaGon commented Mar 11, 2025

Yay, conflicting requirements. I previously made this exact behavior not happen in #4539 , fixing #4232 , because they wanted the opposite thing for their tooling, where reading from stdin with a force excluded path wouldn't give an output. Feel free to read the issue and PR, maybe I misinterpreted what they actually wanted. Past that I don't really have an opinion/preference on which of these behaviors would be better, and I also don't know what to do in these conflict of needs situations.
Edit: On a bit further thought, this isn't exactly the same, because under the old behavior stdin would be formatted, while under your proposed changes it wouldn't be formatted, just returned. I guess that would be fine? Still not sure, since I've never used integrations for this sort of thing.

@tusharsadhwani
Copy link
Collaborator

I don't think there's a conflict, i think they just didn't want formatting to happen. Outputting the original source back to stdout should be okay if I'm reading this correctly.

@tusharsadhwani
Copy link
Collaborator

FWIW i do think that if we're exiting with zero we should be outputting something, rather than nothing.

@sacereda
Copy link
Author

Yay, conflicting requirements. I previously made this exact behavior not happen in #4539 , fixing #4232 , because they wanted the opposite thing for their tooling, where reading from stdin with a force excluded path wouldn't give an output. Feel free to read the issue and PR, maybe I misinterpreted what they actually wanted. Past that I don't really have an opinion/preference on which of these behaviors would be better, and I also don't know what to do in these conflict of needs situations. Edit: On a bit further thought, this isn't exactly the same, because under the old behavior stdin would be formatted, while under your proposed changes it wouldn't be formatted, just returned. I guess that would be fine? Still not sure, since I've never used integrations for this sort of thing.

Your original change it's ok, stdin should be excluded if stdin_filename matches any exclusion.

I think when you run the cli with a path it's ok to not modify the file if it's excluded, but when you run it through a pipe, say:

cat no_format.py | black - --stdin-filename no_format.py

you would expect to output the original content. At least that's what's intended in #4610.

That's how other formatters such as ruff or clang-format work:

>cat noformat.c

int main(       ) {
        return 0;
}

>cat .clang-format | grep DisableFormat
DisableFormat:   true

>cat noformat.c | clang-format --assume-filename=noformat.c

int main(       ) {
        return 0;
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants