1616
1717load ("@bazel_skylib//lib:paths.bzl" , "paths" )
1818load ("//rust/private:common.bzl" , "rust_common" )
19- load ("//rust/private:rustc.bzl" , "rustc_compile_action" )
19+ load ("//rust/private:rustc.bzl" , "OutputDiagnosticsInfo" , " rustc_compile_action" )
2020load (
2121 "//rust/private:utils.bzl" ,
2222 "can_build_metadata" ,
@@ -65,6 +65,32 @@ def _assert_correct_dep_mapping(ctx):
6565 ),
6666 )
6767
68+ def _generate_output_diagnostics (ctx , sibling , require_process_wrapper = True ):
69+ """Generates a .rustc-output file if it's required.
70+
71+ Args:
72+ ctx: (ctx): The current rule's context object
73+ sibling: (File): The file to generate the diagnostics for.
74+ require_process_wrapper: (bool): Whether to require the process wrapper
75+ in order to generate the .rustc-output file.
76+ Returns:
77+ Optional[File] The .rustc-object file, if generated.
78+ """
79+
80+ # Since this feature requires error_format=json, we usually need
81+ # process_wrapper, since it can write the json here, then convert it to the
82+ # regular error format so the user can see the error properly.
83+ if require_process_wrapper and not ctx .attr ._process_wrapper :
84+ return
85+ provider = ctx .attr ._output_diagnostics [OutputDiagnosticsInfo ]
86+ if not provider .output_diagnostics :
87+ return
88+
89+ return ctx .actions .declare_file (
90+ sibling .basename + ".rustc-output" ,
91+ sibling = sibling ,
92+ )
93+
6894def _determine_lib_name (name , crate_type , toolchain , lib_hash = None ):
6995 """See https://github.com/bazelbuild/rules_rust/issues/405
7096
@@ -279,11 +305,13 @@ def _rust_library_common(ctx, crate_type):
279305 rust_lib = ctx .actions .declare_file (rust_lib_name )
280306
281307 rust_metadata = None
308+ rustc_rmeta_output = None
282309 if can_build_metadata (toolchain , ctx , crate_type ) and not ctx .attr .disable_pipelining :
283310 rust_metadata = ctx .actions .declare_file (
284311 paths .replace_extension (rust_lib_name , ".rmeta" ),
285312 sibling = rust_lib ,
286313 )
314+ rustc_rmeta_output = _generate_output_diagnostics (ctx , rust_metadata )
287315
288316 deps = transform_deps (ctx .attr .deps )
289317 proc_macro_deps = transform_deps (ctx .attr .proc_macro_deps + get_import_macro_deps (ctx ))
@@ -301,7 +329,9 @@ def _rust_library_common(ctx, crate_type):
301329 proc_macro_deps = depset (proc_macro_deps ),
302330 aliases = ctx .attr .aliases ,
303331 output = rust_lib ,
332+ rustc_output = _generate_output_diagnostics (ctx , rust_lib ),
304333 metadata = rust_metadata ,
334+ rustc_rmeta_output = rustc_rmeta_output ,
305335 edition = get_edition (ctx .attr , toolchain , ctx .label ),
306336 rustc_env = ctx .attr .rustc_env ,
307337 rustc_env_files = ctx .files .rustc_env_files ,
@@ -348,6 +378,7 @@ def _rust_binary_impl(ctx):
348378 proc_macro_deps = depset (proc_macro_deps ),
349379 aliases = ctx .attr .aliases ,
350380 output = output ,
381+ rustc_output = _generate_output_diagnostics (ctx , output ),
351382 edition = get_edition (ctx .attr , toolchain , ctx .label ),
352383 rustc_env = ctx .attr .rustc_env ,
353384 rustc_env_files = ctx .files .rustc_env_files ,
@@ -414,6 +445,7 @@ def _rust_test_impl(ctx):
414445 proc_macro_deps = depset (proc_macro_deps , transitive = [crate .proc_macro_deps ]),
415446 aliases = ctx .attr .aliases ,
416447 output = output ,
448+ rustc_output = _generate_output_diagnostics (ctx , output ),
417449 edition = crate .edition ,
418450 rustc_env = rustc_env ,
419451 rustc_env_files = rustc_env_files ,
@@ -447,6 +479,7 @@ def _rust_test_impl(ctx):
447479 proc_macro_deps = depset (proc_macro_deps ),
448480 aliases = ctx .attr .aliases ,
449481 output = output ,
482+ rustc_output = _generate_output_diagnostics (ctx , output ),
450483 edition = get_edition (ctx .attr , toolchain , ctx .label ),
451484 rustc_env = ctx .attr .rustc_env ,
452485 rustc_env_files = ctx .files .rustc_env_files ,
@@ -682,6 +715,9 @@ _common_attrs = {
682715 "_is_proc_macro_dep_enabled" : attr .label (
683716 default = Label ("//:is_proc_macro_dep_enabled" ),
684717 ),
718+ "_output_diagnostics" : attr .label (
719+ default = Label ("//:output_diagnostics" ),
720+ ),
685721 "_per_crate_rustc_flag" : attr .label (
686722 default = Label ("//:experimental_per_crate_rustc_flag" ),
687723 ),
0 commit comments