rekkice

rekkice

IEx - Expose API to evaluate code inside IEx session for REPL Integrations

I’m building an editor integration to evaluate Elixir code in an IEx session. While Code.eval_string/3 allows tracking variable bindings, i think there’s currently no way to persist the environment (aliases, imports, etc.) between evaluations using this approach.

Use Case:

  • User is using an editor to send code to IEx (through a named pipe, e.g.), which is then read by a custom module defined in .iex.exs.
  • User defines alias MyApp.Module in one evaluation.
  • In subsequent evaluations, they expect Module.function() to work (as if it would in a regular IEx shell).

Proposal:
Expose a public API to evaluate code directly in the IEx instance, e.g.:

# it could be a helper function
result = IEx.Helpers.eval_string("IO.puts(123)")

# or maybe work by directly sending a message to IEx’s evaluator process (e.g., using eval_pid() if such a function were available)
send(eval_pid(), {:eval, "IO.puts(123)"})

I think this would make integrating IEx with external tools such as code editors a much easier task. Helix allows piping selected code directly to a Bash command, so i was using that to send code to a named pipe and read that in IEx. IEx is very useful, but i always experience some friction between it and my editor because i have to manually copy and paste code to it.

I found this post from a while ago that suggests something that i think would work for my case, but i tried it on Elixir 1.18.1 with OTP 26 and it didn’t work, maybe because it’s not a public API.

I’d love to hear if others have faced similar challenges or if there’s an existing approach i might have missed. I’d also be happy to contribute to this implementation if there’s interest, though I’m still learning Elixir and don’t have experience with Erlang.

Most Liked

v0idpwn

v0idpwn

Hi,

I think most of what you want is achievable with Code:

def iterative_eval(code, bindings \\ [], env \\ env_for_eval([])) do
  quoted = Code.string_to_quoted(code)
  Code.eval_quoted_with_env(quoted, bindings, env)
end

Which can be used as:

iex(5)> {result, bindings, env} = Repl.iterative_eval("a = 1"); :ok # Using this `:ok` to avoid printing env
:ok
iex(6)> {result, bindings, env} = Repl.iterative_eval("a + 2", bindings, env); :ok
:ok
iex(7)> result
{:ok, 3}
iex(8)> bindings
[a: 1]

It carries the env, hence it carries aliases, requires, etc. To use the env from iex, you can just use __ENV__:

iex(9)> alias Map, as: M
Map
iex(10)> {result, bindings, env} = Repl.iterative_eval("map = M.new()", [], __ENV__); :ok
:ok
iex(11)> result
{:ok, %{}}
iex(12)> bindings
[map: %{}]
iex(13)> 

I don’t know if there’s a way to load these bindings/env back in iex, though.

Where Next?

Popular in Proposals: Ideas Top

tubedude
Hey, Earlier i posted a question, but after some research I think a proposal is due. Working on a Phoenix Live View app, I needed clien...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
MUSTDOS
Hello all! assign(socket, :name, “Elixir”) Why can’t we have assign/stream take a group of atoms and maps/structs as a default to r...
New
manhvu
In a large repo, working with module need to add alias too much is quite annoyed and not good for organizing code. I think better add su...
New
New
Matsa59
Lets start by a story, I work with a team on a pretty big CMS with lots of modules. We split the cms using umbrella apps. So we have comp...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
bamorim
Story behind Recently, I gave a talk on a meetup about improving performance of Phoenix applications and the example app was a LiveView ...
New
eagle-head
Hi everyone, I’ve been researching Content Security Policy Level 3 support in Phoenix and wanted to share my findings and a proposal for...
New
alaadahmed
Hi folks, I tried Phoenix 1.7 and it is awesome, but I have small suggestion, which in my opinion will organize files in a better way. ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement