Skip to content

Commit

Permalink
Fix build failure finding crate_roots when mixed with generated sourc…
Browse files Browse the repository at this point in the history
…es (#2041)
  • Loading branch information
UebelAndre committed Jul 5, 2023
1 parent 7f751cd commit cd126be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
21 changes: 13 additions & 8 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))

Expand All @@ -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])
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions test/generated_inputs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cd126be

Please sign in to comment.