hst337

hst337

How to change AST before Elixir compilation

XY Notice

I know about macros, __before_compile__ and other attributes, compiler tracers and such stuff. The question is exactly about implementing wrapper for Elixir compiler which be compatible with Mix.Task.Compiler, nothing else.

The Question

How do I write the Mix.Task.Compiler which will do some global changes to the AST before calling Elixir’s compiler to generate .beam files.

Current solution

Right now my compiler

  1. Searches for .ex files in the path specified by Mix.Project.config()[:elixirc_paths]
  2. Calls Code.string_to_quoted() on each file
  3. Changes the AST
  4. Calls Code.compile_quoted with File.write!() to the build_path of the binaries returned by compile_quoted

Current solution drawbacks

  1. I think it is possible to compile straight to file (without File.write!)
  2. I think this kind of compilation does not call compiler tracers
  3. I think it can have some problems with protocol consolidation
  4. It needs to read/write elixir’s compiler manifest, which is some kind of a private structure.
  5. It is difficult to change the AST because modules can have compile-time code in their bodies, which I need to preevaluate, to change the code inside functions.

Marked As Solved

josevalim

josevalim

Creator of Elixir

Not possible. By design. We want to avoid anything that goes and globally changes code, without a trace. You can hand roll, as in your current solution, but it will have pitfalls such as losing incremental compilation.

I would suggest taking a step back and discuss what you want to solve and try to find a way that would be considered idiomatic Elixir.

Sorry if this is not the answer you were looking for. :slight_smile:

Last Post!

hst337

hst337

That’s sad to hear, but I kinda had a feeling that these limitations are intentional. I’ll think about other solutions then, thanks!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement