How to get OUT_DIR without running the build script?


#1

I have a build script that generates some files and puts them in the directory specified by the OUT_DIR environment variable.

However, I want to do some post-processing on some of those files (or even just open them manually and verify they look right), but they’re in a directory like this:

target/debug/build/dose-response-904e10d079f0c018/out/

Is there a Cargo command or some way I can figure out what the out dir path is going to be without running the build script? As far as I can tell, the path doesn’t change when I modify my code and rebuild, but it does change occasionally and I end up with several such directories lying around.


#2

I would also like to know the answer to this! I’ve resorted to creating a stamp file, and then detecting the correct out directory by finding the most recently modified stamp file. This likely isn’t bullet proof, but it has worked for me in practice.

To find the stamp file: https://github.com/BurntSushi/ripgrep/blob/a2d8c49d6f8e4c7810642976174dde8642d1d8e8/ci/utils.sh#L5-L17

To generate the stamp file: https://github.com/BurntSushi/ripgrep/blob/a2d8c49d6f8e4c7810642976174dde8642d1d8e8/build.rs#L35-L38


#3

I suspect the underlying requirement is a way to find final build artifacts. This keeps coming up in different forms, like this discussion which led to https://github.com/rust-lang/cargo/issues/5457

On a related note, cargo is adding an --out-dir so users can control where the binary gets written to but doesn’t solve the problem of build.rs generated artifacts.

This is a problem relevant to the CLI-WG and was part of our discussion yesterday. I’m not yet sure where this will lead but I’m hoping we get a reasonable solution soon.


#4

Ah, thanks for the information! I didn’t realise this would fall under the CLI-WG but that’s good to know.