bazelbuild / bazelbuild/rules_rust
Runfile has some assumption about __main__ / using `rust_binary`?
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
I struggled to figure out how to use `data` in `rust_test` in docs and end up finding the comment block of https://github.com/bazelbuild/rules_rust/blob/0ed7157402aacd2c8b5310d8348ec792826da4d4/tools/runfiles/runfiles.rs#L1-L32.
I just want to load some test data in `rust_test` and everything I've seen suggest usage in `rust_binary`, so my setup isn't working:
```python
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
rust_library(
name = "txt",
srcs = [
"src/lib.rs",
],
edition = "2021",
)
rust_test(
name = "txt_test",
crate = ":txt",
data = ["testdata/items.txt"],
deps = ["@rules_rust//tools/runfiles"],
)
```
when trying to read the file as:
```rust
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let r = runfiles::Runfiles::create().unwrap();
let path = r.rlocation("path/to/testdata/items.txt");
// Try to open the file.
}
}
```
This will fail for file not found, so I just went looking myself on if the data file was built under `bazel-bin/.../test-845656100/txt_test.runfiles/__main__/path/to/testdata/items.txt`. This is puzzling, and I can "fix" the issue by doing:
```rust
let path = r.rlocation("__main__/path/to/testdata/items.txt");
```
I would expect my use case to just work without having to hack the `__main__` part.
Contributor guide
Assessment
This issue has not been assessed yet.