Skip to content

Minimal version of #1082 - just add the __init__.py files - #1087

Merged
aiuto merged 6 commits into
bazelbuild:mainfrom
aiuto:init
Nov 11, 2026
Merged

aiuto merged 6 commits into
bazelbuild:mainfrom
aiuto:init

Conversation

@aiuto

@aiuto aiuto commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Add init.py files to modules. Working towards #1081

@onnosteenbergen-samotics

Copy link
Copy Markdown

A small test project still reports warnings.

use_lib.py

from pkg.private import manifest
import pkg, pkg.private
print("ok", manifest.__file__, pkg.__file__, pkg.private.__file__)

BUILD

load("@rules_pkg//pkg:deb.bzl", "pkg_deb")
load("@rules_pkg//pkg:install.bzl", "pkg_install")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_pkg//pkg:verify_archive.bzl", "verify_archive_test")
load("@rules_pkg//pkg:zip.bzl", "pkg_zip")

pkg_files(name = "files", srcs = ["hello.txt"], prefix = "usr/share/hello")

pkg_tar(name = "my_tar", srcs = [":files"])

pkg_zip(name = "my_zip", srcs = [":files"])

pkg_tar(name = "data", srcs = [":files"], extension = "tar.gz")

pkg_deb(
    name = "my_deb",
    data = ":data",
    package = "hello",
    version = "1.0",
    architecture = "all",
    maintainer = "x <[email protected]>",
    description = "test",
)

pkg_install(name = "install", srcs = [":files"])

verify_archive_test(
    name = "verify_tar",
    target = ":my_tar",
    must_contain = ["usr/share/hello/hello.txt"],
)

load("@rules_python//python:py_binary.bzl", "py_binary")

py_binary(
    name = "use_lib",
    srcs = ["use_lib.py"],
    deps = ["@rules_pkg//pkg/private:manifest"],
)

MODULE.bazel

module(name = "consumer")
bazel_dep(name = "rules_pkg")
local_path_override(module_name = "rules_pkg", path = "../pr1087")
bazel_dep(name = "rules_python", version = "2.3.0")

rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
rules_python_config.explicit_init_py(default = True)
$ bazel shutdown
$ bazel build //...
Starting local Bazel server (9.3.0) and connecting to it...
DEBUG: .../rules_python+/python/private/py_executable.bzl:1595:14: 
======================================================================
WARNING: Target @@rules_pkg+//pkg/private/zip:build_zip is using implicit __init__.py creation.
  This diabolic behavior is deprecated and will be disabled by default in a
  future release.
  See https://github.com/bazel-contrib/rules_python/issues/2945

  Ensure all __init__.py files are explicitly created and
  added to the srcs or deps of your targets.

  Disable implicit creation for your module in MODULE.bazel:

    rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
    rules_python_config.explicit_init_py(default = True)

  Or for a specific target by setting:

    legacy_create_init = 0

  Or globally with the following Bazel flag:

    --incompatible_default_to_explicit_init_py
======================================================================
DEBUG: .../rules_python+/python/private/py_executable.bzl:1595:14: 
======================================================================
WARNING: Target @@rules_pkg+//pkg/private/tar:build_tar is using implicit __init__.py creation.
  This diabolic behavior is deprecated and will be disabled by default in a
  future release.
  See https://github.com/bazel-contrib/rules_python/issues/2945

  Ensure all __init__.py files are explicitly created and
  added to the srcs or deps of your targets.

  Disable implicit creation for your module in MODULE.bazel:

    rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
    rules_python_config.explicit_init_py(default = True)

  Or for a specific target by setting:

    legacy_create_init = 0

  Or globally with the following Bazel flag:

    --incompatible_default_to_explicit_init_py
======================================================================
DEBUG: .../rules_python+/python/private/py_executable.bzl:1595:14: 
======================================================================
WARNING: Target @@rules_pkg+//pkg/private/deb:make_deb is using implicit __init__.py creation.
  This diabolic behavior is deprecated and will be disabled by default in a
  future release.
  See https://github.com/bazel-contrib/rules_python/issues/2945

  Ensure all __init__.py files are explicitly created and
  added to the srcs or deps of your targets.

  Disable implicit creation for your module in MODULE.bazel:

    rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
    rules_python_config.explicit_init_py(default = True)

  Or for a specific target by setting:

    legacy_create_init = 0

  Or globally with the following Bazel flag:

    --incompatible_default_to_explicit_init_py
======================================================================
INFO: Analyzed 10 targets (103 packages loaded, 3269 targets configured).
INFO: Found 10 targets...
INFO: Elapsed time: 3.321s, Critical Path: 0.06s
INFO: 1 process: 82 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action

@aiuto

aiuto commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Sure, if you use a newer rules_python, you'll see the warnings,
But that means it's under the control of the users MODULE.bazel file.
They could add the recommend stanza

rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
rules_python_config.explicit_init_py(default = True)

To avoid the warning.

@aiuto
aiuto marked this pull request as ready for review November 10, 2026 04:58
@onnosteenbergen-samotics

Copy link
Copy Markdown

Sure, if you use a newer rules_python, you'll see the warnings, But that means it's under the control of the users MODULE.bazel file. They could add the recommend stanza

rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
rules_python_config.explicit_init_py(default = True)

To avoid the warning.

If you check my MODULE.bazel example it has those two lines. Please try it out. The rules only apply to the users own usage of rules_python, not the internal usage of rules_pkg

@aiuto

aiuto commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author
rules_python_config = use_extension("@rules_python//python/extensions:config.bzl", "config")
rules_python_config.explicit_init_py(default = True)

If you check my MODULE.bazel example it has those two lines. Please try it out. The rules only apply to the users own usage of rules_python, not the internal usage of rules_pkg

I see what you are getting at. That's broken in rules_python, because the outer user should be able to control behaviors all the way down. So this PR is partial, but not sufficient for all users.
What I don't want to do, though, is force an update to the minimal rules_python we are currently using.
I'll talk to @rickeylev about this at some point. I wonder what they are planning.

@aiuto
aiuto merged commit e4c9b50 into bazelbuild:main Nov 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants