Skip to content

rerun cpp-overload-test build script when src/ changes - #63

Open
0xull wants to merge 1 commit into
rustfoundation:mainfrom
0xull:build-rs-rerun-if-changed
Open

0xull wants to merge 1 commit into
rustfoundation:mainfrom
0xull:build-rs-rerun-if-changed

Conversation

@0xull

@0xull 0xull commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

related to #61

adds cargo::rerun-if-changed=src to cpp-overload-test/build.rs.

once a build script prints any rerun-if line, cargo stops rescanning the package (cargo docs). so editing a cpp! block never reran the script, and the build failed with "This cpp! macro is not found in the library's rust-cpp metadata" until build.rs was touched.

after touch cpp-overload-test/src/stdcpp/forward_list.rs and cargo build -v:

before:

       Dirty cpp-overload-test v0.0.0 (...): the file `cpp-overload-test/src/stdcpp/forward_list.rs` has changed (...)

after:

       Dirty cpp-overload-test v0.0.0 (...): the file `cpp-overload-test/src` has changed (...)
     Running `.../target/debug/build/cpp-overload-test/<hash>/out/build_script_build`

Signed-off-by: Ikeh Chukwuka <mrikehchukwuka@gmail.com>
@0xull

0xull commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

hi @teor2345

i was thinking of these edits to the docs, since the build.rs change somewhat affects this section:

 ## Testing Quirks

-Sometimes the C++ code extracted by `cpp_build` will become outdated, and you will get errors like:
+`cpp-overload-test/build.rs` tells Cargo to rerun `cpp_build` whenever a file in
+`cpp-overload-test/src` changes, so new or changed `cpp!` blocks are picked up on the next build.
+
+If you still get errors like:

```text
 error: This cpp! macro is not found in the library's rust-cpp metadata.
        NOTE: Only cpp! macros found directly in the program source will be parsed -
        NOTE: They cannot be generated by macro expansion.
 ...
 error: cannot find macro `__cpp_closure_impl` in this scope
```

-To fix these errors, run `cargo clean` then rebuild.
-If the errors still happen after a clean, check your Rust and C++ code is compiling correctly.
+check that the `cpp!` block is written directly in `cpp-overload-test/src`, and isn't generated by
+another macro, or used in an example or binary (`cpp_build` only runs for `lib.rs`).
+If the errors still happen, run `cargo clean` then rebuild, and check your Rust and C++ code is
+compiling correctly.

what do you think about it?

@teor2345

Copy link
Copy Markdown
Collaborator

hi @teor2345

i was thinking of these edits to the docs, since the build.rs change somewhat affects this section:

Go for it, it is easier for me to review doc changes in the GitHub changes view than as a diff in a comment.

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good but I'd like to add a CI test if possible.

// `cc` prints `rerun-if-env-changed` lines, which turns off Cargo's default rescan of the
// package, so instruct Cargo to rerun this script whenever `src/` changes and pick up
// `cpp!` updates.
println!("cargo::rerun-if-changed=src");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a CI test for this change?
This will make sure:

  • the change does what it is documented to do
  • it keeps on doing that when we make future changes to the build script, or upgrade cpp or cpp_build
  • it works in the CI environment

For example, you could add a CI test script that:

  1. Builds some cpp! code and checks its output
  2. Adds a new cpp! Invocation and/or changes the C++ code inside an existing one
  3. Rebuilds and checks the build is successful and the output has changed

You could create a replacement file (which isn't in the build by default) and use cp to copy it over the original file in step 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants