Libraries without Hex
Most Erlang projects on a package index start with a dependency such as cowboy or jsx, and a course that runs offline cannot install one. That is not a small corner of the language: a large part of what those packages do is already in Erlang's own applications, and the rest is a decision about the build. This page lists both sides, and the program on it runs everywhere because it only asks the runtime which applications are installed.
What OTP covers
An OTP installation is eight or so applications that a package index would call libraries. crypto has the hashes, the ciphers and the random bytes; ssl has clients and servers over TLS; inets has the HTTP client and server, which the HTTP lesson uses; mnesia is the database of the Mnesia lesson; tools holds the profilers and the test tools, and the json module of OTP 27 removes the JSON dependency of the modern Erlang lesson. The program asks the runtime which of them are here:
otp.erl
-module(otp). -export([main/0]). main() -> Apps = [kernel, stdlib, crypto, ssl, inets, mnesia, tools], Present = [{App, code:lib_dir(App) =/= {error, bad_name}} || App <- Apps], io:format("count: ~p~n", [length(Present)]), io:format("all present: ~p~n", [lists:all(fun({_App, Found}) -> Found end, Present)]).
erlc otp.erl
erl -noshell -eval 'otp:main(), halt().'
count: 7 all present: true
code:lib_dir/1 answers the folder of an application or {error, bad_name}, so the check needs no start and no configuration. A project that needs one of these lists it in the applications key of its .app.src and calls application:ensure_all_started/1 at the start.
What this environment cannot fetch
The image of this course has no Hex and no network, so a package that is not in OTP cannot be downloaded. The names a reader will meet in other projects are these: the web servers cowboy and cowlib with ranch; the JSON encoders jsx and jiffy; the test tools proper and meck; the observability library recon of the observability lesson; the pool poolboy; the registry gproc; and the older logger lager. A dependency in rebar.config reads like this fragment:
{deps, [{cowboy, "2.12.0"}, {jsx, "3.1.0"}]}.
rebar3 compile would fetch both from Hex and fail here. The command is shown but not run, and the program below is the evidence: the runtime knows none of the names, and it says so without a network:
rebar3 compile
missing.erl
-module(missing). -export([main/0]). main() -> [io:format("~p: ~p~n", [Name, code:lib_dir(Name)]) || Name <- [cowboy, jsx, jiffy, proper]].
erlc missing.erl
erl -noshell -eval 'missing:main(), halt().'
cowboy: {error,bad_name} jsx: {error,bad_name} jiffy: {error,bad_name} proper: {error,bad_name}
The same answer comes from a project that lists the dependency: the build stops at Failed to fetch or at a missing application, not at a compile error in the code. That is why a lesson of this course never depends on one of these packages: a page that cannot be built on a machine with no network is a page whose output nobody can check.
The three ways out
A project that must build offline has three honest options. The first is to use OTP where it is enough: json instead of jsx, inets instead of a small HTTP client, ssl for a TLS socket, crypto for a hash, and mnesia or dets for a store. The second is to vendor the dependency: its source tree goes into the repository, rebar.config names the folder instead of a version, and the build never asks a server for it. The third is to write the small piece yourself when the dependency was a few hundred lines anyway, which is how the json module and the uri_string module came into the standard library in the first place.
What none of the three does is hide the difference. A page or a project that needs a package says so, and a command this environment cannot run is marked as such rather than given an output nobody saw. The finishing project builds a release with an empty dependency list, which is the same rule from the other side.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
code:lib_dir/1answer for an application that is not installed?Answer
{error, bad_name}The call looks the name up in the installation and reports a missing one as
bad_name. -
Which OTP application holds the HTTP client?
Answer
inetshttpcis a module insideinets, andsslis the TLS layer below it. -
Which OTP 27 module removes the need for a JSON package?
Answer
jsonThe
jsonmodule of OTP 27 encodes and decodes JSON without a dependency. -
What does a
rebar.configdependency need beforerebar3 compilecan build it here?Answer
A copy of the package on the machine or in the repository
Without Hex or the network the source must already be there, which is what vendoring means.
-
What does vendoring a dependency mean?
Answer
Its source tree lives in the repository
A vendored package is a folder of the project, so the build never fetches it.
-
Which application has the built-in profilers this course uses?
Answer
toolscprof, eprof and fprof are modules of the
toolsapplication. -
What does this module print?
-module(q1). -export([main/0]). main() -> io:format("~p~n", [code:lib_dir(stdlib) =/= {error, bad_name}]).Answer
truestdlibis part of every installation, so the folder is found and the comparison holds. -
What does this module print?
-module(q2). -export([main/0]). main() -> io:format("~p~n", [code:lib_dir(cowboy)]).Answer
{error,bad_name}cowboy is not part of OTP and this image cannot fetch it, so the lookup reports a bad name.
-
What does this module print?
-module(q3). -export([main/0]). main() -> Json = iolist_to_binary(json:encode(#{ok => true})), io:format("~p~n", [Json]).Answer
<<"{\"ok\":true}">>The json module writes the map as an object with a boolean value.
-
What does this module print?
-module(q4). -export([main/0]). main() -> io:format("~p~n", [byte_size(crypto:strong_rand_bytes(8))]).Answer
8The call answers eight random bytes, so their size is eight.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container. The `rebar3 compile` with a Hex dependency was not run: this environment has no Hex and no network.
Article author: Arthur Isaev