Rerun build script of the dependecies

Hi Community, I have some problems with build a crate A which depends on another crate B that has a build script. Here is the details of my setup and problem.
I have crate B which uses the build script to generate some code and I have crate A which depends on crate B. If I build crate B from scratch which means there is no cache, clean target folder, I could build crate B successfully. I think it means that compiling crate B will trigger the compilation of crate A and runs the build script of crate B.
But if I remove the code generated by the build script in crate B and rebuild crate A which means there is some cache in the target folder then I got a bunch of missing code errors.
The behavior I expect is that rebuilding crate A could also rerun the build script of crate B. I saw it is recompiling the crate B but not rerun the build script. I am wondering if there is a way that could let me specify that behavior.
The reason why I have this problem is that I am using some cache across different machine. So when building on different machine, the cache is there but the code hasn't been generated. I am wondering if the easy fix is to remove crate B from the cache. If it is, how I should do it?
Thank you ahead for your help.

Is B's build script writing to the correct directory?

Build scripts may save any output files or intermediate artifacts in the directory specified in the OUT_DIR environment variable. Scripts should not modify any files outside of that directory.

https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script

If not, that might explain why Cargo did not notice the missing files.

I didn't set OUT_DIR environment variable and the build script just writes to the src directory of the crate. It works totally fine when I compile crate B independently(the build script get run when the code is not generated). The problem happens when I try to compile crate A which depends on B.

And I also noticed that if I use rerun-if-change instruction, the problem will be gone. Does that mean the cache I am using has some information that blocks the build script gets rerun?

I didn't set OUT_DIR environment variable and the build script just writes to the src directory of the crate.

Yes, that's not how build scripts are expected to work. They must read the OUT_DIR environment variable and only write files into that directory — not into src/.

And I also noticed that if I use rerun-if-change instruction, the problem will be gone. Does that mean the cache I am using has some information that blocks the build script gets rerun?

Yes. Build scripts often do very expensive tasks like running C compilers. Thus, Cargo tracks what they depend on (using rerun-if-changed) and doesn't run the script again if it isn't necessary.

Cargo even checks that build scripts don't change the source directory when you try to upload a crate to crates.io.

Thank you so much for your reply. I figured out the problem. It is exactly what you pointed out. I shouldn't use build script to write to src directory. After fixing that, the problem is gone.