diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 6b8729d62ae32..59ff0f34aba86 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -3117,16 +3117,12 @@ fn add_lld_args( let self_contained_linker = self_contained_cli || self_contained_target; if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() { // We tell `cc` where to find the lld wrappers, in a similar way to how we locate - // codegen-backends dylibs: - // - in the tool search paths: the sysroot's `rustlib` bin path, - // - and as a fallback for cases where `--sysroot` only contains a target std: in the - // default host sysroot where rustc is currently located. - let fallback_sysroot_paths = filesearch::sysroot_candidates() - .into_iter() - .map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_triple())); - + // codegen-backends dylibs, in the tools search paths, which contain: + // - the sysroot's `rustlib` bin path, + // - and as a fallback for cases using `--sysroot` without tools: in the default host + // sysroot where rustc is currently located. let mut linker_path_exists = false; - for path in sess.get_tools_search_paths(false).into_iter().chain(fallback_sysroot_paths) { + for path in sess.get_tools_search_paths(false) { let linker_path = path.join("gcc-ld"); if linker_path.exists() { linker_path_exists = true; diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index db01bb90d6fac..1b55fb1595626 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -449,7 +449,9 @@ impl Session { ) } - /// Returns a list of directories where target-specific tool binaries are located. + /// Returns a list of directories where target-specific tool binaries are located. Some fallback + /// directories are also returned, for example if `--sysroot` is used but tools are missing + /// (#125246): we also add the bin directories to the sysroot where rustc is located. pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec { let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, config::host_triple()); let p = PathBuf::from_iter([ @@ -457,7 +459,19 @@ impl Session { Path::new(&rustlib_path), Path::new("bin"), ]); - if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] } + + let fallback_sysroot_paths = filesearch::sysroot_candidates() + .into_iter() + .map(|sysroot| filesearch::make_target_bin_path(&sysroot, config::host_triple())); + + if self_contained { + [p.clone(), p.join("self-contained")] + .into_iter() + .chain(fallback_sysroot_paths) + .collect() + } else { + std::iter::once(p).chain(fallback_sysroot_paths).collect() + } } pub fn init_incr_comp_session(&self, session_dir: PathBuf, lock_file: flock::Lock) {