Skip to content

Commit c0be385

Browse files
authored
Add best pratice for how to check in generated files (#16405)
Best practice here is to have a test that checks if the generated file matches, but that wasn't documented anywhere.
2 parents e364307 + f488dd5 commit c0be385

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/doc/src/reference/build-script-examples.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ There’s a couple of points of note here:
8686
such crate as a dependency, because there's an *implicit* invariant that
8787
sources in `.cargo/registry` should be immutable. `cargo` won't allow such
8888
scripts when packaging.
89+
* Sometimes, projects want to check in a generated file, and treat it as
90+
source code. However, in this case, the file shouldn't be generated from
91+
`build.rs`. Instead, have a test or similar which checks that the file
92+
precisely matches the generated version *and fails if the result doesn't
93+
match*, and run that test as part of your CI. (The test can generate a
94+
temporary file to compare to, and if you want to update the generated file,
95+
you can replace the checked-in file with that temporary file.)
8996
* This script is relatively simple as it just writes out a small generated file.
9097
One could imagine that other more complex operations could take place such as
9198
generating a Rust module from a C header file or another language definition,

0 commit comments

Comments
 (0)