bazel-contrib / bazel-contrib/rules_python

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

Đang mở
#2,947 5 bình luận 0 reaction 0 người được giao Xem trên GitHub
core-rules type: bug
Ngôn ngữ chính
Starlark
Star
688
Fork
721
Merge trung bình
15 giờ 7 phút
Pull request đã merge (30 ngày)
76

Mô tả

### 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 https://github.com/bazel-contrib/rules_python/issues/2945.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Tái hiện hành vi từ issue bằng MODULE.bazel và some_package/BUILD.bazel, sau đó kiểm tra cách hai target py_test xử lý some_package/__init__.py. Chạy correct_test, bad_dep_test và lại correct_test để xác nhận việc ghi đè; hoàn tất khi test sai không còn làm rỗng tệp nguồn và test đúng tiếp tục vượt qua.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
build-system
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.