Skip to content

feat(diagnostics): add colorized diagnostics to the native engine #144

Closed

Description

@zackees

Goal

Give reld's native (wild-derived) engine properly colorized diagnostics, matching lld's conventions. Color handling goes through one helper that behaves correctly on each platform (Windows vs everything else) and is controlled by --color-diagnostics[=always|never|auto] / --no-color-diagnostics.

Current state (main @ fcecfdb)

  • Parsing: the flags are parsed (crates/reld-core/src/args/elf.rs:1102-1120) and only flip colored's global override.
  • Colored output today: only two diagnostics are colored, and neither matches lld:
    • error.rs:208 prints reld: \e[31merror\e[0m: …. The colon is outside the colored span, and the Caused by: chain is plain.
    • error.rs:108 prints warning: in yellow; lld uses magenta.
  • Always plain: bridge.rs:406, bridge.rs:1097 (engine routing log), bridge.rs:1121 (bridge killed by signal) and crates/reld/src/main.rs:29 (DHAT dump failure).
  • Auto mode checks the wrong stream. colored enables color when stdout is a terminal, but reld writes diagnostics to stderr. Reproduced both ways:
    • reld missing.o 2>err.log from a terminal writes escape codes into err.log.
    • With stderr on a terminal and stdout redirected, reld prints no color.
  • Windows: nothing switches on ANSI rendering in the console. colored::control::set_virtual_terminal wouldn't fix it: in colored 3.1.1 it targets the stdout handle, ignores API errors, and always returns Ok.
  • Early errors: diagnostics emitted before the flag is parsed ignore it.
  • Tests: nothing covers color.

Reference output, captured locally:

ld.lld: ^[[0;31merror: ^[[0mcannot open nonexistent.o: No such file or directory
ld.lld: ^[[0;35mwarning: ^[[0munknown -z value: unknownzopt
reld: ^[[31merror^[[0m: Failed to open input file `nonexistent.o`

Design

1. crates/reld-core/src/diagnostic.rs: the colorization helper (same on every OS)

pub enum ColorChoice { Always, Never, Auto }
pub enum Severity { Error, Warning, Note }

pub fn set_color_choice(choice: ColorChoice); // last flag wins
pub fn render(severity: Severity, message: &str) -> String;
pub fn emit(severity: Severity, message: impl std::fmt::Display);
  • Deciding once: the on/off result is computed once and cached. It is also mirrored into colored::control::set_override, so existing colored output (sym-info in diagnostics.rs) follows the same decision.
  • The decision is a pure function that tests can check on any OS: should_color(choice, env, stderr_terminal). The first matching rule wins:
    1. an explicit always/never flag;
    2. CLICOLOR_FORCE set and not 0 turns color on;
    3. NO_COLOR set and non-empty turns it off;
    4. TERM=dumb turns it off;
    5. otherwise, color when the platform helper reports that stderr can render ANSI.
  • Styling copies lld: the reld: prefix stays plain; error: is red and warning: is magenta, with the colon and space inside the colored span; Caused by: lines stay plain.

2. reld_core::platforms::term: the platform helper (per #137, no OS-specific cfg outside platforms/)

pub enum StderrTerminal { NotATerminal, Ansi }
/// Probe stderr and, where the OS needs it, switch on ANSI rendering. Called at most once.
pub fn prepare_stderr_for_ansi() -> StderrTerminal;
  • Unix, macOS, illumos, other unix, WASI: return Ansi if std::io::stderr().is_terminal(), otherwise NotATerminal. Nothing needs switching on.
  • Windows has three cases:
    1. Real console (conhost or Windows Terminal). GetConsoleMode(STD_ERROR_HANDLE) succeeds, so call SetConsoleMode(mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING).
      • If that works, return Ansi.
      • If it fails (a console too old for ANSI), return NotATerminal, so reld prints plain text instead of raw escape codes.
      • Don't restore the old mode at exit; cargo and git don't either.
    2. MSYS2/Cygwin/mintty terminal. GetConsoleMode fails but is_terminal() is true, and these terminals render ANSI natively, so return Ansi.
    3. File or pipe. Return NotATerminal.
  • always on Windows still attempts case 1, so forced color renders whenever possible.

3. Wiring

  • Early flag scan: Args::new scans argv for the color flags (last one wins) before full parsing, so argument-parsing errors respect the flag.
  • Parser entries: the existing entries in args/elf.rs call set_color_choice, so the flags remain native.
  • Default and inheritance: anything printed before a choice is set uses Auto, and forked children inherit the decision.
  • Call sites to move onto diagnostic::emit / render:
    • error.rs:108 (Warning Display) and error.rs:208 (report_error)
    • bridge.rs:406, bridge.rs:1097 and bridge.rs:1121 (as Note)
    • main.rs:29

Dependency approval (#88)

Approved by the repository owner (2026-09-17): reld-core may take windows-sys as a direct
[target.'cfg(windows)'.dependencies] entry, features Win32_System_Console and
Win32_Foundation, pinned to the version already resolved in Cargo.lock (0.61.2, pulled in by
colored), so no new crate enters the graph. ci/dependency-baseline.json is updated in the same
reviewed change. This approval covers no other crate.

The design above (the shared diagnostic helper plus platforms::term::prepare_stderr_for_ansi
with its three Windows cases) is also approved as specified.

Acceptance

  • Table-driven unit tests for should_color cover choice × env × stderr terminal state, and run on all hosts.
  • Byte-exact render tests cover error, warning and note, with color on and off.
  • Integration test on Linux, macOS and Windows CI: with --color-diagnostics=always, a native link error contains the colored error: span.
  • Integration test: auto mode with stderr piped, and --no-color-diagnostics, produce no ESC bytes.
  • An argument-parsing error honors --no-color-diagnostics placed anywhere on the command line.
  • Every native reld: diagnostic goes through diagnostic::emit / render.
  • Windows manual check: cmd, PowerShell, Windows Terminal and MSYS2 bash, each also with stdout redirected, show correct color and no raw escape codes.
  • fmt, clippy (-D warnings), the host-platform Dylint and the dependency-baseline check are green.

Activity

  1. changed the title [-]route --color-diagnostics as no-op/native, never to lld (perf regression)[/-] [+]native engine: real colored diagnostics for --color-diagnostics (not a no-op, never routed to lld)[/+] on Sep 17, 2026
  2. zackees commented on Sep 17, 2026

    @zackees
    CollaboratorAuthor

    Updated this issue's title and description with a new directive: add real colored diagnostics to the native engine rather than treating --color-diagnostics as a no-op (never routing it to lld still stands). The investigation found that auto mode keys off stdout instead of stderr, reproduced in both directions. It also found that only error and warning lines are colored today, with styling that doesn't match lld, and that no tests cover color. The previous description is superseded; it described #142's no-op/native state.

  3. changed the title [-]native engine: real colored diagnostics for --color-diagnostics (not a no-op, never routed to lld)[/-] [+]feat(diagnostics): add colorized diagnostics to the native engine[/+] on Sep 17, 2026
  4. zackees commented on Sep 17, 2026

    @zackees
    CollaboratorAuthor

    Renamed and redirected: this issue is now just about adding colorized diagnostics to the native engine. The earlier no-op/routing framing is gone. The description has the design: a same-on-every-OS diagnostic helper plus a platform helper in reld_core::platforms::term covering the Windows console, MSYS and file/pipe cases. It also has the open windows-sys dependency decision and the acceptance checklist.

  5. zackees commented on Sep 17, 2026

    @zackees
    CollaboratorAuthor

    Owner approved both the design and the direct windows-sys dependency for reld-core (#88). Recorded in the description; implementation starting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions