tino415

tino415

Hello all,

I’m triing to integrate dialyzer/dialyxer into one project that is using cachex, but dialyzer keeps reporting some veird error with Cachex.start_link/2 function. I managed to destille minimal example:

defmodule Test do
  def hello do
    {:ok, _pid} = Cachex.start_link(__MODULE__, stats: true)
  end
end

Dialyzer is complaining that

lib/test.ex:3:pattern_match
The pattern can never match the type.

Pattern:
{:ok, __pid}

Type:
:error | :ok | {:error, atom() | {:already_started, pid() | {_, _}}}

But when I tri to run function in iex, it returns {:ok, pid()}, also when I look at the code, typescpecs seems right and code also, so, how did dialyzer come up with return type :error | :ok | {:error, atom() | {:already_started, pid() | {_, _}}}? The code for Cachex.start_link/2 looks like this:

  @spec start_link(atom | Keyword.t) :: { atom, pid }
  def start_link(options) when is_list(options) do
    with { :ok,  name } <- Keyword.fetch(options, :name),
         { :ok,  true } <- ensure_started(),
         { :ok,  true } <- ensure_unused(name),
         { :ok, cache } <- setup_env(name, options),
         { :ok,   pid }  = Supervisor.start_link(__MODULE__, cache, [ name: name ]),
         { :ok,  link }  = Informant.link(cache),
                ^link   <- Overseer.update(name, link),
     do: { :ok,   pid }
  end
  def start_link(name) when not is_atom(name),
   do: error(:invalid_name)
  def start_link(name),
    do: start_link(name: name)

  @doc false
  @spec start_link(atom(), Keyword.t) :: { atom(), pid() }
  def start_link(name, options),
    do: start_link([name: name] ++ options)

Anybody have experience with similar problem and how to solve it?

Showing Posts 1 to 5

ityonemo

ityonemo

if Cachex.start_link/1 returns anything besides {:ok, pid}, (for example :error, from one of the with block fallthroughs), then your hello function will crash due to match error, dialyzer is trying to tell you this.

michallepicki

michallepicki

This seems to be a bug in specs in the sleeplocks library and should be fixed by Fix execute/2 spec by michallepicki · Pull Request #5 · whitfin/sleeplocks · GitHub

NobbZ

NobbZ

Which is fine, dialyzer assumes that it is exactly what the programer wants, in spirit of let-it-crash. It also is not what dialyzer complaints about here.

The error message of dialyzer says, that there will never be a {:ok, _} value returned, as dialyzer only knows about the possibilities :ok, :error, and an :error-tuple`

tino415

tino415 OP

Thank you, when I override sleeplock dependenci in mix.exs, it started to work

michallepicki

michallepicki

It’s possible that Cachex also has wrong specs, at least I think in many modules types referenced from specs won’t be found by Dialyzer, e.g. here: cachex/lib/cachex/services/overseer.ex at main · whitfin/cachex · GitHub Spec is used but Supervisor.Spec is aliased, not Cachex.Spec. But I may be wrong, and in that case Dialyzer is probably smart enough to ignore the spec.

Overall this is a common problem through the Elixir ecosystem, almost none of the libraries are running Dialyzer on the CI so specs just get outdated or invalid. I try to maintain Elixir itself working somehow with Dialyzer by running a check on it every day: GitHub - michallepicki/elixir-lang-dialyzer-runs: Daily Dialyzer checks on Elixir source code · GitHub but it won’t catch some issues because they may pop up when a function is called, not defined, and I am not analyzing Elixir test modules since they are .exs files.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews