AndyL

AndyL

I’d like to add some custom helper functions to iex. I use .iex.exs and can define a module there with my custom functions. But I can’t figure out how to auto-import the module into my iex environment. I’ve tried hacking the iex bash script (at /usr/local/bin/iex), browsed thru the iex source - no luck. Is there a way to extend IEx.Helpers, or auto-import my .iex.exs module?

Showing Posts 20 to 11

axelson

axelson

Scenic Core Team

I knew that aliasing would work but I swear I tried an import_file_if_available ".iex.exs" and that it failed to actually import a module defined in that .iexs.exs but indeed it does work great! Thank you! :tada:

> cat ~/.iex.exs
defmodule JaxIEx do
  def rr(opts \\ []), do: IEx.Helpers.recompile(opts)
end

import JaxIEx
import_file_if_available ".iex.exs"
[07:04:23] jason@Jasons-MacBook-Air /Users/jason/dev/password-validator
> cat .iex.exs
defmodule ProjIExUtils do
  def my_helper, do: IO.puts("hello, world")
end

import ProjIExUtils
[07:04:41] jason@Jasons-MacBook-Air /Users/jason/dev/password-validator
> iex --dot-iex ~/.iex.exs -S mix
Erlang/OTP 26 [erts-14.2.5.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.17.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> rr
:noop
iex(2)> my_helper
hello, world
:ok
iex(3)>

Now if I update my i and s aliases to:

alias s='iex --dot-iex ~/.iex.exs -S mix phx.server'
alias i='iex --dot-iex ~/.iex.exs -S mix'

I can type rr instead of recompile :grin:

zachallaun

zachallaun

Looking at what import_file_if_available does, it’s just a macro that reads the file, parses it with Code.string_to_quoted and then spits it out. So I think it should make any aliases/imports available unless you’ve established a new scope. So just to confirm, this definitely doesn’t work when running with iex --dot-iex ~/.iex.exs?

# ~/.iex.exs

alias Example.One
alias Example.Two

import_file_if_available ".iex.exs"

Edit: I was just able to try this out and it does seem to be working for me, which is good news! So there must be something else going on, I think:

~/dev/lib_elixir main wip ⇡1                                                                                10:54:22 AM
❯ echo -n 'alias Example.One\nimport_file_if_available ".iex.exs"' > ~/.iex.exs

~/dev/lib_elixir main wip ⇡1                                                                                10:56:14 AM
❯ echo -n 'alias Example.Two' > .iex.exs

~/dev/lib_elixir main wip ⇡1 ?1                                                                             10:56:23 AM
❯ iex --dot-iex ~/.iex.exs
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.17.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> One
Example.One
iex(2)> Two
Example.Two
axelson

axelson

Scenic Core Team

This has been a great development! However I’m still struggling with one specific aspect.

If a given project defines it’s own .iex.exs that imports a module, is there a way that I can also have a ~/.iex.exs that imports a module? So far I haven’t been able to get that to work without both .iex.exs scripts from having knowledge of each other.

Things I’ve tried:

  • I can use iex --dot-iex ~/.iex.exs to import my own .iex.exs, but then I don’t have a way to load the project-specific .iex.exs
    • If I load it with Code.eval_file(".iex.exs") then any imports it defines are not available
    • If I load it with import_file_if_available(".iex.exs") then any imports it defines is not available

The only workaround I have so far is for my ~/.iex.exs to have knowledge of the name of all modules that any of the project-specific .iex.exs wants to import and import them all if they exist.

josevalim

josevalim

Creator of Elixir

In Elixir v1.14+ you will be able to import the module as you define it in .iex.exs files, as we now evaluate it line by line: Evaluate --dot-iex line by line · elixir-lang/elixir@395b053 · GitHub

axelson

axelson

Scenic Core Team

Ah, ok. I’ll just define the helper somewhere else then.

Qqwy

Qqwy

TypeCheck Core Team

Unfortunately, no, because it is not possible to import a module in the same file as in which it was defined: you will get above compile error scenario.

axelson

axelson

Scenic Core Team

Is there an easy way to define an IEx helper now? I’d prefer to keep the helper definition inside .iex.exs if possible.

chazsconi

chazsconi

That works

Thanks @andy !

AndyL

AndyL OP

You need to start iex with the -pa option to append your home dir to the iex search path. Try iex -pa ~/

Also FYI you can use import_if_available - prevents the error message if the module is not found.

chazsconi

chazsconi

Doesn’t work for me unfortunately.

I have the Elixir.IEXHelper.beam file in my home folder and in
.iex.ex I have
import IEXHelper
I get
** (CompileError) /Users/chazsconi/.iex.exs:1: module IEXHelper is not loaded and could not be found

Any suggestions for how to fix this?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews