-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Write unchanged, excluded files to stdout when read via stdin #8596
Conversation
3296387
to
5b1aaa1
Compare
Once we make this change, we can in turn change the LSP to always write empty fixes on supported Ruff versions. |
|
|
||
/// Read a string from `stdin`. | ||
pub(crate) fn read_from_stdin() -> Result<String, io::Error> { | ||
let mut buffer = String::new(); | ||
io::stdin().lock().read_to_string(&mut buffer)?; | ||
Ok(buffer) | ||
} | ||
|
||
/// Read bytes from `stdin` and write them to `stdout`. | ||
pub(crate) fn parrot_stdin() -> Result<(), io::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice name!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha I wasn't sure if it would be clear but I'm glad :)
## Summary We can avoid the safeguard we introduced in #317 for newer Ruff versions, since Ruff will now avoid writing empty output for excluded files (see: astral-sh/ruff#8596). Closes #267. ## Test Plan I tested this with a local debug build. Now, when I "Fix all" on a non-excluded file with `import os`, it _does_ properly get removed, while running "Format" or "Fix all" on an excluded file leaves the contents untouched.
## Summary We can avoid the safeguard we introduced in astral-sh/ruff-lsp#317 for newer Ruff versions, since Ruff will now avoid writing empty output for excluded files (see: astral-sh/ruff#8596). Closes astral-sh/ruff-lsp#267. ## Test Plan I tested this with a local debug build. Now, when I "Fix all" on a non-excluded file with `import os`, it _does_ properly get removed, while running "Format" or "Fix all" on an excluded file leaves the contents untouched.
Summary
When you run Ruff via stdin, and pass
format
orcheck --fix
, we typically write the changed or unchanged contents to stdout. It turns out we forgot to do this when the file is excluded, so if you runruff format /path/to/excluded/file.py
, we don't write anything tostdout
. This led to a bug in the LSP whereby we deleted file contents for third-party files.The right thing to do here is write back the unchanged contents, as it should always be safe to write the output of stdout back to a file.