Phoenix: Requiring IEx in only :dev and :test for entire project
Asked Answered
S

1

7

I'm new to Elixir/Phoenix and I was wondering if there was a way to essentially require IEx for the purpose of IEx.pry in the same manner that in Ruby/Rails you can add something like

group :test, :development do
  gem 'pry', require: 'pry'
end

which would allow you to binding.pry in essentially any file without needing to include require 'pry'

I'm finding that having to require IEx on any controller, model, view, etc I want to debug to be tedious.

Socinus answered 6/1, 2017 at 20:18 Comment(0)
H
1

You can use web/web.ex file.

A module that keeps using definitions for controllers, views and so on.

This can be used in your application as:

use App.Web, :controller
use App.Web, :view

The definitions below will be executed for every view, controller, etc, so keep them short and clean, focused on imports, uses and aliases.

Just put require IEx here when you need it for controllers, models etc.

defmodule App.Web do
  def model do
    quote do
      ...
      require IEx
    end
  end
  def controller do
    quote do
      ...
      require IEx
    end
  end
end
Hypochondriac answered 7/1, 2017 at 12:43 Comment(4)
This will be done for :prod environment as well, which OP apparently doesn't want.Discommode
^ Precisely. I had previously done exactly as @Hypochondriac suggested. It's a work-around, but I was hoping for something more dev/test specific.Socinus
Can someone tell me why if Mix.env == "dev", do: require IEx does not work?Ema
@Johannes When the compiler tries to expand the macro IEx.pry it has not yet evaluated the conditional (if being a macro itself). You can use IEx.pry in the same condition in which you require IEx though. Also note what the docs say for Mix.html#env/0: This function should not be used at runtime in application code"Alkalinity

© 2022 - 2024 — McMap. All rights reserved.