Skip to content

Include compile_data targets in rustc_env location expansion for rust_test - #4000

Merged
UebelAndre merged 3 commits into
bazelbuild:mainfrom
tamasvajk:compile-data-in-data-paths
Apr 28, 2026
Merged

Include compile_data targets in rustc_env location expansion for rust_test#4000
UebelAndre merged 3 commits into
bazelbuild:mainfrom
tamasvajk:compile-data-in-data-paths

Conversation

@tamasvajk

Copy link
Copy Markdown
Contributor

Summary

Include compile_data targets in rustc_env location expansion for rust_test.

Problem

In rust_test, $(location) and $(rootpath) expansions in rustc_env only search data targets. When a compile_data target is referenced in rustc_env — for example to pass the path of a generated file via env!() and include_str!() — the expansion fails because compile_data targets are not in the search set.

This affects both rust_test code paths: the crate-wrapping path (testing an existing rust_library) and the standalone path (test with its own sources).

Fix

Add compile_data targets to the data_paths depset passed to expand_dict_value_locations in both rust_test code paths.


Note: This PR was largely AI-generated using Claude Code, with human review and guidance throughout.

@tamasvajk
tamasvajk force-pushed the compile-data-in-data-paths branch 2 times, most recently from e1b4935 to 2862fe0 Compare April 28, 2026 14:31

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you add a test for this somewhere in //test/unit?

In rust_test, $(location) and $(rootpath) expansions in rustc_env
only searched data targets. When a compile_data target was referenced
in rustc_env (e.g. include_str!(env!("PATH_TO_GENERATED_FILE"))),
the expansion failed because compile_data wasn't in the search set.

Add compile_data targets to the data_paths depset used by
expand_dict_value_locations in both the crate-wrapping and standalone
rust_test code paths.
@tamasvajk
tamasvajk force-pushed the compile-data-in-data-paths branch from 2862fe0 to 5481876 Compare April 28, 2026 15:21
@tamasvajk
tamasvajk requested a review from UebelAndre April 28, 2026 15:22

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like CI is failing

(15:23:36) ERROR: /workdir/test/unit/compile_data_env/BUILD.bazel:4:28: Compiling Rust bin crate_wrap_test (1 file) failed: (Exit 1): process_wrapper failed: error executing Rustc command (from rust_test rule target //test/unit/compile_data_env:crate_wrap_test)
  (cd /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/sandbox/linux-sandbox/737/execroot/_main && \
  exec env - \
    CARGO_CFG_TARGET_ARCH=x86_64 \
    CARGO_CFG_TARGET_OS=linux \
    CARGO_CRATE_NAME=mylib \
    CARGO_MANIFEST_DIR='${pwd}/test/unit/compile_data_env' \
    CARGO_PKG_AUTHORS='' \
    CARGO_PKG_DESCRIPTION='' \
    CARGO_PKG_HOMEPAGE='' \
    CARGO_PKG_NAME=mylib \
    CARGO_PKG_VERSION=0.0.0 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=0 \
    CARGO_PKG_VERSION_PATCH=0 \
    CARGO_PKG_VERSION_PRE='' \
    GENERATED_PATH='${pwd}/bazel-out/k8-fastbuild/bin/test/unit/compile_data_env/generated.txt' \
    PATH=/bin:/usr/bin:/usr/local/bin \
    REPOSITORY_NAME='' \
    ZERO_AR_DATE=1 \
  bazel-out/k8-opt-exec/bin/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' --subst 'exec_root=${exec_root}' --subst 'output_base=${output_base}' -- bazel-out/k8-fastbuild/bin/external/+rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/bin/rustc test/unit/compile_data_env/lib.rs '--crate-name=mylib' '--crate-type=bin' '--error-format=human' '--codegen=metadata=-3204932321' '--codegen=extra-filename=-3204932321' '--out-dir=bazel-out/k8-fastbuild/bin/test/unit/compile_data_env' '--codegen=opt-level=0' '--codegen=debuginfo=0' '--codegen=strip=none' '--remap-path-prefix=${pwd}=.' '--remap-path-prefix=${exec_root}=.' '--remap-path-prefix=${output_base}=.' '--emit=link=bazel-out/k8-fastbuild/bin/test/unit/compile_data_env/crate_wrap_test' '--emit=dep-info' '--color=always' '--target=x86_64-unknown-linux-gnu' -L bazel-out/k8-fastbuild/bin/external/+rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib --test '--edition=2021' '-Cembed-bitcode=no' '--codegen=linker=/usr/bin/gcc' '--codegen=link-arg=-fuse-ld=gold' '--codegen=link-arg=-B/usr/bin' '--codegen=link-arg=-Wl,-no-as-needed' '--codegen=link-arg=-Wl,-z,relro,-z,now' '--codegen=link-arg=-pass-exit-codes' '--codegen=link-arg=-Wl,--push-state,-as-needed' '--codegen=link-arg=-lstdc++' '--codegen=link-arg=-Wl,--pop-state' '--codegen=link-arg=-Wl,--push-state,-as-needed' '--codegen=link-arg=-lm' '--codegen=link-arg=-Wl,--pop-state' '--sysroot=bazel-out/k8-fastbuild/bin/external/+rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain')
# Configuration: bbf8a40a9f44b6c42caef393bd2f86fe4f730742c9380170decb994f975e91a7
# Execution platform: @@platforms//host:host
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error: couldn't read `test/unit/compile_data_env/lib.rs`: No such file or directory (os error 2)
error: aborting due to 1 previous error

Tamas Vajk added 2 commits April 28, 2026 15:39
The rust_test(crate=..., compile_data=[generated]) combination
causes a build error (source file not found after transform_sources).
Keep only the standalone test which covers the fix.
Use a non-generated file in compile_data for the crate-wrap test to
avoid a pre-existing bug where rust_test(crate=...) with generated
compile_data fails to build (transform_sources doesn't update
crate.root). The test still verifies that compile_data targets are
included in the rustc_env location expansion.

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@UebelAndre
UebelAndre added this pull request to the merge queue Apr 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 28, 2026
@UebelAndre
UebelAndre added this pull request to the merge queue Apr 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 28, 2026
@UebelAndre
UebelAndre added this pull request to the merge queue Apr 28, 2026
Merged via the queue into bazelbuild:main with commit 2f07767 Apr 28, 2026
3 checks passed
dzbarsky pushed a commit to hermeticbuild/rules_rust that referenced this pull request Apr 30, 2026
…`rust_test` (bazelbuild#4000)

## Summary

Include `compile_data` targets in `rustc_env` location expansion for
`rust_test`.

## Problem

In `rust_test`, `$(location)` and `$(rootpath)` expansions in
`rustc_env` only search `data` targets. When a `compile_data` target is
referenced in `rustc_env` — for example to pass the path of a generated
file via `env!()` and `include_str!()` — the expansion fails because
`compile_data` targets are not in the search set.

This affects both `rust_test` code paths: the crate-wrapping path
(testing an existing `rust_library`) and the standalone path (test with
its own sources).

## Fix

Add `compile_data` targets to the `data_paths` depset passed to
`expand_dict_value_locations` in both `rust_test` code paths.

---

> **Note:** This PR was largely AI-generated using Claude Code, with
human review and guidance throughout.

---------

Co-authored-by: Tamas Vajk <tamas.vajk@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants