google / google/yapf

Reading code from stdin causes incorrect formatting on Windows

Open
#790 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

This is a very **minor** bug, as YAPF is mainly used with **a file** or a **directory** passed as a parameter. But in case of using stdin for a code with **\r\n** (Windows EOL) it can break your code.

**How to reproduce**
Use these code lines (with **Windows EOL**):
```
def foo():
return \
{
a:1
}
```

**1st variant:**
1) run `yapf` in `cmd.exe` on Windows with `--style=google --lines=1-1`
2) type the code above
3) press ^Z (ctrl+z)

**2nd variant:**
cat test.py | python -myapf --style=google --lines=1-1

You will see that formatting changed:

```def foo():
return <---- (backslash lost)
{
a: 1 <---- (space added?)
}
````

**With Linux newlines **(\n)** everything works fine**

Not able to get why does this problem happen from the first point of view.
I appreciate if you will help to understand the issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.