rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

`rust_test()` doesn't set `OUT_DIR`

Open rejuvenile opened this issue 1 month ago • 1 comments

My use case is calling a Rust macro in my test which expects to find generated code relative to the OUT_DIR compilation-time environment variable. It's likely that rust_binary() doesn't set this either; I haven't checked because I use a genrule to create the output files.

rejuvenile avatar Nov 25 '25 23:11 rejuvenile

Note, I don't use cargo_build_script() here due to compilation difficulties.

rejuvenile avatar Nov 25 '25 23:11 rejuvenile

There isn't a general concept of an OUT_DIR independent of a cargo_build_script - your genrule can output code, but it won't know what a shared directory exposed to the rust code would be.

You can use cargo_build_script to write to the OUT_DIR, or you can use some other way of passing the path to the rust code being compiled.

illicitonion avatar Dec 15 '25 20:12 illicitonion

Got it. It appears some third party Rust code always assumes OUT_DIR exists, thus my question. I've worked around it via env, but that is a bit tricky as it's somewhat restrictive what can be set to an environment variable in Bazel. Maybe this is uncommon, but might be useful to compile rust code generally if code like this is in wide circulation. Feel free to close.

rejuvenile avatar Dec 17 '25 18:12 rejuvenile