ruhshan

ruhshan

Go-to-definition & hover lost when using dynamic module calls via Application.get_env/3

On vscode, I’ve noticed that ElixirLS navigation and hover only work when I call modules directly, but break as soon as I switch to a dynamic lookup via Application.get_env/3.

Direct calls (works)

defmodule TaskProcessor.Events do
  alias TaskProcessor.Segments
  alias TaskProcessor.Events.Queue.Oban,         as: EventsQueue
  alias TaskProcessor.Events.Persistance.Ecto,   as: Repo

  def enqueue_create_event(event_id) when is_integer(event_id) and event_id > 0 do
    EventsQueue.enqueue_create_event(event_id)
  end

  def enqueue_create_event(bad_event_id), do: {:error, {:invalid_event_id, bad_event_id}}

  def process_event(%{event_id: id, isCreated: true}) do
    case Repo.fetch_with_segments(id) do
      :not_found -> {:error, :not_found}
      {:ok, %{date: date, segments: segs}} ->
        Segments.schedule_segments(segs, id, date)
    end
  end
end
  • Cmd+Click on EventsQueue.enqueue_create_event/1 or Repo.fetch_with_segments/1 jumps to the implementation.
  • Hover shows the function signature.

Dynamic calls (Not working, or am i not doing right ?)

defmodule TaskProcessor.Events.Impl do
  @behaviour TaskProcessor.Events.Behavior

  alias TaskProcessor.Segments

  @impl true
  @spec enqueue_create_event(any()) :: {:ok, Oban.Job.t()} | {:error, any()}
  def enqueue_create_event(event_id) when is_integer(event_id) and event_id > 0 do
    queue_impl().enqueue_create_event(event_id)
  end

  def enqueue_create_event(bad_event_id), do: {:error, {:invalid_event_id, bad_event_id}}

  @impl true
  def process_event(%{event_id: id, isCreated: true}) do
    case repo_impl().fetch_with_segments(id) do
      :not_found -> {:error, :not_found}
      {:ok, %{date: date, segments: segs}} ->
        Segments.schedule_segments(segs, id, date)
    end
  end

  defp queue_impl do
    Application.get_env(:task_processor, :events_queue, TaskProcessor.Events.Queue.Oban)
  end

  defp repo_impl do
    Application.get_env(:task_processor, :events_repo,  TaskProcessor.Events.Persistance.Ecto)
  end
end

  • Cmd+Click on queue_impl().enqueue_create_event/1 or repo_impl().fetch_with_segments/1 does nothing.
  • Hovering shows nothing.

Environment

  • ElixirLS version: 0.28.0
  • Elixir version: 1.18.4
  • Erlang/OTP version: 28
  • Macbook M1

Is there any way to configure ElixirLS (or leverage Dialyzer/Elixir compiler hints) so that go-to-definition and hover info work for modules resolved at runtime via Application.get_env/3? I need this functionality so that I can dynamically inject mocks during tests using the Mox library.

Thank you!

Most Liked

lud

lud

If you can, use Application.compile_env/2 and assign to a module attibute. This works.

If you really need to change the implementationat runtime then it cannot work as far as I know. Better use a callback in thas case.

ruhshan

ruhshan

Update:

Installed elixir tools, that contains next-ls. Now I can use go to definition on @repo.fetch_with_segments.

I’m wondering, if next-ls can do it, elixir-ls which is more mature supposed to do it too.

lud

lud

I don’t have my dev machine right now but I’m using ElixirLS and this generally works. Maybe try rm -rf .elixir_ls sometimes it helps.

apoorv-2204

apoorv-2204

AFAIK Elixir ls wont.
I would suggest a high level api for Repo and queue.
And separate impl. As it looks coupled

High level api

defmodule TaskProcessor.Events do

  alias TaskProcessor.EventsQueue
  alias TaskProcessor.EventsRepo

  def enqueue_create_event(event_id) when is_integer(event_id) and event_id > 0 do
    EventsQueue.enqueue_create_event(event_id)
  end

  def enqueue_create_event(bad_event_id), do: {:error, {:invalid_event_id, bad_event_id}}

  def process_event(%{event_id: id, isCreated: true}) do
    case EventsRepo.fetch_with_segments(id) do
      :not_found -> {:error, :not_found}
      {:ok, %{date: date, segments: segs}} ->
        Segments.schedule_segments(segs, id, date)
    end
  end
end
defmodule TaskProcessor.EventsQueue do
  
defp impl do
    Application.get_env(:task_processor, :events_queue, TaskProcessor.Events.Queue.Oban)
 end

def create_event() do
impl()...function_call()
end
  
end
defmodule TaskProcessor.EventsRepo do
 
  defp repo_impl do
    Application.get_env(:task_processor, :events_repo,  TaskProcessor.Events.Persistance.Ecto)
  end

   def with_segs() do
      repo_impl()...function_call()
   end
end
defmodule TaskProcessor.Events.Persistance.Ecto do
    @behaviour Repo....

   def fetch_with_segments(...) do
     ...
   end
end

its an example and wont solve the issue of elixir ls…
x

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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 45766 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement