Location of generated non-binary artifacts

Cargo exports an “OUT_DIR” environment variable to build scripts so that build scripts can store outputs there. And for generated source code this is fine. However when generating final artifacts that should be distributed with the binary, such as documentation (man pages) or shell completion scripts, storing them in the OUT_DIR is somewhat problematic. Not only is OUT_DIR different per package, but the path has some kind of hash in it, and isn’t included in the output of cargo metadata. Which makes such artifacts difficult to discover with tools like cargo-deb or to write robust packaging scripts.

As a specific example, ripgrep uses clap to generate shell completion scripts, which it stores in the OUT_DIR, and the maintainer uses cargo-deb to produce debian packages. However, the resulting debs don’t contain the completion scripts. cargo-deb has a way to specify additional assets by path to include, but since the OUT_DIR isn’t fixed, it isn’t possible to specify a fixed path for the completion scripts.

I’m not sure what the solution is, but I think there should be a better way to generate final artifacts in a way that makes it easy to package them with the binary.

CARGO_TARGET_DIR might work?

Is CARGO_TARGET_DIR set by cargo when it runs build.rs even if it isn’t set by the user? It isn’t entirely clear from the documentation, and I haven’t tested it out.

I have confirmed that CARGO_TARGET_DIR is not currently set by cargo (it is used to allow users to configure the target path for cargo). However, if it was set (or maybe something with the same meaning but different name), that could be a solution.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.