-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bazel] Unset PYTHONSAFEPATH
in env.bat and env.sh.
#1423
base: main
Are you sure you want to change the base?
Conversation
In Python 3.11 and above `PYTHONSAFEPATH` prevents the containing directory of a script from being imported into `sys.path`. This results in `emcc.py` (and other programs) failing with "ModuleNotFoundError: No module named 'tools'". By unsetting `PYTHONSAFEPATH` in env.bat and env.sh, the expected behavior of importing the containing directory of a script to `sys.path` is restored.
@PiotrSikora what do you think? This is needed for using the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
BTW, where is PYTHONSAFEPATH being set?
I believe it comes from python_bootstrap_template.txt and makes its way into |
If Meson is setting this variable and calling out to Bazel, I'm inclined to think the appropriate solution is for Meson to unset |
@@ -3,3 +3,7 @@ | |||
set ROOT_DIR=%CD% | |||
set EMSCRIPTEN=%ROOT_DIR%\%EM_BIN_PATH%\emscripten | |||
set EM_CONFIG=%ROOT_DIR%\%EM_CONFIG_PATH% | |||
|
|||
:: Ensure PYTHONSAFEPATH is not set so that modules in the same | |||
:: directory as emcc.py and emar.py etc. can be found via sys.path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mention that this is needed because py_binary sets this?
Alternatively, perhaps there is a more canonical way to express this in a py_binary
? i.e. perhaps there is an explicit way to add the emscripten root the PYTHONPATH
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
py_binary
is the more idiomatic solution. Although I am blocked on this at the moment due to a bug in python_rules
.
It is the other way around. Bazel is calling into Meson to build something whose outputs can then be used by other rules. |
can you unset |
It would also be possible to change this in |
It seems that the emscripten bazel toolchain doesn't set it; python rules (I think?) set it. So changing how the python rules work makes sense as a solution, and changing where bazel is calling into Meson makes sense as well. But If it's something that emscripten doesn't set and doesn't read, I'm not sure it makes sense to change it in emscripten. |
It's entirely possible that I don't fully understand the situation, and I appreciate your patience with me if that's the case. Having bazel interact so heavily with another build system is quite foreign to me. |
If bazel wants to set PYTHONSAFEPATH then I think it probably make sense to add the paths we need explicitly using whatever the offiical/recommended way of doing so is. |
Quick explainer: currently we have So why not just compile the dependencies with The solution to this problem is
|
So meson itself it written in python and you use Is meson itself happy to run in The problem seems to be that because meson is a This suggests to me that meson itself should unset |
Exactly
Yes! Meson needs access to other modules but that is part of the functionality that
I have some counter arguments there:
I still feel that unsetting |
Surely there are plenty of builds that run python scripts are part of the build. e.g. any project that does code generation using python. I'm pretty llvm does this, and wabt. Lots of build tools use python don't they? And they would all be effected by this problem I think. |
I would argue that as long as Meson could conceivably run other python subprocesses then it should unset PYTHONSAFEPATH before it does so. I imagine the |
This is exactly the problem.
But how would you limit the scope of that logic? Beyond |
This would only be when meson is running as part of So it seems reasonable to explicitly unset this one? Are there other environment variable that effect python the |
Potential alternative: bazelbuild/rules_python#2060 |
It looks like the rules_python fix is moving along and should fix your issue. Let me know if that isn't the case. |
In Python 3.11 and above
PYTHONSAFEPATH
prevents the containing directory of a script from being imported intosys.path
. This results inemcc.py
(and other programs) failing withModuleNotFoundError: No module named 'tools'
. By unsettingPYTHONSAFEPATH
inenv.bat
andenv.sh
, the expected behavior of importing the containing directory of a script tosys.path
is restored. Resolves #1420.