astral-sh / astral-sh/ruff

Running as file watcher in container with PyCharm truncates file

Open
#8,552 9 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
458

Description

I've setup a file watcher that run `ruff --fix` and `ruff format` in a docker image. I've run just one of those commands to isolate the issue and have seen it happen when just `ruff --fix` or `ruff format` is running so it's happening with either command run separately

Here's an example of the error output and when I look at the file, several characters from the last few lines have been removed (i.e. the file was truncated):
```
/Users/dlj/projects/myproject/local/format_file.sh myfile.py
error: Failed to parse myfile.py:117:5: Unexpected token 'exc'
Found 1 error (1 fixed, 0 remaining).
error: Failed to format myfile.py: source contains syntax errors: ParseError { error: UnrecognizedToken(Name { name: "exc" }, None), offset: 3254, source_path: "" }

Process finished with exit code 2
```

Here's the scripts that I use as the watcher (which I have setup in the same manner that [`black` recommends](https://black.readthedocs.io/en/stable/integrations/editors.html#as-file-watcher)):
```
#!/usr/bin/env sh

cd "$(dirname "$0")/.." || exit

if ! local/_run_ruff.sh --fix "$@"; then
exit 1
fi
local/_run_ruff.sh format "$@"
exit $?
```

```
#!/usr/bin/env sh

cd "$(dirname "$0")/.." || exit

docker run --rm \
-v "${PWD}":/usr/src/app:z,cached \
-w /usr/src/app \
ruff:0.1.4 \
/usr/local/bin/ruff "$@"
exit $?
```

On a related note, I know there's a [PyCharm plugin](https://github.com/koxudaxi/ruff-pycharm-plugin) but I like running it in a container, because then [CI and every developer machine is automagically kept in sync](https://github.com/astral-sh/ruff/issues/5072#issuecomment-1779227790)

Here's my `ruff.toml`
```
line-length = 120

select = ["C", "I", "U", "W"]
ignore = ["E", "F", "C901", "W191"]

target-version = "py39"

[isort]
combine-as-imports = true

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"
```

Contributor guide

Open the contributing guide

Research direction

Start with local/format_file.sh, local/_run_ruff.sh, and the supplied ruff.toml, then reproduce the PyCharm watcher through the ruff:0.1.4 Docker image using both --fix and format separately. Compare the file contents before and after each run; done means the watcher no longer truncates the final lines while preserving the reported linting and formatting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python, rust, shell
Domain
developer-experience, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.