microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
146 stars 35 forks source link

[BUG] Black formatter breaks python code when it contains Emoji #411

Closed pktiuk closed 6 months ago

pktiuk commented 6 months ago

Problem description

I have a Python code:

def fun():
    print("🎥 Input Stream will be streamed. Address: rtsp://IP_ADDRESS:8557/input_stream")

When I format it using black 23.12.1 I get:

def fun():
    print(
        "🎥 Input Stream will be streamed. Address: rtsp://IP_ADDRESS:8557/input_stream"
    )

But When I format it with this addon I get:

def fun():
    print(
        "🎥 Input Stream will be streamed. Address: rtsp://IP_ADDRESS:8557/input_stream
    ")

Which is invalid code.

Configuration

2024-01-15 10:45:13.705 [info] Settings used to run Server:
[
    {
        "cwd": "/home/pkotiuk/Projects/eyeq",
        "workspace": "file:///home/pawel/proj1",
        "args": [],
        "path": [],
        "interpreter": [
            "/usr/bin/python3"
        ],
        "importStrategy": "useBundled",
        "showNotifications": "off"
    }
]

2024-01-15 10:45:13.970 [info] Version info for formatter running for /home/pawel/proj1:
black, 23.10.1 (compiled: no)
Python (CPython) 3.10.12
pktiuk commented 6 months ago

I guess you should update your black version

karthiknadig commented 6 months ago

Duplicate of https://github.com/microsoft/vscode-black-formatter/issues/395

This is fixed in pre-release. Nothing to do with Black formatter itself. More to do with VS code uses UTF-16 when calculating string lengths in the editor no matter which encoding is used for the file itself. You can read the details in #395