Propagate `cargo_build_script.data` to `Rustc` compile actions (#2856)
This way any outputs that the build script may refer to in flags it
emits can be referenced without needing to also explicitly pass the data
target to the `compile_data` attribute of it's consumer.
diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl
index 6c89b04..56f6499 100644
--- a/cargo/private/cargo_build_script.bzl
+++ b/cargo/private/cargo_build_script.bzl
@@ -433,7 +433,7 @@
flags = flags_out,
linker_flags = link_flags,
link_search_paths = link_search_paths,
- compile_data = depset(),
+ compile_data = depset(transitive = script_data),
),
OutputGroupInfo(
streams = depset([streams.stdout, streams.stderr]),
diff --git a/test/cargo_build_script/compile_data/BUILD.bazel b/test/cargo_build_script/compile_data/BUILD.bazel
new file mode 100644
index 0000000..d8aafa5
--- /dev/null
+++ b/test/cargo_build_script/compile_data/BUILD.bazel
@@ -0,0 +1,16 @@
+load("//cargo:defs.bzl", "cargo_build_script")
+load("//rust:defs.bzl", "rust_test")
+
+cargo_build_script(
+ name = "build_rs",
+ srcs = ["build.rs"],
+ data = ["data.txt"],
+ edition = "2018",
+)
+
+rust_test(
+ name = "test",
+ srcs = ["test.rs"],
+ edition = "2018",
+ deps = [":build_rs"],
+)
diff --git a/test/cargo_build_script/compile_data/build.rs b/test/cargo_build_script/compile_data/build.rs
new file mode 100644
index 0000000..ccab891
--- /dev/null
+++ b/test/cargo_build_script/compile_data/build.rs
@@ -0,0 +1,9 @@
+use std::path::PathBuf;
+
+fn main() {
+ let path = "data.txt";
+ if !PathBuf::from(path).exists() {
+ panic!("File does not exist in path.");
+ }
+ println!("cargo:rustc-env=DATA={}", path);
+}
diff --git a/test/cargo_build_script/compile_data/data.txt b/test/cargo_build_script/compile_data/data.txt
new file mode 100644
index 0000000..f58d689
--- /dev/null
+++ b/test/cargo_build_script/compile_data/data.txt
@@ -0,0 +1 @@
+La-Li-Lu-Le-Lo
diff --git a/test/cargo_build_script/compile_data/test.rs b/test/cargo_build_script/compile_data/test.rs
new file mode 100644
index 0000000..8837e32
--- /dev/null
+++ b/test/cargo_build_script/compile_data/test.rs
@@ -0,0 +1,6 @@
+#[test]
+pub fn test_compile_data() {
+ let data = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", env!("DATA")));
+
+ assert_eq!("La-Li-Lu-Le-Lo\n", data);
+}