NobbZ

NobbZ

Raise warnings during compile time

I’ve forked an abondoned hex-package and continue to maintain it under a new name.

The old code-base does provide a macro that we call finalize/0 for the purpose of this thread. This macro does its work depending on some module-attributes which are filled by other macros (which I do not need to explain here, just consider them similar to the test macro of ex_unit).

Now I want to move the functionallity of this finalize/0-macro into __before_compile__/1-macro and add a proper deprecation warning to finalize/0.

Such that a warning is emitted during compiletime when someone still uses finalize/0 and the hint that it is handled automatically in the current version and that the feature will get completely removed with version X. But how? I did not find anything in the documentation.

Marked As Solved

josevalim

josevalim

Creator of Elixir

TL;DR - Don’t do this. Use IO.warn(msg, Macro.Env.stacktrace(env)).

Please don’t do this.

:elixir_errors is a private module. It is undocumented. It is internal to Elixir only. It is not meant to be accessed by other libraries besides Elixir. There is no guarantee it will continue working on future releases.

Relying on private functions it is very harmful. In the future, if such practices are wide-spread, one of the two things will happen:

  1. We won’t be able to improve Elixir, or improving it will be more complex, because developers are relying on APIs they should not, and we will need to phase them out first

  2. Packages will break on new Elixir versions, which will make it harder for people to migrate to future Elixir versions, or give the impression the ecosystem is fragile (which would be if new Elixir versions are breaking supposedly valid code).

Please don’t rely on private APIs. Not for Elixir, not for other libraries. It is more harmful than you may think.

12
Post #5

Also Liked

hubertlepicki

hubertlepicki

You can do this:

warn.ex:

defmodule Foo do
  defmacro __using__(_opts) do
    :elixir_errors.warn __ENV__.line, __ENV__.file, "this is compilation warning"
  end
end

defmodule Bar do
  use Foo
end

and when compiling you get:

$ elixirc --warnings-as-errors warn.ex 
warn.ex:3: warning: this is compilation warning
Compilation failed due to warnings while using the --warnings-as-errors option
$ rm *.beam                           
$ elixirc warn.ex                     
warn.ex:3: warning: this is compilation warning

In both cases warning is printed to the stderr while compilation. In first case, however, compilation fails and appropriate shell exit code is set. In second case only warning is printed.

@Edit: Code search results · GitHub

Looks like it’s ok to do that, but better in this form:

%{file: file, line: line} = __CALLER__
:elixir_errors.warn(line, file, "error")
josevalim

josevalim

Creator of Elixir

No problem! Although Discourse shows I replied to you, it was really to everyone that is using private APIs, here or on GitHub. :wink:

hubertlepicki

hubertlepicki

This is great. I am terribly sorry to post misleading message, it was the thing I saw somewhere and adopted.

➜  elixir  cat warn.ex                         
defmodule Foo do
  defmacro __using__(_opts) do
    msg = "this is compilation error"
    IO.warn(msg, Macro.Env.stacktrace(__ENV__))
  end
end

defmodule Bar do
  use Foo
end
➜  elixir  elixirc --warnings-as-errors warn.ex
warning: this is compilation error
  warn.ex:4: Foo.__using__/1

Compilation failed due to warnings while using the --warnings-as-errors option

This is way better, however!

Edit: ah yeah it’s plenty of wrong usage in different libs then:

low hanging fruits if someone wants to make some pull requests.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement