Repository navigation
Conversation
|
Hi @phr4nky! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
…onment
Summary:
`compile.py` took its default output name from
`importlib.util.cache_from_source`, which follows `PYTHONPYCACHEPREFIX`
and `PYTHONOPTIMIZE`, and `py_compile.compile` takes the optimization
level from `PYTHONOPTIMIZE` as well. `py_compile` actions run the host
interpreter with the environment the action inherits, so a developer
with either variable set got bytecode at a path under the prefix with
the machine's absolute path in it, or named `.opt-1.pyc` with asserts
stripped. Neither is what the packaged program imports, and the output
of the same action differs between machines.
The default format is now `{pkg}/__pycache__/{name}.<cache_tag>.pyc`
and the bytecode is compiled at optimization level 0, as the
interpreter that imports it expects.
Test Plan:
New case in `tests/compile_test.py` runs compile.py with each variable
set; it fails on the parent commit (`pycache/<tmp>/a/b/c.cpython-314.pyc`
and `a/b/__pycache__/c.cpython-314.opt-1.pyc`) and passes with this
change. With either variable set, the pyc is now byte-identical to the
one compiled without it.
983927f to
2b6a89a
Compare
|
This pull request has been imported. If you are a Meta employee, you can view this in D134594468. (Because this pull request was imported automatically, there will not be any future comments.) |
|
This pull request has been merged in 7fa179c. |
…onment
Summary:
`prelude/python/tools/compile.py`, which the `py_compile` action runs for every Python library with toolchains that set `compile` (such as `python_toolchain` in `prelude/toolchains/python.bzl`), takes its output name from `importlib.util.cache_from_source`. That function follows `PYTHONPYCACHEPREFIX` and `PYTHONOPTIMIZE`, and `py_compile.compile` follows `PYTHONOPTIMIZE` as well. Local actions inherit both variables, so a developer who has either one exported gets bytecode the packaged program never imports, and the action output differs from the same action on another machine.
## Affected version
Checked on `main` at 4b47e1430e and on the release `2026-11-15` (5b3b13d3ea); `compile.py` is identical in both.
## Reproduction
From a buck2 checkout in `$BUCK2`, on macOS with Python 3.14:
```
mkdir /tmp/b06 && cd /tmp/b06
printf 'def f(x):\n assert x\n return x\n' > m.py
echo "[[\"pkg/m.py\", \"$PWD/m.py\", \"//x:x\"]]" > in.json
python3 "$BUCK2/prelude/python/tools/compile.py" --output=out1 --bytecode-manifest=bm1.json in.json
PYTHONPYCACHEPREFIX=$PWD/pfx python3 "$BUCK2/prelude/python/tools/compile.py" --output=out2 --bytecode-manifest=bm2.json in.json
PYTHONOPTIMIZE=1 python3 "$BUCK2/prelude/python/tools/compile.py" --output=out3 --bytecode-manifest=bm3.json in.json
python3 -c "import json; [print(json.load(open(f))[0][0]) for f in ('bm1.json', 'bm2.json', 'bm3.json')]"
```
## Expected
All three runs write pkg/__pycache__/m.cpython-314.pyc with the same bytes. That is the file the interpreter running the packaged program looks for, and the action pins `PYTHONHASHSEED` (`prelude/python/compile.bzl`) so that its output is deterministic.
## Actual
```
pkg/__pycache__/m.cpython-314.pyc
tmp/b06/pfx/private/tmp/b06/pkg/m.cpython-314.pyc
pkg/__pycache__/m.cpython-314.opt-1.pyc
```
On macOS `/tmp` resolves to `/private/tmp`, hence the second line. The third file also has the `assert` compiled out, so its bytes differ from the first.
## Analysis
`DEFAULT_FORMAT` in `compile.py` is `importlib.util.cache_from_source("{pkg}/{name}.py")`, evaluated when the script starts. With a pycache prefix set it returns an absolute path under the prefix, which `get_pyc_path` then splits and joins into a relative path that embeds the build machine's directories. With an optimization level set it adds `.opt-1`, and `py_compile.compile` with the default `optimize=-1` compiles at that level. `compile.bzl` runs the host interpreter without `-I` or `-E`, and local actions do not remove either variable from the environment.
## Patch
The default format is now built from `sys.implementation.cache_tag` as `{pkg}/__pycache__/{name}.cpython-3XX.pyc` (on CPython), and the bytecode is compiled with `optimize=0`.
X-link: meta/buck2#1659
Reviewed By: dtolnay
Differential Revision: D134594468
fbshipit-source-id: 92c314b2298206b837c039e1d895ac6ae8893991
Summary
prelude/python/tools/compile.py, which thepy_compileaction runs for every Python library with toolchains that setcompile(such aspython_toolchaininprelude/toolchains/python.bzl), takes its output name fromimportlib.util.cache_from_source. That function followsPYTHONPYCACHEPREFIXandPYTHONOPTIMIZE, andpy_compile.compilefollowsPYTHONOPTIMIZEas well. Local actions inherit both variables, so a developer who has either one exported gets bytecode the packaged program never imports, and the action output differs from the same action on another machine.Affected version
Checked on
mainat 4b47e14 and on the release2026-11-15(5b3b13d);compile.pyis identical in both.Reproduction
From a buck2 checkout in
$BUCK2, on macOS with Python 3.14:Expected
All three runs write pkg/pycache/m.cpython-314.pyc with the same bytes. That is the file the interpreter running the packaged program looks for, and the action pins
PYTHONHASHSEED(prelude/python/compile.bzl) so that its output is deterministic.Actual
On macOS
/tmpresolves to/private/tmp, hence the second line. The third file also has theassertcompiled out, so its bytes differ from the first.Analysis
DEFAULT_FORMATincompile.pyisimportlib.util.cache_from_source("{pkg}/{name}.py"), evaluated when the script starts. With a pycache prefix set it returns an absolute path under the prefix, whichget_pyc_paththen splits and joins into a relative path that embeds the build machine's directories. With an optimization level set it adds.opt-1, andpy_compile.compilewith the defaultoptimize=-1compiles at that level.compile.bzlruns the host interpreter without-Ior-E, and local actions do not remove either variable from the environment.Patch
The default format is now built from
sys.implementation.cache_tagas{pkg}/__pycache__/{name}.cpython-3XX.pyc(on CPython), and the bytecode is compiled withoptimize=0.Test Plan: a new case in
prelude/python/tools/tests/compile_test.pyrunscompile.pywith each variable set. It fails on the parent commit (a path under the prefix, and.opt-1.pyc) and passes with the change. With the change, the reproduction above prints pkg/pycache/m.cpython-314.pyc three times and the three files are byte-identical.