Skip to content

Unexpected error with Black 25.11.0 when fmt: off and fmt: on are used within block of commentsย #4841

@alexhernandezgarcia

Description

@alexhernandezgarcia

Describe the bug

To Reproduce

For example, take this code:

[
    (1, 2),
    # # fmt: off
    # (3,
    #    4),
    # # fmt: on
    (5, 6),
]

And run it with these arguments:

$ black file.py

The resulting error is:

error: cannot format file.py: INTERNAL ERROR: Black 25.11.0 on Python (CPython) 3.11.13 produced different code on the second pass of the formatter. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_wtxhgk34.log

Oh no! ๐Ÿ’ฅ ๐Ÿ’” ๐Ÿ’ฅ
1 file failed to reformat.

The error is most likely caused by the inclusion of a set of commented lines that include # # fmt: off and # # fmt: on. Removing one or both of these lines does not cause the error.

Expected behavior

I would expect no errors and in fact, this does not occur in previous versions. Is this new behaviour expected?

Environment

  • Black's version: 25.11.0
  • OS and Python version: Linux/Ubuntu 24.04.2 LTS/Python 3.11.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: crashBlack is crashingF: fmtofffmt: off implementationT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions