Skip to content

PyExecutableInfo.runfiles_without_exe includes full interpreter exe with standard library #3181

Closed

Description

@malt3

🐞 bug report

Affected Rule

The issue is caused by the rule: py_binary

Is this a regression?

This has probably been broken since it was introduced.

Description

I'd like to build container images where the interpreter + standard library is part of a base image and the "app" layer contains just my python code (and explicit data dependencies).
I tried to write a small rule that extracts the necessary info from the provider to avoid pulling in the Python interpreter itself.
Looking at the output, the runfiles include the full interpreter, which is unexpected.

🔬 Minimal Reproduction

I have a working branch on tweag/rules_img: https://github.com/tweag/rules_img/tree/python_example/e2e/python

In the subdirectory e2e/python, I have a clean example of what I mean with a separate MODULE.bazel and a clean reproducer.
e2e/python/BUILD.bazel contains a py_binary and my custom rule for creating a "light" binary (using PyExecutableInfo).
e2e/python/extract_python_files/defs.bzl contains the rule I wrote to extract only the data I want.

Here are the repro steps:

  • bazel build :app
  • find bazel-bin/app.runfiles/

🔥 Exception or Error

You can see may files that shouldn't be there, including bazel-bin/app.runfiles/rules_python++python+python_3_13_x86_64-unknown-linux-gnu/bin/python3.13

🌍 Your Environment

Operating System:

  
NixOS 24.11 on amd64
  

Output of bazel version:

  
Bazelisk version: development
Build label: 8.3.1
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Mon Jun 30 16:23:40 2025 (1751300620)
Build timestamp: 1751300620
Build timestamp as int: 1751300620
  

Rules_python version:

  
bazel_dep(name = "rules_python", version = "1.5.3")
  

Anything else relevant?

Activity

  1. malt3 commented on Aug 16, 2025

    @malt3
    ContributorAuthor

    I tried to understand why this happens.

    Runfiles containing the toolchain are collected in _get_runtime_details as runtime_files:

    direct = [] # List of files
    transitive = [] # List of depsets
    if effective_runtime.interpreter:
    direct.append(effective_runtime.interpreter)
    transitive.append(effective_runtime.files)
    if ctx.configuration.coverage_enabled:
    if effective_runtime.coverage_tool:
    direct.append(effective_runtime.coverage_tool)
    if effective_runtime.coverage_files:
    transitive.append(effective_runtime.coverage_files)
    runtime_files = depset(direct = direct, transitive = transitive)

    The toolchain (including bin/python and the standard library) are passed into _get_base_runfiles_for_binary as extra_common_runfiles.

    extra_common_runfiles = [
    runtime_details.runfiles,
    cc_details.extra_runfiles,
    native_deps_details.runfiles,
    semantics.get_extra_common_runfiles_for_binary(ctx),
    ],

    Those get added to common_runfiles:

    common_runfiles.add(extra_common_runfiles)

    .. which are finally used as runfiles_without_exe:

    runfiles_without_exe = common_runfiles,

    All of this makes me think that my assumptions about the intended use of PyExecutableInfo.runfiles_without_exe might be incorrect, and this is actually expected behavior. If this is the case, I'd like to turn this into a feature request instead:

    For the purpose of creating container image layers (and other types of bundling), could we provide a field in some provider that contains the files of the application without the toolchain and standard library? (Bonus points if we can separate between third-party deps and normal application code). This would make it much easier for the larger ecosystem to bundle Python apps efficiently.

  2. added theissue type on Aug 17, 2025
  3. added
    core-rulesIssues concerning core bin/test/lib rules
    on Aug 17, 2025
  4. aignas commented on Aug 17, 2025

    @aignas
    Collaborator

    In general, I think it is a valid request, if users are using non-hermetic toolchains but want to build docker images, there still should be a way. Which toolchain are you using?

  5. malt3 commented on Aug 17, 2025

    @malt3
    ContributorAuthor

    This is the Python toolchain setup in MODULE.bazel:

    python = use_extension("@rules_python//python/extensions:python.bzl", "python")
    python.toolchain(
        python_version = "3.13",
    )
    use_repo(python, "python_3_13")
    
  6. aignas commented on Aug 17, 2025

    @aignas
    Collaborator

    Ah, so you are using the hermetic toolchain, but you want to replace it during packaging? Are you concerned that the thing you are packaging is not strictly the same thing as the one you are testing?

    How do you imagine the Python bootstrap to work? Do you use bootstrap_impl=script?

    In theory, you could still use tar.bzl even today, where you have the mtree manifest that you can modify and just drop the runtime files and replace python with a symlink to the filesystem Python.

  7. malt3 commented on Aug 17, 2025

    @malt3
    ContributorAuthor

    Ah, so you are using the hermetic toolchain, but you want to replace it during packaging?

    That's correct. I want to use the same Python version in Bazel and in the container, but for efficiency, I want to put the toolchain in a separate container image layer or have it preinstalled in the base image (it doesn't change often and can be shared by multiple Python apps). I didn't think about the setting for bootstrap_impl yet.
    The container image does have a "system Python" in the sense that the base image I'm layering upon contains a python interpreter.

    Custom filtering with mtree manifests and tar.bzl does sound like an option. I'm currently building a more optimized way to build layers in rules_img that doesn't have any method for filtering runfiles so far, so that wouldn't work (yet).

  8. malt3 commented on Aug 17, 2025

    @malt3
    ContributorAuthor

    I'm also considering just writing my own Aspect that collects srcs and data from py_library and friends, but I'm sure it's not easy to do this correctly.

  9. aignas commented on Aug 17, 2025

    @aignas
    Collaborator

    FYI, The tar.bzl and rules_oci example shows how to put the interpreter in a separate layer.

  10. rickeylev commented on Aug 17, 2025

    @rickeylev
    Collaborator

    The origin of runfiles_without_exe is from Google, where the entry point executable and the runtime are one in the same. The goal being, the original executable won't work as-is in the intended deployment environment, so a new one (with a different bootstrap, but same runtime) must be derived.

    It's a similar intent as what you have, though.

    It sounds like what you want is something like runfiles_without_runtime. You don't want the runtime, but probably want the same bootstrap.

    That sounds mostly reasonable. Hm. However. The big catch is the bootstrap and runtime are fairly closely coupled when an in-build (eg hermetic) runtime is used -- the bootstrap assumes the interpreter is in the runfiles, but in your case, you want it to come from outside. Hm, now it's sounding contradictory. (Such isn't the case for a platform (non in-build) runtime, because it assumes the system provides it somehow).

    What might work better is to switch the tool chain when building for your deployment environment. Switch it to a runtime_env tool chain, or equivalent, which assumes it has to get python from the environment. Though, setting that up is a bit of a pain. A transition or config change is also annoying, perf wise. Hm.

    I'd be ok with adding a runfiles_without_runtime field, or changing runfiles_without_exe to also exclude the python runtime, or having separate fields (base binary runfiles, exe, py runtime). You'll probably end up needing to generate your own bootstrap, though, which is tedious.

  11. FrankPortman commented on Nov 8, 2025

    @FrankPortman
    Contributor

    @malt3 this sounds related to #3324 WDYT? I can share the heuristic I've settled on for removing the "extra stuff".

  12. rickeylev commented on Feb 7, 2026

    @rickeylev
    Collaborator

    PyExecutableInfo exposes extra info now that should make OP possible. In particular, there are separate fields for the app's runfiles and the interpreter files.

    See the docs for more info:
    https://rules-python.readthedocs.io/en/latest/api/rules_python/python/py_executable_info.html#PyExecutableInfo

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 rules

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions