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! |
…ilure Summary: buildscript_run.py runs a crate's build script with `check_output(encoding="utf-8")` and exits with the script's code when it fails. Two things go wrong for build scripts that compile C code with cc-rs, which forwards the C compiler's output byte for byte as `cargo:warning=` lines on stdout: - One byte that is not UTF-8 (a compiler message about a Latin-1 source line, a compiler in a non-UTF-8 locale) raises UnicodeDecodeError, and the action fails although the build script succeeded. Cargo builds the same crate; it does not read directives from such lines. - When the build script fails, its stdout is dropped, and with it the compiler's error message. Cargo prints it under `--- stdout`. Decode with `surrogateescape`, skip directive parsing for lines that are not UTF-8 (they are still echoed, with replacement characters), and write the captured stdout out before exiting with the build script's code. Test Plan: `python3 -m unittest tests/test_buildscript_run.py` in `prelude/rust/tools`: a build script printing `cargo:warning=caf\xe9`, `cargo:rustc-cfg=bad\xe9` and `cargo:rustc-cfg=ok` now yields `--cfg=ok` only (before: UnicodeDecodeError); a build script that prints cc-rs style lines and exits 101 now exits 101 with those lines on stdout (before: UnicodeDecodeError, and without the non-UTF-8 line exit 101 with no output). The existing test still passes.
c2e9ed9 to
2cd0b27
Compare
|
This pull request has been imported. If you are a Meta employee, you can view this in D134594617. (Because this pull request was imported automatically, there will not be any future comments.) |
…ilure
Summary:
`prelude/rust/tools/buildscript_run.py` runs a crate's build script with `check_output(encoding="utf-8")`. Build scripts that compile C code with cc-rs forward the C compiler's output byte for byte as `cargo:warning=` lines on stdout. One byte that is not UTF-8 there makes the action fail with UnicodeDecodeError although the build script succeeded; Cargo builds the same crate. When the build script fails, its stdout is dropped, so the compiler error cc-rs forwarded is never shown; Cargo prints it under `--- stdout`.
## Affected version
Checked at main 4b47e1430e; `buildscript_run.py` is the same in the latest release tag `2026-11-15`.
## Reproduction
```
mkdir -p repro/manifest && cd repro
printf '#!/bin/sh\nprintf "cargo:warning=caf\\351\\n"\nprintf "cargo:rustc-cfg=ok\\n"\n' > ok.sh
printf '#!/bin/sh\nprintf "running: cc -c foo.c\\n"\nprintf "cargo:warning=foo.c:1:10: fatal error: missing.h\\n"\nexit 101\n' > fail.sh
printf '#!/bin/sh\necho rustc 1.0.0\n' > rustc
chmod +x ok.sh fail.sh rustc; echo unix > rustc_cfg
for s in ok.sh fail.sh; do
OUT_DIR=$PWD/out RUSTC=$PWD/rustc TARGET=x86_64-unknown-linux-gnu HOST=x86_64-unknown-linux-gnu \
python3 ../prelude/rust/tools/buildscript_run.py --buildscript=$s --rustc-cfg=rustc_cfg \
--manifest-dir=manifest --create-cwd=cwd --outfile=flags; echo "$s exit=$?"
done
```
## Expected
`ok.sh`: exit 0 and `--cfg=ok` in `flags`, as `cargo build` does for the same build script output. `fail.sh`: exit 101 with the two lines the build script printed, as Cargo shows them.
## Actual
```
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 17: invalid continuation byte
ok.sh exit=1
fail.sh exit=101
```
`fail.sh` prints nothing.
## Analysis
`run_buildscript` decodes the whole stdout strictly as UTF-8, so a single invalid byte anywhere raises. Its `CalledProcessError` handler calls `sys.exit(ex.returncode)` without writing `ex.output`; only the build script's stderr, which is not captured, reaches the user, and cc-rs puts the compiler's diagnostics on stdout.
## Patch
Decode with `surrogateescape`, do not read directives from lines that are not UTF-8 (they are still echoed, with replacement characters), and write the captured stdout out before exiting with the build script's code. Two tests are added to `prelude/rust/tools/tests/test_buildscript_run.py`; both fail before the change (UnicodeDecodeError; without the non-UTF-8 line the failing script exits 101 with empty stdout) and pass after it. The existing test still passes.
X-link: meta/buck2#1662
Differential Revision: D134594617
Pulled By: dtolnay
fbshipit-source-id: 995c0b2477d96cb28fee442960bf47fc9087e4a2
Summary
prelude/rust/tools/buildscript_run.pyruns a crate's build script withcheck_output(encoding="utf-8"). Build scripts that compile C code with cc-rs forward the C compiler's output byte for byte ascargo:warning=lines on stdout. One byte that is not UTF-8 there makes the action fail with UnicodeDecodeError although the build script succeeded; Cargo builds the same crate. When the build script fails, its stdout is dropped, so the compiler error cc-rs forwarded is never shown; Cargo prints it under--- stdout.Affected version
Checked at main 4b47e14;
buildscript_run.pyis the same in the latest release tag2026-11-15.Reproduction
Expected
ok.sh: exit 0 and--cfg=okinflags, ascargo builddoes for the same build script output.fail.sh: exit 101 with the two lines the build script printed, as Cargo shows them.Actual
fail.shprints nothing.Analysis
run_buildscriptdecodes the whole stdout strictly as UTF-8, so a single invalid byte anywhere raises. ItsCalledProcessErrorhandler callssys.exit(ex.returncode)without writingex.output; only the build script's stderr, which is not captured, reaches the user, and cc-rs puts the compiler's diagnostics on stdout.Patch
Decode with
surrogateescape, do not read directives from lines that are not UTF-8 (they are still echoed, with replacement characters), and write the captured stdout out before exiting with the build script's code. Two tests are added toprelude/rust/tools/tests/test_buildscript_run.py; both fail before the change (UnicodeDecodeError; without the non-UTF-8 line the failing script exits 101 with empty stdout) and pass after it. The existing test still passes.