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

Write unchanged, excluded files to stdout when read via stdin #8596

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Nov 10, 2023

Summary

When you run Ruff via stdin, and pass format or check --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 run ruff format /path/to/excluded/file.py, we don't write anything to stdout. 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.

@charliermarsh charliermarsh added bug Something isn't working cli Related to the command-line interface labels Nov 10, 2023
@charliermarsh charliermarsh force-pushed the charlie/parrot branch 2 times, most recently from 3296387 to 5b1aaa1 Compare November 10, 2023 03:54
@charliermarsh
Copy link
Member Author

Once we make this change, we can in turn change the LSP to always write empty fixes on supported Ruff versions.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.


/// 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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice name!

Copy link
Member Author

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 :)

@charliermarsh charliermarsh merged commit 7968e19 into main Nov 10, 2023
17 checks passed
@charliermarsh charliermarsh deleted the charlie/parrot branch November 10, 2023 04:15
charliermarsh added a commit to astral-sh/ruff-lsp that referenced this pull request Nov 10, 2023
## 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.
azurelotus0926 added a commit to azurelotus0926/ruff-lsp that referenced this pull request Jun 27, 2024
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cli Related to the command-line interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants