Skip to content

Commit 5dce253

Browse files
committed
Do not print linker command in linker error by default
Only print the entire linker command that failed if `--verbose` is set. CC rust-lang#110763. Fix rust-lang#109979.
1 parent 96e51d9 commit 5dce253

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ fn link_natively(
10041004
exit_status: prog.status,
10051005
command: &cmd,
10061006
escaped_output,
1007+
verbose: sess.opts.verbose,
10071008
};
10081009
sess.dcx().emit_err(err);
10091010
// If MSVC's `link.exe` was expected but the return code

compiler/rustc_codegen_ssa/src/errors.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub(crate) struct LinkingFailed<'a> {
349349
pub exit_status: ExitStatus,
350350
pub command: &'a Command,
351351
pub escaped_output: String,
352+
pub verbose: bool,
352353
}
353354

354355
impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
@@ -359,7 +360,17 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
359360

360361
let contains_undefined_ref = self.escaped_output.contains("undefined reference to");
361362

362-
diag.note(format!("{:?}", self.command)).note(self.escaped_output);
363+
let command = format!("{:?}", self.command);
364+
let width = 100;
365+
match (self.verbose, command.len() > width) {
366+
(false, true) => {
367+
diag.note("to see the full command that was run, rerun with `--verbose` flag");
368+
}
369+
(true, _) | (_, false) => {
370+
diag.note(command);
371+
}
372+
}
373+
diag.note(format!("output:\n{}", self.escaped_output));
363374

364375
// Trying to match an error from OS linkers
365376
// which by now we have no way to translate.

src/tools/run-make-support/src/external_deps/rustc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ impl Rustc {
221221
self
222222
}
223223

224+
pub fn verbose(&mut self) -> &mut Self {
225+
self.cmd.arg("--verbose");
226+
self
227+
}
228+
224229
/// Specify json messages printed by the compiler
225230
pub fn json(&mut self, items: &str) -> &mut Self {
226231
self.cmd.arg(format!("--json={items}"));

tests/run-make/linkage-attr-framework/rmake.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
//@ only-apple
44

5-
use run_make_support::{Rustc, run, rustc};
5+
use run_make_support::{Rustc, diff, run, rustc};
66

77
fn compile(cfg: &str) -> Rustc {
88
let mut rustc = rustc();
@@ -16,11 +16,15 @@ fn main() {
1616
run("main");
1717
}
1818

19-
let errs = compile("omit").run_fail();
19+
let errs = compile("omit").verbose().run_fail();
2020
// The linker's exact error output changes between Xcode versions, depends on
2121
// linker invocation details, and the linker sometimes outputs more warnings.
2222
errs.assert_stderr_contains_regex(r"error: linking with `.*` failed");
2323
errs.assert_stderr_contains_regex(r"(Undefined symbols|ld: symbol[^\s]* not found)");
2424
errs.assert_stderr_contains_regex(r".?_CFRunLoopGetTypeID.?, referenced from:");
2525
errs.assert_stderr_contains("clang: error: linker command failed with exit code 1");
26+
// Ensure we don't show the full linker command by default.
27+
let errs = compile("omit").run_fail().assert_stderr_contains(
28+
"to see the full command that was run, rerun with `--verbose` flag",
29+
);
2630
}

0 commit comments

Comments
 (0)