diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 88b98d1e60..d6f09e855e 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -251,15 +251,16 @@ def _rust_library_common(ctx, crate_type): Returns: list: A list of providers. See `rustc_compile_action` """ - - srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None)) - if not crate_root: - crate_root = crate_root_src(ctx.attr.name, srcs, "lib") _assert_no_deprecated_attributes(ctx) _assert_correct_dep_mapping(ctx) toolchain = find_toolchain(ctx) + crate_root = getattr(ctx.file, "crate_root", None) + if not crate_root: + crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_type) + srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, crate_root) + # Determine unique hash for this rlib. # Note that we don't include a hash for `cdylib` and `staticlib` since they are meant to be consumed externally # and having a deterministic name is important since it ends up embedded in the executable. This is problematic @@ -332,9 +333,10 @@ def _rust_binary_impl(ctx): deps = transform_deps(ctx.attr.deps) proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx)) - srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None)) + crate_root = getattr(ctx.file, "crate_root", None) if not crate_root: - crate_root = crate_root_src(ctx.attr.name, srcs, ctx.attr.crate_type) + crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, ctx.attr.crate_type) + srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, crate_root) return rustc_compile_action( ctx = ctx, @@ -373,9 +375,7 @@ def _rust_test_impl(ctx): toolchain = find_toolchain(ctx) - srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None)) crate_type = "bin" - deps = transform_deps(ctx.attr.deps) proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx)) @@ -392,6 +392,8 @@ def _rust_test_impl(ctx): ), ) + srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None)) + # Optionally join compile data if crate.compile_data: compile_data = depset(ctx.files.compile_data, transitive = [crate.compile_data]) @@ -425,9 +427,12 @@ def _rust_test_impl(ctx): owner = ctx.label, ) else: + crate_root = getattr(ctx.file, "crate_root", None) + if not crate_root: crate_root_type = "lib" if ctx.attr.use_libtest_harness else "bin" crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type) + srcs, crate_root = _transform_sources(ctx, ctx.files.srcs, crate_root) output_hash = determine_output_hash(crate_root, ctx.label) output = ctx.actions.declare_file( diff --git a/test/generated_inputs/BUILD.bazel b/test/generated_inputs/BUILD.bazel index 19fc8e6f66..646f019394 100644 --- a/test/generated_inputs/BUILD.bazel +++ b/test/generated_inputs/BUILD.bazel @@ -84,6 +84,17 @@ rust_library( tags = ["norustfmt"], ) +rust_test( + name = "use_generated_src_with_crate_root_in_subdir_srcs_test", + srcs = [ + "src/generated.rs", + "src/generated/submodule.rs", + "src/lib.rs", + ], + edition = "2018", + tags = ["norustfmt"], +) + rust_test( name = "use_generated_src_with_crate_root_in_subdir_test", crate = "use_generated_src_with_crate_root_in_subdir",