Skip to content

non-root requirement seems too much for an ordinary user of rules_python #1169

Closed

Description

@qzmfranklin

🐞 bug report

Affected Rule

The issue is caused by the rule:

During the loading phase.

Is this a regression?

I don't think so.

Description

Per #713 , rules_python cannot be used by root.

CI systems such as CircleCI and BuildBuddy uses root user by default.

Setting up non-root in those systems aren't very straightforward.

Also, it does not look like a responsibility of an ordinary user of rules_python to have to do this.

🔬 Minimal Reproduction

Example build event:

https://app.buildbuddy.io/invocation/55238174-54c3-459c-8d80-72722f2d00f9

🔥 Exception or Error

Relevant error message:

Error in fail: The current user is root, please run as non-root when using the hermetic Python interpreter. See https://github.com/bazelbuild/rules_python/pull/713.




🌍 Your Environment

Operating System:

  
Linux, CentOS8, WSL2-Ubuntu22.04
  

Output of bazel version:

  
6.1.1
  

Rules_python version:

  
1e869d8d945f0b406da65817cf70b4fa3105a0de
  

Anything else relevant?

Activity

  1. nsubiron commented on Apr 18, 2023

    @nsubiron

    You can disable the error

    python_register_toolchains(
        name = ...,
        python_version = ...,
        ignore_root_user_error = True,
    )
  2. faximan commented on Apr 19, 2023

    @faximan
    Contributor

    It is good there is an option to choose but you are choosing between two evils: either you get the pyc cache misses or you have to go out of your way to support non-root builds (e.g. when building in a docker container).

    We have opted for the second evil, but it creates so much extra work.

    I really wish there was a way to solve the cache issue without this extra requirement.

  3. phlax commented on Apr 19, 2023

    @phlax
    Contributor

    we hit the same issues - my feeling is that this is just not the responsibility of rule_python to enforce this

  4. qzmfranklin commented on Apr 26, 2023

    @qzmfranklin
    Author

    To be fair, it's probably the CPython to blame, as they programmed to produce the .pyc files on the fly.

    Not sure if they are willing to make a flag to NOT generate .pyc files on the stdlib on the fly just for this use case, though.

  5. daixiang0 commented on May 10, 2023

    @daixiang0

    You can disable the error

    python_register_toolchains(
        name = ...,
        python_version = ...,
        ignore_root_user_error = True,
    )

    Works for me!

  6. tsaarni commented on Sep 13, 2023

    @tsaarni

    When it is not feasible to run as non-root, currently one needs to modify the build files of the project. This is bit clumsy. Could the ignore_root_user_error option be exposed to the user who runs the build of some random project "xyz", which happens to use rules_python internally? What would be the standard approach to do so?

  7. learnsomethingtoday commented on Jan 10, 2024

    @learnsomethingtoday

    You can disable the error

    python_register_toolchains(
        name = ...,
        python_version = ...,
        ignore_root_user_error = True,
    )

    Works for me!

    @daixiang0 / @nsubiron Could you please let me know how and where did u make this edit?

  8. cpsauer commented on Feb 1, 2024

    @cpsauer

    ^ I think this workaround doesn't suffice for build tools seeking to use rules_python in their implementations, since they aren't the root module. More context over in hedronvision/bazel-compile-commands-extractor#166

    It'd be quite valuable to have this just work out of the box. That is, if running as root, fall back to the best behavior you can rather than erroring.

  9. added a commit that references this issue on Feb 1, 2024
  10. calebzulawski commented on May 16, 2024

    @calebzulawski

    Could this setting at least be overridden with an environment variable? I have a rules_$company module that is used by maybe a dozen separate repos that establishes uniform toolchains and CI scripts among other things. Since the toolchain can only be set by the root module, I have no way of overriding this other than separately tracking the toolchain in each repo.

  11. rickeylev commented on May 31, 2024

    @rickeylev
    Collaborator

    Could this setting at least be overridden with an environment variable?

    Yes, I think thats fine. Send a PR?

    re: the issue in general: Unfortunately, I don't see any great solutions to this problem.

    Fall back to best behavior you can

    I am somewhat inclined to agree. The reason being: the problem a writable install causes is spurious rebuilds. When it's an error to have a writable install, and people have to opt out, then they're just going to opt out, because that going to mostly work, rather than entirely not work. And they just have to live with spurious rebuilds.

    I don't see any interpreter options or environment variable to use hashed-based pycs instead of timestamps. Which is too bad; it would have been a nice fix to this. Nor do I see any options to control where pycs are read-from/written-to for just for the stdlib.

    When a runtime is initially downloaded and extracted, we could run a compile step to pre-populate the pyc files. However, this only works if the downloaded runtime runs on the host. If we could somehow ensure that there was a host-runnable python of the same version available, then that could be used to perform the compilation; or if we had a prebuilt tool to perform this. This does seem like one of the better options, though. If the host can't run the Python, then we just move along.

    Alternatively, the pyc could be generated at build time. We can do this for regular libraries now; I don't see why we couldn't also do it for the py files coming from the runtime. The downside is it'll add some build overhead, since there's a few thousand files to compile.

    Maybe if the stdlib was in a zip file we'd have more options? This idea was floated as a way to reduce the number of files in the runtime, too. I'm not sure where Python will write pyc files if it reads a file from a zip.

    It'd be most ideal if the upstream python-standalone packages came with the pyc files already. That would make this go away entirely.

  12. bluec0re commented on Nov 21, 2024

    @bluec0re

    @rickeylev Maybe I'm missing sth, but there are several ways to deal with pyc generation without having to mark directories as read-only:

     ➜ docker run --rm -it python:3.11 bash
    root@8c07a07f5535:/# find /usr -name '*.pyc' | wc -l
    1031
    root@8c07a07f5535:/# find /usr -name '__pycache__' -exec rm -rf {} +
    root@8c07a07f5535:/# find /usr -name '*.pyc' | wc -l
    0
    root@8c07a07f5535:/# PYTHONPYCACHEPREFIX=/tmp/pycs python3 -c 'import sys; print(sys.version)'
    3.11.3 (main, May 23 2023, 13:25:46) [GCC 10.2.1 20210110]
    root@8c07a07f5535:/# find /usr -name '*.pyc' | wc -l
    0
    root@8c07a07f5535:/# find /tmp/pycs -name '*.pyc' | wc -l
    4
    root@8c07a07f5535:/# PYTHONDONTWRITEBYTECODE=True python3 -c 'import sys; print(sys.version)'
    3.11.3 (main, May 23 2023, 13:25:46) [GCC 10.2.1 20210110]
    root@8c07a07f5535:/# find /usr -name '*.pyc' | wc -l
    0
    root@8c07a07f5535:/# python3 -c 'import sys; print(sys.version)'
    3.11.3 (main, May 23 2023, 13:25:46) [GCC 10.2.1 20210110]
    root@8c07a07f5535:/# find /usr -name '*.pyc' | wc -l
    4
    

    E.g. the pyc files could be written to a scratch directory, or you can tell the python interpreter to use existing, but not writing new pyc files.

  13. rickeylev commented on Nov 21, 2024

    @rickeylev
    Collaborator

    Unfortunately, environment variables aren't a good option because they get inherited by sub-processes (we've had bug reports due to using env vars and are trying to avoid them). They're also somewhat of a pain because, in the bootstrap, we have to have special logic to try and respect if the caller sets the environment variable. -B et al are slightly better, but then have the issue of "What if the caller sets the env var?".

    sys.dont_write_bytecode is mutable, however, setting that would happen after the interpreter starts up, so anything imported before the bootstrap can set that suffers from the issue. A smaller window, at least, I guess.

    Disabling all runtime pyc generation is also a heavy hammer -- if someone wants to set the pyc cache prefix, then why not? Considering how Bazel works, that's not a bad idea to get pyc files with less overhead. Also, this problem only occurs when Python is trying to create pyc files in the backing repository directory instead of the runfiles directory. I think that behavior is somewhat specific to the interpreter itself; IIRC it jumps through some extra hoops to resolve symlinks and find the "real" location of the interpreter.

  14. 12 remaining items

  15. faximan commented on Mar 3, 2025

    @faximan
    Contributor

    This certainly looks like a pragmatic and reasonable UX fix - thanks for working on it!

    Do I understand it correctly that #2641 is all that is missing for being able to run as non-root and avoid the pyc cache miss issue?

  16. alexeagle commented on Mar 3, 2025

    @alexeagle
    Contributor

    @f0rmiga originally worked on this behavior. My vague recollection was that it's an attempt to be very correct in a way that few users will trip over.

  17. f0rmiga commented on Mar 3, 2025

    @f0rmiga
    Member

    I've always been opinionated on this. Remember that with Bazel it's Correct, Fast, pick two, not one. The current behaviour is CORRECT. If users don't understand the problems that come with running builds as root, then maybe we are not communicating the problem well.

    I don't have a better solution to the problem other than try to educate people on the subject.

    For people who don't care about running as root, I don't think it's a problem for rules_python to solve.

  18. fausturs commented on Mar 17, 2025

    @fausturs

    If you are sure you need run as root, just add following to your MODULE.bazel (in your root project)

    bazel_dep(name = "rules_python", version = "0.40.0", dev_dependency = True)
    python = use_extension("@rules_python//python/extensions:python.bzl", "python")
    python.toolchain(is_default = True, python_version = "3.11", ignore_root_user_error = True)
    

    (maybe you need adjust the rules_python version, and the python_version to match your env)

    it works for me.

  19. thesayyn commented on May 8, 2026

    @thesayyn
    Contributor

    Whoever thought it was a good idea to force users to run as non-root needs a raise.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions