How to make warning into error?

In C++, we can be pedantic and convert all warnings into errors during compilation:

shell> g++ -Werror

Is there a similar feature in Julia? This switch could be very useful in Travis CI builds for mature packages. I am tired of flagging other’s packages about warnings whenever a new Julia version is released.

That’s one of the worst option to be enabled in a project. It’s for dev’s to enable locally. I’ve had countless number of issues with projects that force it or have it on by default.

For depwarn we have it already. Not for warning in general, that may or may not be useful for local development.

Please don’t. This must not be the default or recommanded for any package. It won’t help fixing anything and it’ll only discourage people from supporting nightly/new release. It has already happened multiple times for many packages.

And please also don’t file those issues, especially not within a few months after new julia release. People usually know the depwarn exists and just haven’t got around to fix them yet or it could be a package without an active maintainer in which case the issue will also just be ignored. It’s usually easy and much more helpful to submit a pr.

1 Like

Thank you @yuyichao for your prompt reply. I was thinking of -Werror in Travis CI (so it is for devs only), but I will follow your suggestions and forget about it for now.

A related question: Is there a way of finding out how many warnings were generated by running a given piece of code? Like a global warning counter? (I looked at the source for warn, but I could not find anything.)

in particular, I’d be interested in the number of warnings emitted by Pkg.test. This would make it possible to auto-generate statistics about warnings per commit.

Julia doesn’t “throw” warnings. However, warning messages are typically written to the stderr stream, so you can turn such warnings to errors simply by redirecting stderr to a read-only stream:

julia> redirect_stderr(open(touch(tempname()), "r"))
IOStream(<file ...>)

julia> @warn "foo"
ERROR: ArgumentError: write failed, IOStream is not writeable

Originally posted by @stevengj in Terminate after warning is thrown - global setting -#2 by stevengj


Or, you can install a custom logger that turns warnings into errors:

julia> using Logging, LoggingExtras

julia> error_on_warning = EarlyFilteredLogger(global_logger()) do log_args
           if log_args.level >= Logging.Warn
               error(log_args)
           end
           return true
       end;

julia> global_logger(error_on_warning);

Adapted from @fredrikekre’s answer here

2 Likes