Jma353: Moved the check to `EmptyLineTracker` @zsol, this should be good for another review.
diff --git a/black.py b/black.py
index 910a0ed..05edf1a 100644
--- a/black.py
+++ b/black.py
@@ -1480,7 +1480,13 @@ def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
         lines (two on module-level).
         """
         before, after = self._maybe_empty_lines(current_line)
-        before -= self.previous_after
+        before = (
+            # Black should not insert empty lines at the beginning
+            # of the file
+            0
+            if self.previous_line is None
+            else before - self.previous_after
+        )
         self.previous_after = after
         self.previous_line = current_line
         return before, after
@@ -3008,8 +3014,7 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
         # make parentheses invisible
         first.value = ""  # type: ignore
         last.value = ""  # type: ignore
-        if len(node.children) > 1:
-            maybe_make_parens_invisible_in_atom(node.children[1], parent=parent)
+        maybe_make_parens_invisible_in_atom(node.children[1], parent=parent)
         return False
 
     return True

Black produced different code on the second pass of the formatter: Explicit line join in the first line
Operating system: macOS Mojave 10.14.5 (18F132)
Python version: Python 3.7.3
*Black* version: 19.3b0 (f3bb22a828)
Does also happen on master: yes

Minified reproducer:
```python
\


pass
```

Black diff:
```diff
--- source
+++ first pass
@@ -1,5 +1,3 @@
-\
-

 pass

--- first pass
+++ second pass
@@ -1,3 +1,2 @@
-
 pass
```

Looks like some kind of boundary condition in `EmptyLineTracker`?

diff --git a/tests/data/beginning_backslash.py b/tests/data/beginning_backslash.py
new file mode 100644
index 0000000..66c347d
--- /dev/null
+++ b/tests/data/beginning_backslash.py
@@ -0,0 +1,12 @@
+\
+
+
+
+
+
+print("hello, world")
+
+# output
+
+
+print("hello, world")
diff --git a/tests/test_black.py b/tests/test_black.py
index 7b3a8b6..015243f 100644
--- a/tests/test_black.py
+++ b/tests/test_black.py
@@ -639,6 +639,14 @@ def test_tuple_assign(self) -> None:
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, black.FileMode())
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_beginning_backslash(self) -> None:
+        source, expected = read_data("beginning_backslash")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, black.FileMode())
+
     def test_tab_comment_indentation(self) -> None:
         contents_tab = "if 1:\n\tif 2:\n\t\tpass\n\t# comment\n\tpass\n"
         contents_spc = "if 1:\n    if 2:\n        pass\n    # comment\n    pass\n"

pip install -e .[dev]
parse_log_pytest
pytest
pytest pytest-cov
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
appdirs==1.4.4
attrs==25.3.0
-e git+https://github.com/psf/black.git@e66451761fab864cce9077a3ee72999543b8ef65#egg=black
click==8.1.8
coverage==7.8.0
exceptiongroup @ file:///croot/exceptiongroup_1706031385326/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
packaging @ file:///croot/packaging_1734472117206/work
pluggy @ file:///croot/pluggy_1733169602837/work
pytest @ file:///croot/pytest_1738938843180/work
pytest-cov==6.0.0
toml==0.10.2
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typed-ast==1.5.5

name: black
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - exceptiongroup=1.2.0=py39h06a4308_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - packaging=24.2=py39h06a4308_0
  - pip=25.0=py39h06a4308_0
  - pluggy=1.5.0=py39h06a4308_0
  - pytest=8.3.4=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py39h06a4308_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - appdirs==1.4.4
      - attrs==25.3.0
      - black==19.3b0
      - click==8.1.8
      - coverage==7.8.0
      - pytest-cov==6.0.0
      - toml==0.10.2
      - typed-ast==1.5.5
prefix: /opt/conda/envs/black

tests/test_black.py::BlackTestCase::test_beginning_backslash
tests/test_black.py::BlackTestCase::test_expression tests/test_black.py::BlackTestCase::test_expression_diff tests/test_black.py::BlackTestCase::test_expression_ff tests/test_black.py::BlackTestCase::test_shhh_click
tests/test_black.py::BlackTestCase::test_assertFormatEqual tests/test_black.py::BlackTestCase::test_assert_equivalent_different_asts tests/test_black.py::BlackTestCase::test_async_as_identifier tests/test_black.py::BlackTestCase::test_black tests/test_black.py::BlackTestCase::test_bracket_match tests/test_black.py::BlackTestCase::test_broken_symlink tests/test_black.py::BlackTestCase::test_cache_broken_file tests/test_black.py::BlackTestCase::test_cache_multiple_files tests/test_black.py::BlackTestCase::test_cache_single_file_already_cached tests/test_black.py::BlackTestCase::test_cantfit tests/test_black.py::BlackTestCase::test_check_diff_use_together tests/test_black.py::BlackTestCase::test_comment_after_escaped_newline tests/test_black.py::BlackTestCase::test_comments tests/test_black.py::BlackTestCase::test_comments2 tests/test_black.py::BlackTestCase::test_comments3 tests/test_black.py::BlackTestCase::test_comments4 tests/test_black.py::BlackTestCase::test_comments5 tests/test_black.py::BlackTestCase::test_comments6 tests/test_black.py::BlackTestCase::test_comments7 tests/test_black.py::BlackTestCase::test_composition tests/test_black.py::BlackTestCase::test_debug_visitor tests/test_black.py::BlackTestCase::test_detect_pos_only_arguments tests/test_black.py::BlackTestCase::test_empty tests/test_black.py::BlackTestCase::test_empty_exclude tests/test_black.py::BlackTestCase::test_empty_ff tests/test_black.py::BlackTestCase::test_empty_include tests/test_black.py::BlackTestCase::test_empty_lines tests/test_black.py::BlackTestCase::test_endmarker tests/test_black.py::BlackTestCase::test_failed_formatting_does_not_get_cached tests/test_black.py::BlackTestCase::test_filter_cached tests/test_black.py::BlackTestCase::test_fmtonoff tests/test_black.py::BlackTestCase::test_fmtonoff2 tests/test_black.py::BlackTestCase::test_format_file_contents tests/test_black.py::BlackTestCase::test_fstring tests/test_black.py::BlackTestCase::test_function tests/test_black.py::BlackTestCase::test_function2 tests/test_black.py::BlackTestCase::test_function_trailing_comma tests/test_black.py::BlackTestCase::test_get_features_used tests/test_black.py::BlackTestCase::test_get_future_imports tests/test_black.py::BlackTestCase::test_import_spacing tests/test_black.py::BlackTestCase::test_include_exclude tests/test_black.py::BlackTestCase::test_invalid_include_exclude tests/test_black.py::BlackTestCase::test_lib2to3_parse tests/test_black.py::BlackTestCase::test_multi_file_force_py36 tests/test_black.py::BlackTestCase::test_multi_file_force_pyi tests/test_black.py::BlackTestCase::test_new_line_between_class_and_code tests/test_black.py::BlackTestCase::test_no_cache_when_stdin tests/test_black.py::BlackTestCase::test_no_cache_when_writeback_diff tests/test_black.py::BlackTestCase::test_no_files tests/test_black.py::BlackTestCase::test_numeric_literals tests/test_black.py::BlackTestCase::test_numeric_literals_ignoring_underscores tests/test_black.py::BlackTestCase::test_numeric_literals_py2 tests/test_black.py::BlackTestCase::test_pep_570 tests/test_black.py::BlackTestCase::test_pep_572 tests/test_black.py::BlackTestCase::test_pep_572_version_detection tests/test_black.py::BlackTestCase::test_pipe_force_py36 tests/test_black.py::BlackTestCase::test_pipe_force_pyi tests/test_black.py::BlackTestCase::test_piping tests/test_black.py::BlackTestCase::test_piping_diff tests/test_black.py::BlackTestCase::test_preserves_line_endings tests/test_black.py::BlackTestCase::test_preserves_line_endings_via_stdin tests/test_black.py::BlackTestCase::test_python2 tests/test_black.py::BlackTestCase::test_python2_print_function tests/test_black.py::BlackTestCase::test_python2_unicode_literals tests/test_black.py::BlackTestCase::test_python37 tests/test_black.py::BlackTestCase::test_read_cache_line_lengths tests/test_black.py::BlackTestCase::test_read_cache_no_cachefile tests/test_black.py::BlackTestCase::test_remove_empty_parentheses_after_class tests/test_black.py::BlackTestCase::test_remove_parens tests/test_black.py::BlackTestCase::test_report_normal tests/test_black.py::BlackTestCase::test_report_quiet tests/test_black.py::BlackTestCase::test_report_verbose tests/test_black.py::BlackTestCase::test_root_logger_not_used_directly tests/test_black.py::BlackTestCase::test_self tests/test_black.py::BlackTestCase::test_setup tests/test_black.py::BlackTestCase::test_single_file_force_py36 tests/test_black.py::BlackTestCase::test_single_file_force_pyi tests/test_black.py::BlackTestCase::test_slices tests/test_black.py::BlackTestCase::test_string_prefixes tests/test_black.py::BlackTestCase::test_string_quotes tests/test_black.py::BlackTestCase::test_stub tests/test_black.py::BlackTestCase::test_symlink_out_of_root_directory tests/test_black.py::BlackTestCase::test_tab_comment_indentation tests/test_black.py::BlackTestCase::test_tuple_assign tests/test_black.py::BlackTestCase::test_write_cache_creates_directory_if_needed tests/test_black.py::BlackTestCase::test_write_cache_read_cache tests/test_black.py::BlackTestCase::test_write_cache_write_fail
black.py
black.py