Skip to content

Commit 85d712c

Browse files
authored
Rollup merge of #72296 - ChrisDenton:msvc-link-check, r=petrochenkov
Suggest installing VS Build Tools in more situations When MSVC's `link.exe` wasn't found but another `link.exe` was, the error message given can be [impenetrable](https://pastebin.com/MRMCr7HM) to many users. The usual suspect is GNU's `link` tool. In this case, inform the user that they may need to install VS build tools. This only applies when Microsoft's link tool is expected.
2 parents e279bd5 + 2fd504c commit 85d712c

File tree

1 file changed

+49
-0
lines changed
  • src/librustc_codegen_ssa/back

1 file changed

+49
-0
lines changed

src/librustc_codegen_ssa/back/link.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,55 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
611611
.note(&format!("{:?}", &cmd))
612612
.note(&escape_string(&output))
613613
.emit();
614+
615+
// If MSVC's `link.exe` was expected but the return code
616+
// is not a Microsoft LNK error then suggest a way to fix or
617+
// install the Visual Studio build tools.
618+
if let Some(code) = prog.status.code() {
619+
if sess.target.target.options.is_like_msvc
620+
&& flavor == LinkerFlavor::Msvc
621+
// Respect the command line override
622+
&& sess.opts.cg.linker.is_none()
623+
// Match exactly "link.exe"
624+
&& linker_path.to_str() == Some("link.exe")
625+
// All Microsoft `link.exe` linking error codes are
626+
// four digit numbers in the range 1000 to 9999 inclusive
627+
&& (code < 1000 || code > 9999)
628+
{
629+
let is_vs_installed = windows_registry::find_vs_version().is_ok();
630+
let has_linker = windows_registry::find_tool(
631+
&sess.opts.target_triple.triple(),
632+
"link.exe",
633+
)
634+
.is_some();
635+
636+
sess.note_without_error("`link.exe` returned an unexpected error");
637+
if is_vs_installed && has_linker {
638+
// the linker is broken
639+
sess.note_without_error(
640+
"the Visual Studio build tools may need to be repaired \
641+
using the Visual Studio installer",
642+
);
643+
sess.note_without_error(
644+
"or a necessary component may be missing from the \
645+
\"C++ build tools\" workload",
646+
);
647+
} else if is_vs_installed {
648+
// the linker is not installed
649+
sess.note_without_error(
650+
"in the Visual Studio installer, ensure the \
651+
\"C++ build tools\" workload is selected",
652+
);
653+
} else {
654+
// visual studio is not installed
655+
sess.note_without_error(
656+
"you may need to install Visual Studio build tools with the \
657+
\"C++ build tools\" workload",
658+
);
659+
}
660+
}
661+
}
662+
614663
sess.abort_if_errors();
615664
}
616665
info!("linker stderr:\n{}", escape_string(&prog.stderr));

0 commit comments

Comments
 (0)