bazelbuild / bazelbuild/rules_rust

`rust_library` cannot find `compile_data` if `srcs` contains generated code

Open
#2,269 3 comments 1 reaction 0 assignees View on GitHub
bug needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

`include_str` and `include_bytes` don't work if some of the codes are generated, even if the `include` macros are called from codes that are not generated.

I have a reproducible example [github.com/fardream/rules-rust-bug](https://github.com/fardream/rules-rust-bug/)

For example, if I have `lib.rs` as below, where `test.txt` is a file in the same folder as `lib.rs`,

```rust
pub const BYTES: &'static str = include_str!("test.txt");
```

The below will compile

```Starlark
rust_library(
name = "include_str",
srcs = ["lib.rs"],
compile_data = ["test.txt"],
)
```

However, compile will not work if some of the `srcs` are generated.

If I use below rule to write `lib2.rs`

```Starlark
def _write_rs(ctx):
output = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = output,
content = "pub const BYTES: &'static str = \"astring\";",
)
return [DefaultInfo(files = depset([output]))]

write_rs = rule(implementation = _write_rs)
```

Below will not work

```Starlark
rust_library(
name = "include_str_with_gen_code",
srcs = [
"lib.rs",
"lib2.rs",
],
compile_data = ["test.txt"],
)
```

And the error is compiler cannot find `text.txt`, even though it is there.

```shell
error: couldn't read bazel-out/darwin_arm64-fastbuild/bin/test.txt: No such file or directory (os error 2)
--> bazel-out/darwin_arm64-fastbuild/bin/lib.rs:1:33
|
1 | pub const BYTES: &'static str = include_str!("test.txt");
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Target //:include_str_with_gen_code failed to build
Use --verbose_failures to see the command lines of failed build steps.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.