Skip to content

Formatting does not work if first line is blank #11416

@karthiknadig

Description

@karthiknadig

Environment data

  • VS Code version: 1.44.2
  • Extension version (available under the Extensions sidebar): master
  • OS and version: windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: black
  • Relevant/affected Python-related VS Code extensions and their versions: N/A
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version #3977): false
  • Value of the python.languageServer setting: Microsoft

Expected behaviour

Should format document.

Actual behaviour

Formatting fails to apply.

Steps to reproduce:

  1. Create a python file with blank first line:
import sys
print(sys.executable)

Reason

From the logs:

Formatting with black failed.
TypeError: Cannot read property 'length' of undefined

This issue is caused in getTextEditsInternal. When the first line is blank, start.line is 0. beforeLines[start.line - 1].length === 0 check fails with TypeError: Cannot read property 'length' of undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-formattingbugIssue identified by VS Code Team member as probable buggood first issueregressionBug didn't exist in a previous release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions