Skip to content

Commit b8ba7ae

Browse files
authored
docs(build-script): clarify OUT_DIR is not cleaned between builds (rust-lang#16437)
### What does this PR try to resolve? _This PR documents the behavior of the OUT_DIR environment variable, clarifying that Cargo does not clean or reset it between_ _builds. The contents of OUT_DIR may persist across rebuilds, which can surprise build scripts that assume a clean directory._ _This behavior is intentional to support incremental builds (such as native code compilation), but was not clearly stated in the_ _documentation._ Fixes rust-lang#16332 ### How to test and review this PR? _Documentation-only change._ _Review the updated notes in the Build Scripts and Environment Variables sections._
2 parents f47d62e + cdc10ac commit b8ba7ae

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/doc/src/reference/build-scripts.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ Build scripts may save any output files or intermediate artifacts in the
7373
directory specified in the [`OUT_DIR` environment variable][build-env]. Scripts
7474
should not modify any files outside of that directory.
7575

76+
> **Note:** Cargo does not clean or reset `OUT_DIR` between builds. The contents
77+
> of this directory may persist across rebuilds, even if the build script is
78+
> re-run. This behavior is intentional to support incremental builds, such as
79+
> native code compilation.
80+
>
81+
>Build scripts should not rely on `OUT_DIR` being empty, as its contents may
82+
>persist across rebuilds. If a script requires a clean directory, it is currently
83+
>responsible for managing or cleaning up any files or subdirectories it creates.
84+
>Future improvements in this area are being discussed (see
85+
>[#16427](https://github.com/rust-lang/cargo/issues/16427) and
86+
>[#9661](https://github.com/rust-lang/cargo/issues/9661)).
87+
7688
Build scripts communicate with Cargo by printing to stdout. Cargo will
7789
interpret each line that starts with `cargo::` as an instruction that will
7890
influence compilation of the package. All other lines are ignored.

src/doc/src/reference/environment-variables.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ corresponding environment variable is set to the empty string, `""`.
255255
file extension, such as `.exe`.
256256
* `OUT_DIR` --- If the package has a build script, this is set to the folder
257257
where the build script should place its output. See below for more information.
258-
(Only set during compilation.)
258+
(Only set during compilation.) Cargo does not guarantee that this directory
259+
is empty, and it is not cleaned between builds.
259260
* `CARGO_BIN_EXE_<name>` --- The absolute path to a binary target's executable.
260261
This is only set when building an [integration test] or benchmark. This may
261262
be used with the [`env` macro] to find the executable to run for testing
@@ -366,7 +367,10 @@ let out_dir = env::var("OUT_DIR").unwrap();
366367
> Some cfg values like `test` are not available.
367368
* `OUT_DIR` --- the folder in which all output and intermediate artifacts should
368369
be placed. This folder is inside the build directory for the package being built,
369-
and it is unique for the package in question.
370+
and it is unique for the package in question. Cargo does not clean or reset this
371+
directory between builds, and its contents may persist across rebuilds. Build
372+
scripts should not assume that `OUT_DIR` is empty, and are responsible for
373+
managing or cleaning up any files they create.
370374
* `TARGET` --- the target triple that is being compiled for. Native code should be
371375
compiled for this triple. See the [Target Triple] description for more information.
372376
* `HOST` --- the host triple of the Rust compiler.

0 commit comments

Comments
 (0)