ijdickinson

ijdickinson

Dialyzer error when checking Mix task

I have a Mix task that extracts the version number of the project (for use in a build pipeline):

defmodule Mix.Tasks.VersionNumber do
  @moduledoc "Mix task to print the current version number to stdout"
  use Mix.Task

  @shortdoc "Print the current application version number"
  def run(_) do
    MyApp.MixProject.project |> Keyword.fetch!(:version) |> IO.puts
  end
end

This all works fine. However, when I run mix dialyzer on my project, I get an error:

$ mix dialyzer
Compiling 1 file (.ex)
Finding suitable PLTs
Checking PLT...
[:asn1, :castore, :certifi, :compiler, :connection, :cowboy, :cowboy_telemetry, :cowlib, 
:crypto, :dart_sass, :db_connection, :decimal, :ecto, :ecto_sql, :eex, :elixir, :esbuild, 
:expo, :file_system, :finch, :geo, :geo_postgis, :gettext, :hackney, :heroicons, :hpax, 
:httpoison, :idna, :jason, :kernel, :logger, :metrics, :mime, :mimerl, :mint, :mix, 
:nimble_options, :nimble_pool, :parse_trans, :phoenix, :phoenix_copy, :phoenix_ecto, 
:phoenix_html, :phoenix_live_dashboard, :phoenix_live_reload, :phoenix_live_view, 
:phoenix_pubsub, :phoenix_template, :plug, :plug_cowboy, ...]
PLT is up to date!
No :ignore_warnings opt specified in mix.exs and default does not exist.

Starting Dialyzer
[
  check_plt: false,
  init_plt: '/home/ian/projects/verna/myapp/_build/dev/dialyxir_erlang-25.1.2_elixir-1.14.2_deps-dev.plt',
  files: ['/home/ian/projects/verna/myapp/_build/dev/lib/myapp/ebin/Elixir.MyApp.LocationSearch.SearchResult.beam',
   '/home/ian/projects/verna/myapp/_build/dev/lib/myapp/ebin/Elixir.MyApp.beam',
   '/home/ian/projects/verna/myapp/_build/dev/lib/myapp/ebin/Elixir.MyApp.Application.beam',
   '/home/ian/projects/verna/myapp/_build/dev/lib/myapp/ebin/Elixir.MyApp.ReferenceData.LandRegistry.beam',
   '/home/ian/projects/verna/myapp/_build/dev/lib/myapp/ebin/Elixir.MyApp.Gettext.beam',
   ...],
  warnings: [:unknown]
]
Total errors: 1, Skipped: 0, Unnecessary Skips: 0
done in 0m4.56s
lib/mix/tasks/version_number.ex:7:unknown_function
Function MyApp.MixProject.project/0 does not exist.
________________________________________________________________________________
done (warnings were emitted)
Halting VM with exit status 2

At a guess, the problem is maybe related to project/0 being defined in an .exs file rather than .ex? Is there a way to either ignore the mix task in the Dialyzer analysis, or, better, help it to find the definition of project/0?

Marked As Solved

al2o3cr

al2o3cr

Dialyzer depends on the compiler, so it’s definitely not going to see modules defined in .exs files.

That file will be loaded by Mix at runtime before running the task, so it doesn’t actually fail.

A better approach might be to use the built-in Mix machinery, specifically Mix.Project.config/0:

defmodule Mix.Tasks.VersionNumber do
  @moduledoc "Mix task to print the current version number to stdout"
  use Mix.Task

  @shortdoc "Print the current application version number"
  def run(_) do
    Mix.Project.config() |> Keyword.fetch!(:version) |> IO.puts
  end
end

Also Liked

kenny-evitt

kenny-evitt

This answer on the following Stack Overflow question seems to have worked to fix similar errors for one of my own projects:

The answer states that you can add the following to (the return value of) the project/0 function in your project Mix module file (i.e. mix.exs) – or add :mix to the list if the key-value pair already exists:

      dialyzer: [plt_add_apps: [:mix]]

Example project/0 function:

  def project do
    [
      app: :some_app,
      version: "0.1.0",
      elixir: "~> 1.6",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      # Added following line:
      dialyzer: [plt_add_apps: [:mix]]
    ]
  end

Where Next?

Popular in Questions 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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement