Add regression test for compile data in alternate packages (#3193)
diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl index 286cce9..abb4562 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl
@@ -865,7 +865,8 @@ File: The created symlink if a non-generated file, or the file itself. """ - if src_file.is_source or src_file.root.path != ctx.bin_dir.path: + src_short_path = paths.relativize(src_file.path, src_file.root.path) + if (src_file.is_source or src_file.root.path != ctx.bin_dir.path) and paths.starts_with(src_short_path, package_root): src_short_path = paths.relativize(src_file.path, src_file.root.path) src_symlink = ctx.actions.declare_file(paths.relativize(src_short_path, package_root)) ctx.actions.symlink(
diff --git a/test/unit/compile_data/data/BUILD.bazel b/test/unit/compile_data/data/BUILD.bazel new file mode 100644 index 0000000..9b5135c --- /dev/null +++ b/test/unit/compile_data/data/BUILD.bazel
@@ -0,0 +1,5 @@ +alias( + name = "data", + actual = "data.txt", + visibility = ["//test/unit/compile_data/src:__pkg__"], +)
diff --git a/test/unit/compile_data/data/data.txt b/test/unit/compile_data/data/data.txt new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/test/unit/compile_data/data/data.txt
@@ -0,0 +1 @@ +Hello World
diff --git a/test/unit/compile_data/src/BUILD.bazel b/test/unit/compile_data/src/BUILD.bazel new file mode 100644 index 0000000..af34b60 --- /dev/null +++ b/test/unit/compile_data/src/BUILD.bazel
@@ -0,0 +1,35 @@ +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("//rust:defs.bzl", "rust_library", "rust_test") + +write_file( + name = "lib_rs", + out = "lib.rs", + content = """\ +pub const DATA: &str = include_str!(env!(\"COMPILE_DATA\")); + +#[cfg(test)] +mod test { + #[test] + fn test_data() { + assert_eq!(super::DATA.trim(), "Hello World"); + } +} +""".splitlines(), + newline = "unix", +) + +rust_library( + name = "lib", + srcs = [":lib.rs"], + compile_data = ["//test/unit/compile_data/data"], + edition = "2021", + rustc_env = { + "COMPILE_DATA": "$(execpath //test/unit/compile_data/data)", + }, +) + +rust_test( + name = "test", + crate = ":lib", + edition = "2021", +)