Skip to content

feat(diagnostics): colorize native diagnostics behind one helper - #151

Merged
zackees merged 1 commit into
mainfrom
feat/144-colorized-diagnostics
Sep 17, 2026
Merged

zackees merged 1 commit into
mainfrom
feat/144-colorized-diagnostics

Conversation

@zackees

@zackees zackees commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Closes #144.

Summary

Native diagnostics are now colorized. Every reld: … line goes through the new reld_core::diagnostic helper, which decides once whether to color and formats the line. Errors are red and warnings magenta, with the colon and trailing space inside the colored span and the reld: prefix plain, matching ld.lld byte for byte:

reld:  ^[[0;31merror: ^[[0mFailed to open input file `nonexistent.o`
reld:  ^[[0;35mwarning: ^[[0m-z unknownzopt is not yet supported
ld.lld: ^[[0;31merror: ^[[0mcannot open nonexistent.o: No such file or directory

The auto-detection bug this fixes

colored turns color on when stdout is a terminal, but reld writes diagnostics to stderr. Both directions were wrong, and both are fixed:

Case Before After
reld … 2>err.log from a terminal escape codes written into the log plain
stderr on a terminal, stdout redirected no color colored

Auto mode now follows stderr and honors NO_COLOR, CLICOLOR_FORCE and TERM=dumb. An explicit flag outranks the environment, and the last flag wins, as in ld.lld.

Platform helper

Host terminal mechanics sit behind a new platforms::term facade, so the #137 boundary still holds (no host cfg outside platforms/). prepare_stderr_for_ansi handles the three Windows cases:

  1. Console: switch ENABLE_VIRTUAL_TERMINAL_PROCESSING on the stderr handle. colored::control::set_virtual_terminal could not be used: it targets the stdout handle, ignores API failures and always returns Ok, so it does the wrong thing whenever stdout is redirected.
  2. MSYS2/Cygwin/mintty: GetConsoleMode fails on these named pipes, but they render ANSI themselves, so they report as ANSI.
  3. Console too old to accept the flag: reported as plain, so reld never writes raw escapes to it.

Unix, macOS, illumos, other unix and WASI just check whether stderr is a terminal.

Other changes

  • The flags are applied by an argv pre-scan in Args::new, so a diagnostic raised while parsing a later argument is colored as asked. That was impossible before.
  • --color-diagnostics[=always|never|auto] / --no-color-diagnostics stay native and never route to lld. The invalid-value error text is unchanged.
  • The decision is mirrored into colored's override, so the symbol-info printer follows it too.
  • The routing log (reld: engine=…) keeps its exact text, because ci/linker_modes.py and ci/consumer_acceptance.py parse it. The bridge signal message and the DHAT dump failure now go through the helper.
  • Adds windows-sys as a direct reld-core dependency for the console calls, approved by the repository owner in feat(diagnostics): add colorized diagnostics to the native engine #144 under the ci: reject unapproved Rust dependency additions #88 policy, with ci/dependency-baseline.json updated in the same change. The version was already resolved through colored, so Cargo.lock grows by one line and no new crate enters the graph.

Validation (local, Linux host)

  • Unit tests (reld-core, 209 passed) cover the decision table (choice × environment × terminal), byte-exact rendering for error/warning/note with color on and off, flag-value parsing, and the argv pre-scan (both dash forms, last-flag-wins, invalid value ignored there).
  • New integration test crates/reld/tests/color_diagnostics.rs (6 tests) runs the real binary with stderr piped: plain by default, forced color, explicit disable, NO_COLOR/CLICOLOR_FORCE, the magenta warning path, and argument errors honoring the flag.
  • Manual pty checks, all correct: piped stderr plain; stderr-on-tty with stdout redirected colored; stderr redirected to a file plain; =always colored through a pipe; --no-color-diagnostics and NO_COLOR plain on a tty; CLICOLOR_FORCE colored through a pipe.
  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, the host-platform Dylint (default and --no-default-features), ci/check_dependencies.py and ci/tests (258 passed) are all green.
  • Cross type-check/clippy: x86_64-pc-windows-msvc (covers the new console code), aarch64-apple-darwin, FreeBSD, illumos, Android and WASI.
  • The full default-features Windows build could not run locally: this machine's soldr broker is a stale singleton whose daemon fails to start, which breaks on proc-macro2's build script before reaching reld. CI's Phase 1 windows-msvc job covers it.
  • Windows console and MSYS behavior itself needs a Windows terminal and is not covered by CI; the issue's manual checklist still applies.

🤖 Generated with Claude Code

…d#144)

Diagnostics are now produced by `reld_core::diagnostic`, which decides once
whether to color and formats every `reld: …` line. Errors are red and warnings
magenta, with the colon and trailing space inside the colored span and the
`reld: ` prefix plain, matching ld.lld byte for byte.

Fixes the auto-detection bug: `colored` enables color when *stdout* is a
terminal, but reld writes diagnostics to *stderr*. Redirecting stderr to a file
from a terminal leaked escape codes into it, and redirecting only stdout
dropped color from an interactive stderr. Auto mode now follows stderr, with
NO_COLOR, CLICOLOR_FORCE and TERM=dumb honored; an explicit flag outranks all
of them, and the last flag wins.

Host terminal mechanics live behind the new `platforms::term` facade, so the
#137 boundary still holds. Windows switches ENABLE_VIRTUAL_TERMINAL_PROCESSING
on the *stderr* console handle (colored's helper targets stdout and ignores
failures), reports MSYS2/Cygwin ptys as ANSI-capable since GetConsoleMode fails
on them, and reports a console too old to accept the flag as plain so reld never
writes raw escapes to it.

Because the flags are applied by an argv pre-scan in `Args::new`, a diagnostic
raised while parsing a later argument is colored as asked, which was impossible
before.

The routing log (`reld: engine=…`) keeps its exact text: ci/linker_modes.py and
ci/consumer_acceptance.py parse it.

Adds `windows-sys` as a direct reld-core dependency for the console calls,
approved by the repository owner in reld#144 under the #88 policy, with the
baseline updated. The version was already resolved through `colored`, so no new
crate enters the graph.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@zackees
zackees merged commit 50107b4 into main Sep 17, 2026
18 checks passed
@zackees
zackees deleted the feat/144-colorized-diagnostics branch September 17, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant