Skip to content

fix: resolve compiler shims through PATH - #1519

Open
rksharma-owg wants to merge 1 commit into
meta:mainfrom
rksharma-owg:fix/buck2-compiler-path-lookup
Open

rksharma-owg wants to merge 1 commit into
meta:mainfrom
rksharma-owg:fix/buck2-compiler-path-lookup

Conversation

@rksharma-owg

Copy link
Copy Markdown
Contributor

Summary

Resolve bare compiler commands through PATH in the Rust build-script compiler shim.

Problem

The default system C++ toolchain can provide a bare compiler name such as clang. from_any_dir.py currently calls os.execl, which does not search PATH, so the build-script path fails with FileNotFoundError even though the same compiler works for ordinary C++ targets.

Root Cause

os.execl requires the executable to be directly resolvable and does not perform PATH lookup.

Fix

Use os.execvp so the compiler command is resolved using the action environment PATH while preserving the original argument vector.

Tests

  • Baseline reproduction: python3 prelude/rust/tools/from_any_dir.py --cwd /tmp clang --version failed with FileNotFoundError.
  • Fixed reproduction: the same command resolved clang and exited successfully.
  • python3 prelude/rust/tools/from_any_dir_test.py
  • python3 -m py_compile prelude/rust/tools/from_any_dir.py prelude/rust/tools/from_any_dir_test.py
  • Fork GitHub Actions: Linux and macOS passed; Windows completed the build and tests but the existing full cargo test command timed out after one hour (run 36185624108: https://github.com/rksharma-owg/buck2/actions/runs/36185624108).

The Windows timeout was not a test assertion failure and is reported here for transparency. Scope is intentionally limited to compiler command resolution in the Rust build-script shim.

Fixes #1491

@meta-cla

meta-cla Bot commented Sep 26, 2026

Copy link
Copy Markdown

Hi @rksharma-owg!

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!

@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 Sep 26, 2026
@meta-cla

meta-cla Bot commented Sep 26, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

meta-codesync Bot pushed a commit that referenced this pull request Nov 27, 2026
Summary:
Cargo always sets `OPT_LEVEL` (`0` to `3`, `s` or `z`) in the environment of a build script. cc-rs requires it: `cc::Build::compile` fails with `Environment variable OPT_LEVEL not defined` when it is missing (cc 1.2.52 and 1.4.x).

`buildscript_run` set `OPT_LEVEL` only when the toolchain's rustc flags contained `-Copt-level=` followed by a digit. So every build script that uses cc-rs (`ring`, `zstd-sys`, `libz-sys`, `psm`, …) fails:

- under a toolchain without such a flag, which includes the default `system_rust_toolchain` (`rustc_flags = []`);
- under `-Copt-level=s` or `-Copt-level=z`, which are excluded by the digit check.

The shim works around this crate by crate: the fixups of `psm`, `stacker` and `aws-lc-fips-sys` set `OPT_LEVEL = "3"`.

This PR takes the last `-Copt-level=` of the rustc flags, `s` and `z` included, and uses rustc's default `0` when there is none. An `OPT_LEVEL` in the target's `env` still takes precedence, as in those fixups.

## Affected version

origin/main 00c2ee4.

## Reproduction

A build script that compiles one C file with cc-rs 1.2.52, run through `buildscript_run` with the prelude's demo toolchains:

```rust
fn main() {
    cc::Build::new().file("foo.c").compile("foo");
}
```

## Expected

The build script runs, as under Cargo.

## Actual

```
OPT_LEVEL = None
error occurred in cc-rs: Environment variable OPT_LEVEL not defined.
```

Pull Request resolved: #1669

Test Plan:
- New `tests/prelude/rust/buildscript_opt_level`: a build script reports its `OPT_LEVEL` as a cfg, and the assertion expects a valid level in `[rustc_flags]`.
- Checked with a locally built buck2 and the demo toolchains on macOS:
  - That build script reports `opt_level=""` on origin/main, `opt_level="0"` with this change, and `opt_level="s"` with `rustc_flags = ["-Copt-level=s"]`.
  - The cc-rs build script above fails on origin/main and builds `libfoo.a` with this change. To get past cc-rs, the local prelude also had the `os.execvp` change of #1519 (#1491): under the default `system_cxx_toolchain`, the next step fails without it.

Reviewed By: dtolnay

Differential Revision: D134832191

fbshipit-source-id: e0c3f9089c49ec8615b58b85ca22992a5cea88e7

This branch has not been deployed

No deployments
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.

Projects

None yet

1 participant