Pointing at the error: compiler-style diagnostics in uutils coreutils
For over half a century, the Coreutils have continuously evolved. Now, they are taking a significant step forward in how they report errors. Traditionally, Unix tools display errors as a single line on stderr, indicating what went wrong but not where. However, certain commands with specific arguments, such as test expressions, chmod modes, sort keys, or tr sets, require more precise error reporting.
Rust's compiler, rustc, has been implementing this for years, and ariadne provides a similar feature without needing additional dependencies. Uutils' awk utility already reports errors in the same manner for awk programs, and Coreutils arguments can also be parsed in the same way.
With the release of 0.11.0, Coreutils begins implementing this new approach. When stderr is a terminal, a parse error is displayed as a report, echoing back the arguments as a source line, marking the culprit with a caret, and providing a help line explaining the syntax when applicable. For instance, when using the tr command, which already assumes familiarity with collating sequences, the caret highlights the specific character causing the error.
Similarly, when using the sort command, the report can pinpoint a stray character in the sort key. The env -S command, which takes a whole command line and splits it like a shell would, also benefits from this enhanced reporting.
The old error messages could only quote the offending fragment, often resulting in truncated or unclear information. The new report format addresses these limitations, ensuring users receive more detailed and actionable error messages. For example, when parsing a test expression built from separate arguments, the report echoes only the problematic argument, making it easier to identify and correct the error.
Whether it's handling a SIZE argument (a number followed by a unit) or a printf-style format string in numfmt --format, the report accurately indicates the exact location of the error.
This innovative approach is now being integrated into other uutils projects, including findutils and sed, which involve parsing small languages like regexes and sed scripts. By leveraging the same caret-based error reporting, these utilities can provide more precise and helpful error messages. As this feature becomes more widespread, the hope is that more utilities will adopt this interactive nicety, enhancing the overall user experience and making error reporting more informative and user-friendly.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.