mix dialyze
Add a "mix dialyze" task that runs the dialyzer on the Elixir project.
I was talking to @yrashk. Giving the complexity in managing different plt files, I believe this approach must be first ironed out in another project and merged once it is stable.
What needs to be achieved to get this added? I have created a task[1] that automates the full dialyzer workflow and manages PLTs efficiently by using 3 disjoint PLTs per analysis:
- A PLT per OTP version stored in
MIX_HOME(or~/.mix/) for global use - A PLT per Elixir version stored in
MIX_HOME(or~/.mix/) for global use - A PLT per build environment per project stored in build path for use locally only in that build environment
For each PLT it aims to guarantee:
- The beam files in the running/loaded in the current VM are used in the PLT
- The minimum number of beam files are in the PLT
- The minimum number of changes are made to the PLT when it is out of date
The Mix task is zero config and aims to just work™ on any project with mix dialyze:
- Automatically chooses suitable PLTs and builds them only as required
- Automatically detects all (nested) deps and their beam files (using
.appfiles) - Automatically finds all beam files in the current project to analyse and ensures none are in the PLTs.
Missing features:
- No tests
- No docs
- No command to clean the PLTs (required if file corrupted?)
- No extra warnings can be turned on (will use command line args, e.g.
--(no-)underspecs)
Possible features (would likely add considerable time but could be done gradually once in):
- Format all warnings using Elixir terms
- Document all warnings
[1] https://github.com/fishcakez/dialyze
That's great work! We can add this for discussion after 1.0 is out.
Automatically generating and maintaining PLTs for Erlang and Elixir are great! Quick question: how does mix dialyze check to guarantee if it should regenerate the plt for a given dependency?
The building (and checking) process for a PLT is as follows:
- Take a selection of apps
- Add all (nested) deps of these apps to the list of apps
- Filter out any app that is part of another PLT file[1][2]
- Find the beam files (using
:code[3]) for all the modules listed in the.appfiles of the remaining apps - Compare this list of beam files with those currently in the PLT[4]
- Remove any beam files that are no longer needed from the PLT
- Add any new beam files are that needed to the PLT
- Ensure the PLT is using an up to date version of all its beam files[5]
The success typing analysis will then use all 3 PLTs (Erlang, Elixir and Deps) as they are disjoint (have no common modules/beam files) but the union (all 3 combined) contain all the dependent modules. While this is slightly complicated, it achieves the best possible efficiency in terms of building/re-using PLTs.
- Erlang PLT is built first so does not filter any apps. Elixir PLT is built next and (I think) just includes
:elixirbecause all its deps are in the Erlang PLT. - The deps PLT reads the
.appof the current project or all the.appfiles in an umbrella project to find all(inclued_)applications(i.e. all the deps). It then removes any deps (like:elixir,:stdlib,:kernel) from its list that are in Erlang PLT or Elixir PLT. So in the case of the task itself it would add:mixand:dialyzerbecause those are the only two deps not yet part of a PLT. The other 2 PLTs actually find their deps like this too. I assumeexrm/relxuses a similar method. - This means we should always get the right module/beam files because they have been loaded/put in the path by mix.
- Path changes are considered new beam files
- This step is done by dialyzer (https://github.com/erlang/otp/blob/b66e75c285cba469c5225f3394da149456d17d16/lib/dialyzer/src/dialyzer_plt.erl) and reads the beam files to check the md5 of the abstract code is the same as it has on record. This means if you are on
elixir-0.14.4-devand update to a later elixir commit in the same version the minimum number of modules will be update in the PLT. This update will be reasonably quick and only need to be done once for all mix projects. Similar efficiencies would occur for the Erlang PLT. For the deps PLT, while it only matters for the local project and build environment, it won't require a full rebuild when one dep changes.
Awesome, thanks for the write up!
FYI, there's also this project which has similar aims: https://github.com/jeremyjh/dialyxir
@jwarwick, dialyxir does not do any of the things I mention except to run dialyzer so I don't think it has similar aims.
Cool idea, but guys, are you aware that "A PLT per OTP version" got somewhat complicated in 17.x line? Please see https://github.com/jeremyjh/dialyxir/issues/18
@herenowcoder, fortunately it is not just a cool idea and does work :). Please see https://github.com/fishcakez/dialyze/blob/c86e72f27946d012a881c2d743a98df9f8c5c816/lib/mix/tasks/dialyze.ex#L63-L78, this makes an attempt to get a full version (e.g. 17.1) before reverting to the release (e.g. 17). In most cases where mix dialyze is used this should get the full version. If creating a custom erlang release, it is possible to supply a custom OTP_VERSION file to use seperate PLT's.
I see that ticket is closed. Is inclusion of dialyze task into Elixir core still considered ? Do you need feedback from real apps ?
@mremond it is not considered because I think the dialyzer-elixir integration still needs a lot of work to fit with our tooling. In particular, reports in dialyzer are very confusing and do not fit our philosophy of great error messages. We would also need to teach dialyzer to print terms in Elixir format and not Erlang. However, if you want to use it, dialyze from @fishcakez is great: https://github.com/fishcakez/dialyze
Sure, I understand. I have already integrated it in my project. I am so used to Erlang message that my brain made the translation for me and I did not notice format needed to be improved. All is fine, thanks !