Today I started messing with Phoenix - a web framework for Elixir - and got to a point where I wanted to inspect the environment around which a chunk of code was running in an EEx template, but ran into a bit of a wall. Following the example in the documentation for
IEx.pry, my controller read thusly:
I started my server via
$ mix phoenix.start, and with the '/posts' route connected properly in my Phoenix router, I initiated a request. This showed up in my Phoenix log:
Cannot pry #PID<0.259.0> at web/controllers/posts_controller.ex:8. Is an IEx shell running?
Well…yeah, an IEx shell was actually running at the time, in a different process. Given the by-default distributed nature of Elixir, I assumed that calling
IEx.pry from the Phoenix process would grab a running IEx process and ask to take it over. My assumption was reinforced by a quick and sloppy scan through
IEx.Server. After many minutes of confusedly poking at
iex, I discovered, almost by accident, that the Phoenix process must run within an IEx session*:
$ iex -S mix phoenix.start
With this process spun up and an idle IEx prompt waiting, I issued a request and saw this in my terminal:
Request to pry #PID<0.266.0> at web/controllers/posts_controller.ex:9. Allow? [Yn]
y<enter> gets me what I expect - an interactive session in the context of my controller request.
Being brand-new at Elixir (and definitely Phoenix), needing to run my Phoenix process through IEx wasn't obvious. I spent quite awhile trying to track this down through Googling everything and anything that seemed relevant and didn't really find anything that pointed me in the right direction, so I decided to post this here in case it helps someone down the road. Based on this experience, the pattern of
iex -S mix <DO_SOMETHING> seems important to keep front-of-mind.
* After briefly reading through the
IEx.Server source again, I'm not convinced that the original behavior I expected isn't possible, it just appears
IEx.pry doesn't support it. I'll follow up here if I later discover that it is.