eqwalizer icon indicating copy to clipboard operation
eqwalizer copied to clipboard

trying eqwalizer on elixir projects

Open tothlac opened this issue 2 years ago • 2 comments

We wanted to try eqwalizer on our elixir projects. I know it would be a lot better to implement a parser for Elixir projects, but as a first step we decompiled the beam files to erlang sources, and we were running eqwalizer on those sources.

We have found some problems:

  • in the decompiled source there is a -file attribute:
-file("lib/asdfqqqq.ex", 1).
-module('Elixir.Asdfqqqq').

When this attribute is there eqwalizer simply ignores all problems. If I manually delete this line, it works.

  • there is also an info/1 function in the source:
'__info__'(module) -> 'Elixir.Asdfqqqq';
'__info__'(functions) -> [{double, 1}];
'__info__'(macros) -> [];
'__info__'(exports_md5) -> <<"\035S8}I7\030\232\236g-\023">>;

this last function clause will cause this problem:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 429, error_len: Some(1) }', crates/elp/src/bin/[reporting.rs:101](http://reporting.rs:101/):78
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  • actually, when I manually fix these problems eqwalizer works quite well, and it is able to spot the problems in the code (of course the line numbers are wrong). One remaining problem is that I could not figure out how to tell to eqwalizer where the declarations of Elixir types are. So for instance:
-spec double({error, 1}) -> 'Elixir.String':t().
double(_x@1) -> 2 * _x@1.

Elixir.String type is declared in $ELIXIR_DIR//lib/elixir/lib/string.ex. Similarly, I want to add all files where elixir types are declared. How can I add those using project.json?

I have one more question... Let's say I have my_app application, which uses my_lib as a dependency. In my_app.erl I have the following:

-include_lib("my_lib/include/my_lib.hrl").

I've tried it with this project.json:

{
  "apps": [
    {
      "name": "my_app",
      "dir": "",
      "ebin": "_build/default/lib/my_app/ebin",
      "extra_src_dirs": [""],
      "include_dirs": ["include"],
      "macros": ["TEST"],
      "src_dirs": ["src"]
    }
  ],
  "deps": [
    {
      "name": "my_lib",
      "dir": "",
      "ebin": "_build/default/lib/my_lib/ebin",
      "extra_src_dirs": [""],
      "include_dirs": ["_build/default/lib/my_lib/include"],
      "macros": ["TEST"],
      "src_dirs": ["src"]
    }

  ],
  "root": ""
}

elp is still complaining about being not able to find the include file:

  eqWAlizing                ████████████████████ 3/3                                                                             error: parse_error
    ┌─ src/my_app.erl:3:14
    │
103 │ -include_lib("my_lib/include/my_lib.hrl").
    │              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find include lib "my_lib/include/my_lib.hrl"

Do you have an idea what should I put into my project.json ?

tothlac avatar Jan 24 '23 13:01 tothlac

We were trying to make it work using the following project.json :

{
  "apps": [
    {
      "name": "asdfqqqq",
      "dir": "/home/joe/Projects/asdfqqqq",
      "ebin": "/home/joe/Projects/asdfqqqq/_build/dev/lib/asdfqqqq/ebin",
      "extra_src_dirs": ["test"],
      "include_dirs": ["include"],
      "macros": ["TEST"],
      "src_dirs": ["erlangized_src/"]
    }
  ],
  "deps": [
    {
      "name": "elixir",
      "dir": "/home/joe/.manager/elixir",
      "ebin": "/home/joe/.managerl/erlang-OTP-23.2.3/elixirs/elixir-v1.12.2/lib/elixir/lib/elixir/ebin",
      "extra_src_dirs": [],
      "include_dirs": [],
      "macros": [],
      "src_dirs": ["lib"]
    }

  ],
  "source_root": "/home/joe/Projects/asdfqqqq",
  "otp_lib_dir": "/home/joe/.managerl/erlang-OTP-23.2.3/lib/"
}

Elixir.String.t() type is declared in the above-mentioned folder. With these settings, it still was not able to find the declaration of this type. Is it possible that to find the declaration eqwalizer also needs the erlang source code present in the src directory, or do you have any other ideas on what's going on wrong?

tothlac avatar Jan 27 '23 15:01 tothlac

  1. We don't provide any support for type-checking Elixir projects out of the box and we don't plan to do it on our own in the near future
  2. Nevertheless, I think that it's feasible to to use it for Elixir, - but someone needs to drive it. And it's hard to estimate the amount of tech work.

Contributions are welcome.

ilya-klyuchnikov avatar Jun 26 '23 12:06 ilya-klyuchnikov