@@ -429,10 +429,16 @@ def _rust_toolchain_impl(ctx):
429429 sysroot_path = sysroot .sysroot_anchor .dirname
430430 sysroot_short_path , _ , _ = sysroot .sysroot_anchor .short_path .rpartition ("/" )
431431
432+ # When system_sysroot is set, override paths to use the platform-installed toolchain.
433+ # This must happen before make_variables is constructed so all consumers see system paths.
434+ if ctx .attr .system_sysroot :
435+ sysroot_path = ctx .attr .system_sysroot
436+ sysroot_short_path = ctx .attr .system_sysroot
437+
432438 # Variables for make variable expansion
433439 make_variables = {
434- "RUSTC" : sysroot .rustc .path ,
435- "RUSTDOC" : sysroot .rustdoc .path ,
440+ "RUSTC" : ( ctx . attr . system_sysroot + "/bin/rustc" ) if ctx . attr . system_sysroot else sysroot .rustc .path ,
441+ "RUSTDOC" : ( ctx . attr . system_sysroot + "/bin/rustdoc" ) if ctx . attr . system_sysroot else sysroot .rustdoc .path ,
436442 "RUST_DEFAULT_EDITION" : ctx .attr .default_edition or "" ,
437443 "RUST_SYSROOT" : sysroot_path ,
438444 "RUST_SYSROOT_SHORT" : sysroot_short_path ,
@@ -563,9 +569,21 @@ def _rust_toolchain_impl(ctx):
563569 )
564570
565571 # Include C++ toolchain files to ensure tools like 'ar' are available for cross-compilation
566- all_files_depsets = [sysroot .all_files ]
567- if cc_toolchain and cc_toolchain .all_files :
568- all_files_depsets .append (cc_toolchain .all_files )
572+ if ctx .attr .system_sysroot :
573+ # When system_sysroot is set, exclude Rust toolchain files from action inputs.
574+ # The execution environment (local or remote) has the toolchain installed at this path.
575+ # This eliminates ~670MB of toolchain file transfers per remote action.
576+ all_files_depsets = []
577+ if cc_toolchain and cc_toolchain .all_files :
578+ all_files_depsets .append (cc_toolchain .all_files )
579+
580+ # Keep linker as input (it's custom and tiny)
581+ if sysroot .linker :
582+ all_files_depsets .append (depset ([sysroot .linker ]))
583+ else :
584+ all_files_depsets = [sysroot .all_files ]
585+ if cc_toolchain and cc_toolchain .all_files :
586+ all_files_depsets .append (cc_toolchain .all_files )
569587
570588 toolchain = platform_common .ToolchainInfo (
571589 all_files = depset (transitive = all_files_depsets ),
@@ -605,6 +623,8 @@ def _rust_toolchain_impl(ctx):
605623 per_crate_rustc_flags = ctx .attr .per_crate_rustc_flags ,
606624 sysroot = sysroot_path ,
607625 sysroot_short_path = sysroot_short_path ,
626+ system_rustc_path = ctx .attr .system_sysroot + "/bin/rustc" if ctx .attr .system_sysroot else "" ,
627+ system_sysroot = ctx .attr .system_sysroot ,
608628 target_arch = target_arch ,
609629 target_flag_value = target_json .path if target_json else target_triple .str ,
610630 target_json = target_json ,
@@ -831,6 +851,17 @@ rust_toolchain = rule(
831851 "opt" : "debuginfo" ,
832852 },
833853 ),
854+ "system_sysroot" : attr .string (
855+ doc = (
856+ "When set, use the platform-provided Rust toolchain at this absolute path " +
857+ "instead of shipping toolchain files as action inputs. This eliminates large " +
858+ "toolchain file transfers per action when using remote execution with " +
859+ "workers that have the Rust toolchain pre-installed. The path should point " +
860+ "to a rustup toolchain directory (e.g. " +
861+ "/home/user/.rustup/toolchains/nightly-2026-01-29-x86_64-unknown-linux-gnu)."
862+ ),
863+ default = "" ,
864+ ),
834865 "target_json" : attr .string (
835866 doc = ("Override the target_triple with a custom target specification. " +
836867 "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html" ),
0 commit comments