ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.72k stars 2.53k forks source link

Executable produced by Zig prints the exit error on the StdErr #17082

Closed evanxg852000 closed 1 year ago

evanxg852000 commented 1 year ago

Zig Version

0.11.0

Steps to Reproduce and Observed Output

Running this snippet with Zig will output error: Foo

const std = @import("std");

const Error = error{Foo};

fn fallible() !void {
    return Error.Foo;
}

pub fn main() !void {
    try fallible();
}

The expectation as an application developer is that nothing should be printed on my behalf. I have already printed a better error message to the user. Probably I don't even want the user knowing the type of Error. At this point, all I care about is that an error code is returned to the shell. But do not print something.

For an anecdote, this is the output of this simple CLI application when ran in a shell https://github.com/evanxg852000/clack/blob/main/src/examples/demo.zig#L11-L35

$ ./demo              
Error: no command specified: `demo`.
A simple command line app to demonstrate clack.

Usage: demo [SUBCOMMAND] [OPTIONS]

Subcommands:
- foo: a command to display name
- help: prints the command help message
error: NotEnoughInput

The last line is undesired here IMHO, especially in release mode. It would be nice to limit this only to debug builds. But a compiler printing on my behalf in release mode, meh.

Expected Output

Nothing

g-w1 commented 1 year ago

I think the application developer should expect this to be the case when returning an error from main. Returning ints from main changes the exit code, and an error is something significant that should not go ignored. If you want to ignore errors, you could use catch instead of try:

const std = @import("std");

const Error = error{Foo};

fn fallible() !void {
    return Error.Foo;
}

pub fn main() void {
    fallible() catch {};
}