Skip to content

Do not print linker command in linker error by default #133871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ fn link_natively(
exit_status: prog.status,
command: &cmd,
escaped_output,
verbose: sess.opts.verbose,
};
sess.dcx().emit_err(err);
// If MSVC's `link.exe` was expected but the return code
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub(crate) struct LinkingFailed<'a> {
pub exit_status: ExitStatus,
pub command: &'a Command,
pub escaped_output: String,
pub verbose: bool,
}

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

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

diag.note(format!("{:?}", self.command)).note(self.escaped_output);
let command = format!("{:?}", self.command);
let width = 100;
match (self.verbose, command.len() > width) {
(false, true) => {
diag.note("to see the full command that was run, rerun with `--verbose` flag");
}
(true, _) | (_, false) => {
diag.note(command);
}
}
diag.note(format!("output:\n{}", self.escaped_output));

// Trying to match an error from OS linkers
// which by now we have no way to translate.
Expand Down
5 changes: 5 additions & 0 deletions src/tools/run-make-support/src/external_deps/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ impl Rustc {
self
}

pub fn verbose(&mut self) -> &mut Self {
self.cmd.arg("--verbose");
self
}

/// Specify json messages printed by the compiler
pub fn json(&mut self, items: &str) -> &mut Self {
self.cmd.arg(format!("--json={items}"));
Expand Down
8 changes: 6 additions & 2 deletions tests/run-make/linkage-attr-framework/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//@ only-apple

use run_make_support::{Rustc, run, rustc};
use run_make_support::{Rustc, diff, run, rustc};

fn compile(cfg: &str) -> Rustc {
let mut rustc = rustc();
Expand All @@ -16,11 +16,15 @@ fn main() {
run("main");
}

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