Skip to content

python: Make the bytecode path in compile.py independent of the environment - #1659

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-compile-py-pyc-path
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-compile-py-pyc-path

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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 4b47e14 and on the release 2026-11-15 (5b3b13d); 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.

Test Plan: a new case in prelude/python/tools/tests/compile_test.py runs compile.py with 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.

@meta-cla

meta-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @phr4nky!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

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.
@phr4nky
phr4nky force-pushed the fix-compile-py-pyc-path branch from 983927f to 2b6a89a Compare November 25, 2026 21:48
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@meta-codesync meta-codesync Bot closed this in 7fa179c Nov 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 7fa179c.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants