Skip to content

Commit 2fd504c

Browse files
committed
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 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. Not `lld-link` or other MSVC compatible linkers.
1 parent 7faeae0 commit 2fd504c

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

0 commit comments

Comments
 (0)