You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
non-root requirement seems too much for an ordinary user of rules_python #1169
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?
Reacted by Néstor Subirón, faximan, Suzen Fylke and 372046933
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.
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?
^ 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.
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.
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.
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.
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.
Activity
nsubiron commentedon Apr 18, 2023
You can disable the error
faximan commentedon Apr 19, 2023
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.
phlax commentedon Apr 19, 2023
we hit the same issues - my feeling is that this is just not the responsibility of
rule_pythonto enforce thisqzmfranklin commentedon Apr 26, 2023
To be fair, it's probably the CPython to blame, as they programmed to produce the
.pycfiles on the fly.Not sure if they are willing to make a flag to NOT generate
.pycfiles on thestdlibon the fly just for this use case, though.daixiang0 commentedon May 10, 2023
Works for me!
tsaarni commentedon Sep 13, 2023
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_erroroption be exposed to the user who runs the build of some random project "xyz", which happens to userules_pythoninternally? What would be the standard approach to do so?learnsomethingtoday commentedon Jan 10, 2024
@daixiang0 / @nsubiron Could you please let me know how and where did u make this edit?
cpsauer commentedon Feb 1, 2024
^ 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.
Switch back off rules_python.
calebzulawski commentedon May 16, 2024
Could this setting at least be overridden with an environment variable? I have a
rules_$companymodule 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.rickeylev commentedon May 31, 2024
Yes, I think thats fine. Send a PR?
re: the issue in general: Unfortunately, I don't see any great solutions to this problem.
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.
bazel: allow rules_python to run with root user
Work around bazel's python_rules root user restriction.
bluec0re commentedon Nov 21, 2024
@rickeylev Maybe I'm missing sth, but there are several ways to deal with pyc generation without having to mark directories as read-only:
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.
rickeylev commentedon Nov 21, 2024
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.
-Bet al are slightly better, but then have the issue of "What if the caller sets the env var?".sys.dont_write_bytecodeis 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.
12 remaining items
fix: Downgrade "running as root" error to a warning by default
fix: Downgrade "running as root" error to a warning by default
fix: Downgrade "running as root" error to a warning by default
fix: Downgrade "running as root" error to a warning by default
fix: Downgrade "running as root" error to a warning by default
fix(pypi): use python -B for all invocations
fix: Downgrade "running as root" error to a warning by default (#2636)
faximan commentedon Mar 3, 2025
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?
alexeagle commentedon Mar 3, 2025
@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.
f0rmiga commentedon Mar 3, 2025
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.
fix: Downgrade "running as root" error to a warning by default (bazel…
fix(pypi): use python -B for repo-phase invocations (#2641)
fausturs commentedon Mar 17, 2025
If you are sure you need run as root, just add following to your MODULE.bazel (in your root project)
(maybe you need adjust the rules_python version, and the python_version to match your env)
it works for me.
thesayyn commentedon May 8, 2026
Whoever thought it was a good idea to force users to run as non-root needs a raise.