# Do build scripts and procedural macros run code on my machine when I build?
Source: https://ruststudio.org/community/t/106/do-build-scripts-and-procedural-macros-run-code-on-my-machine-when-i-build
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: security, cargo, supply-chain, build-scripts | Views: 0

## Short answer
Yes. A build.rs file in your project or any dependency is compiled and run during cargo build with your user's permissions, and procedural macros run inside the compiler. Reading a dependency's build script and macros is part of reviewing it, and untrusted code should be built in a sandbox.

## Answer
## Answer
Downloading and compiling a crate is not passive. Two mechanisms run arbitrary code **during the build**:

- A **build script** (`build.rs`) is a normal Rust program that Cargo builds and runs before compiling the crate. It exists for good reasons (compiling C code, generating bindings), but it can read files, use the network and start programs, with the same permissions as the user who typed `cargo build`.
- A **procedural macro** is a crate that the compiler loads and runs while it expands your code.

The demonstration is harmless: a project whose `build.rs` only prints messages through Cargo's `cargo::warning` channel.

```rust title=build.rs
fn main() {
    // A build script is an ordinary program. It runs on your machine, with your permissions, during `cargo build`.
    let name = std::env::var("CARGO_PKG_NAME").unwrap();
    println!("cargo::warning=build.rs is running for package {name}");
    println!("cargo::warning=it can read your environment: PATH is set = {}", std::env::var_os("PATH").is_some());
}
```

Running `cargo build` on it:

```text
   Compiling bs v0.1.0 (C:\...\bs)
warning: bs@0.1.0: build.rs is running for package bs
warning: bs@0.1.0: it can read your environment: PATH is set = true
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s
```

The messages appear while the project is **built**, before any program of yours is run. A malicious build script could do far more than print.

## What to do about it
1. Treat every new dependency that has a `build.rs` or is a proc-macro crate as code you are running; review it as described in the thread on reviewing a new dependency.
2. Do not build untrusted projects on a machine that holds secrets. Use a container, a virtual machine or a throw-away account, with no access to your SSH keys or tokens.
3. Keep CI runners free of long-lived credentials, and give builds no network when the dependencies are vendored or cached (`cargo vendor`, `--offline` and `--locked`).
4. Pin versions with `Cargo.lock` so a new release of a dependency does not run new code without review.
5. Be aware that the same applies to an IDE: opening a project can run `cargo check`, which runs build scripts and macros. Open unknown projects in a safe environment first.

## Common mistakes
- Believing that `cargo check` is "just checking": it still runs build scripts and procedural macros.
- Reviewing the library code but not `build.rs`.
- Giving the build environment production credentials "for convenience".

## Related questions
### Can I list the crates in my tree that have a build script?
Cargo has no single option for it. In the JSON from `cargo metadata`, a package with a build script has a target whose `kind` is `custom-build` (checked on the test project above). You can also look for `build.rs` in the sources that `cargo vendor` copies into one folder.

### Does vendoring remove the risk?
It removes surprise updates and the network at build time. The vendored code still runs when you build it, so it still needs review.

## Sources
- The Cargo Book, "Build Scripts": https://doc.rust-lang.org/cargo/reference/build-scripts.html
- The Rust Reference, "Procedural Macros": https://doc.rust-lang.org/reference/procedural-macros.html
- The Cargo Book, `cargo vendor`: https://doc.rust-lang.org/cargo/commands/cargo-vendor.html
