Skip to content

Bazel can overwrite __init__.py with empty file in source tree #2947

Open

Description

@tholenst

Description of the bug:

If I mess up my BUILD structure, and forget to include srcs = ["__init__.py"], it seems it can happen that Bazel overwrites the existing __init__.py with an empty file.

Reproduce:

Setup the directory:

cd $(mktemp -d)
cat > MODULE.bazel <<-EOF
module(name = "test_module")

bazel_dep(name = "rules_python", version = "1.4.1")
EOF
mkdir some_package
cat > some_package/BUILD.bazel <<-EOF
load("@rules_python//python:defs.bzl", "py_library", "py_test")
py_library(
  name = "_some_library",
  srcs = ["_some_library.py"],
)
py_library(
  name = "some_package",
  srcs = ["__init__.py"],
  deps = [":_some_library"],
)
py_test(
  name = "correct_test",
  srcs = ["test.py"],
  main = "test.py",
  deps = [":some_package"],
)
py_test(
  name = "bad_dep_test",
  srcs = ["test.py"],
  main = "test.py",
  deps = [":_some_library"],
)
EOF
cat > some_package/__init__.py <<-EOF
import some_package._some_library
method=_some_library.method
EOF
cat > some_package/_some_library.py <<-EOF
def method():
  pass
EOF
cat > some_package/test.py <<-EOF
import some_package
some_package.method()
EOF

Double check the with tree. This gives:

.
├── MODULE.bazel
└── some_package
    ├── BUILD.bazel
    ├── __init__.py
    ├── _some_library.py
    └── test.py

Run the good test -- it will pass.

bazel test //some_package:correct_test

Then run the bad test. It fails and overwrites __init__.py.

bazel test //some_package:bad_dep_test

The run the good test. It now fails.

bazel test //some_package:correct_test

Note: Setting common --incompatible_default_to_explicit_init_py in .bazelrc fixes this.

See also #2945.

Activity

  1. aignas commented on May 31, 2025

    @aignas
    Collaborator

    Right now this looks like a duplicate of #2945, could you please explain how this is different, I don't fully understand the word "overwrite" here. Does it replace data inside the source tree?

  2. tholenst commented on May 31, 2025

    @tholenst
    Author

    Yes, it deletes data inside the source tree.

  3. tholenst commented on May 31, 2025

    @tholenst
    Author

    This bug is about bazel deleting my code. #2945 is about a cleanup.

  4. added theissue type on May 31, 2025
  5. SandyChapman commented on Nov 28, 2025

    @SandyChapman

    If you add common --incompatible_default_to_explicit_init_py to your .bazelrc, it disables this behaviour. You'll now be responsible for creating your own init files though!

    https://bazel.build/reference/command-line-reference#flag--incompatible_default_to_explicit_init_py

  6. Vinh-CHUC commented on Jan 26, 2026

    @Vinh-CHUC

    I'm witnessing this even when my __init__.py is declared in my srcs= super odd. Using bazel 8.4.2

    Anyone else also witnessed that? About to try that flat in .bazelrc to see if the problem goes away. In my case it seems to happen only on bazel test failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    core-rulesIssues concerning core bin/test/lib rulestype: bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions