esl / gradient

Gradient is a static typechecker for Elixir
Apache License 2.0
436 stars 13 forks source link

Autodetect Elixir location in the Gradient escript if Elixir is managed by asdf #126

Closed erszcz closed 1 year ago

erszcz commented 2 years ago

So far the Gradient escript developed in #121 does not by default see Elixir libs, which will lead to false positives on unknown modules, like this:

/tmp/z.ex: Call to undefined function Enum.map/2 on line 9

The issue can be solved by using --path-add on the command line:

$ ./gradient --path-add /Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/elixir/ebin /tmp/z.ex

But we could also try to solve it programmatically on escript startup by detecting if we're running with ASDF ~or Kiex in the env, or at least trying to locate Elixir with which elixir.~ EDIT: let's focus on asdf in this ticket. It would free the user from appending the --add-path flag and looking for the right path(s) each time the escript is used.

erszcz commented 2 years ago

With ASDF in the env it's roughly:

> {elixir_bin, 0} = System.cmd("asdf", ["which", "elixir"])
> Path.wildcard(elixir_bin |> Path.dirname() |> Path.dirname() |> Path.join("lib/*/ebin"))
["/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/eex/ebin",
 "/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/elixir/ebin",
 "/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/ex_unit/ebin",
 "/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/iex/ebin",
 "/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/logger/ebin",
 "/Users/erszcz/.asdf/installs/elixir/1.13.3-otp-24/lib/mix/ebin"]
erszcz commented 2 years ago

Example code which leads to the problem:

$ cat 1.12/range_step.ex
defmodule RangeStep do
  def to_list do
    Enum.to_list(1..100//5)
  end
end

Invocation without --path-add:

$ ../../gradient 1.12/range_step.ex
Typechecking files...
1.12/range_step.ex: Call to undefined function Enum.to_list/1 on line 3
Total errors: 1

With the flag:

$ ../../gradient --path-add /Users/erszcz/.asdf/installs/elixir/1.13.4-otp-24/lib/elixir/ebin 1.12/range_step.ex
Typechecking files...
No errors found!