Repository navigation
Description
The Problem
The data attribute appears on each of the core Rust rules (e.g. rust_binary). The documentation for data says
This attribute can be used to specify any data files that are embedded into the library, such as via the
include_str!macro.
I have found that include_str()! works only for "source" files (i.e. files included in the Bazel source tree). Unfortunately, the include_str!() macro does not work if the data files are generated by some other Bazel target.
Example
To demonstrate this, I've added a small test to my rules_rust fork.
In this test, one data file is a source file and one data file is generated by a genrule. The contents of both files are embedded in the rust_test via the include_str!() macro.
On my linux system, this rust_test fails to compile with the following rustc error:
error: couldn't read test/generated_data/include_str/generated_data.txt: No such file or directory (os error 2)
--> test/generated_data/include_str/include_str.rs:9:13
|
9 | include_str!("generated_data.txt").trim()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
Notice that rustc has no problem finding source_data.txt.
I believe that the problem can be seen by inspecting this rustc action's sandbox. On my Linux system, if I run the test with bazel test --sandbox_debug, and then change directory to <bazel_workspace_output_base>/sandbox/linux-sandbox/<action_id>/execroot/io_bazel_rules_rust, the root of the problem becomes clear: the library and the source data are sibling files
./test/generated_data/include_str/include_str.rs
./test/generated_data/include_str/source_data.txt
but the generated file is not
./bazel-out/k8-fastbuild/bin/test/generated_data/include_str/generated_data.txt
So, the generated data file is an input to the rustc Bazel action, but it isn't anywhere that the rustc executable can find it. Thus, a compiler error occurs.
Temporary Workaround
In my project where I actually want this feature I have an awful-dreadful-ugly hack to overcome this problem: I add a symlink from my source tree pointing to where I expect the generated file should be under the Bazel workspace's output base; then I add both the generated file and its symlink to the data attribute.
A Very Rough Idea for a Solution
I have a little bit of experience writing Bazel rules that also needed to deal with this problem. My solution there was for each target to add various ctx.actions.symlink() actions to place both generated files and source files into a new directory under their logical paths before calling the tool.
Activity
mfarrugi commentedon Oct 21, 2020
dwtj commentedon Oct 21, 2020
Oh shucks. I think you're right. I completely missed these. Sorry for the possible duplicate. I'll read #79 in a bit.
Nope. I just tried using a
genruleto generate a.rsfileand then including
generated_rust.rsin thesrcsattr instead. But this gives the same compiler error,Again, looking at the sandbox contents, this generated file is over in
To quote the Rust documentation for
include_str!(),dae commentedon Oct 21, 2020
I'm thinking that make variable expansion in rustc_env might be a cleaner way to solve this, and will try to get a PoC PR up soon. In the mean time, you can use make variable expansion to get one level up from a build script's OUT_DIR, which your script can write files to, eg:
Then in your code:
This requires the crate to have a build script in order for OUT_DIR to be set.
expand location variables in rustc_env
dwtj commentedon Oct 22, 2020
@dae Thanks for your workaround approach. This is sufficient for my needs at the moment.
I've added a full example of this workaround approach to a branch of my fork and reproduced it here:
The key to this hack is that this crate's
OUT_DIRjust happens to be a sibling file to the generated file.@dae One nitpick about your suggestion. You said
However, I don't really see how either make variable substitution or using the cargo build script to write the files is really necessary here. My example, for instance, doesn't use either.
jfirebaugh commentedon May 20, 2021
Is the workaround from @dwtj still the best way to include Bazel-generated data files, or was #468 intended to provide a better alternative? If so, is there an example of the preferred technique somewhere?
jfirebaugh commentedon May 20, 2021
Ah, found it: https://github.com/bazelbuild/rules_rust/blob/main/examples/env_locations/BUILD.bazel