fix: only delete first sys.path entry in the stage-2 bootstrap if PYTHONSAFEPATH is unset or unsupported#2418
Conversation
|
|
||
| # < Python 3.11 behaviour | ||
| if (major, minor) < (3, 11): | ||
| # Because of https://github.com/bazelbuild/rules_python/blob/0.39.0/python/private/stage2_bootstrap_template.py#L415-L436 |
There was a problem hiding this comment.
Whilst adding tests for this change, I discovered what might be another bug?
This snippet always executes on Python 3.10 and below, because safe_path hasn't been implemented yet.
But it will never execute on Python 3.11, because PYTHONSAFEPATH is unconditionally set in stage 1.
Not sure if this is expected behaviour.
There was a problem hiding this comment.
Hmm, I wish I could remember what this was about, exactly. Maybe if I went and dug through the change history...
This might be about how PYTHONSAFEPATH being set by the outer environment is respected (by the bootstrap impl). While stage1 bootstrap will set it by default, it first checks to see if the outer environment has set it. If so, it'll respect that.
|
Thanks for the PR. This mostly LGTM. Just need to use a non-integration test for this instead. See comment. |
75c5ea9 to
4319193
Compare
|
Hrm. I think "intended behavior" here is all murky. I'm pretty sure the intent of At the least, we can confidently say: for 3.11 and later, there shouldn't be any |
aignas
left a comment
There was a problem hiding this comment.
LGTM, I agree with the reasoning in @rickeylev's message above.
|
Ok, I think I know what this logic is about. It's fixing up the path0 entry to point to the proper directory (runfiles instead of the filepath on the command line). At startup, path0 looks like e.g. But, that directory doesn't have much in it, plus it means things outside the runfiles become importable. So the later Hence, if safe path is disabled, we have to go manipulating sys.path. If safe path is enabled, then there is nothing to manipulate. I did come across an edge case while looking into this: I happen to have PYTHONPATH set in my env. on python 3.11, because of that |
This PR should address that problem, shouldn't it? The burning question I have is - should Removing it will have all Python versions behave identically, and in line with historical behaviour. Is there something undesirable about having the script's directory (where it doesn't follow symlinks) in |
59dfcee to
b788eb2
Compare
|
Pretty sure the build failures with It's because |
I think that PYTHONSAFEPATH was being set in the python template in the bazel repo since quite a while ago, so the historic behaviour might be to have PYTHONSPAFEPATH to be used if it is possible. However I can't find a link to the old source quickly, so my memory may be failing me. |
2f5b78b to
6f816cd
Compare
…THONSAFEPATH is unset or unsupported
6f816cd to
c2ebbc4
Compare
|
Anything left outstanding from my end to get this across the line? :) |
|
LGTM. Sorry, took me awhile to get the free time to finish reviewing. |
Unnconditionally deleting the first
sys.pathentry on the stage-2 bootstrap incorrecly removes a valid search path on Python 3.11 and above, sincePYTHONSAFEPATHis already unconditionally set in stage-1.It should be deleted only if it is unset or unsupported.
Fixes #2318