Skip to content

rust: Keep build script stdout that is not UTF-8 or comes before a failure - #1662

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-buildscript-run-stdout
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-buildscript-run-stdout

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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 4b47e14; 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.

@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!

…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.
@phr4nky
phr4nky force-pushed the fix-buildscript-run-stdout branch from c2e9ed9 to 2cd0b27 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 D134594617. (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 Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 26, 2026
…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
@meta-codesync meta-codesync Bot closed this in 4a9714f Nov 26, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 26, 2026
@meta-codesync

meta-codesync Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@dtolnay merged this pull request in 4a9714f.

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