@@ -30,6 +30,56 @@ defmodule Code do
30
30
file, without tracking. `eval_file/2` should be used when you are interested in
31
31
the result of evaluating the file rather than the modules it defines.
32
32
33
+ ## Code loading on the Erlang VM
34
+
35
+ Erlang has two modes to load code: interactive and embedded.
36
+
37
+ By default, the Erlang VM runs in interactive mode, where modules
38
+ are loaded as needed. In embedded mode the opposite happens, as all
39
+ modules need to be loaded upfront or explicitly.
40
+
41
+ You can use `ensure_loaded/1` (as well as `ensure_lodead?/1` and
42
+ `ensure_lodead!/1`) to check if a module is loaded before using it and
43
+ act.
44
+
45
+ ## `ensure_compiled/1` and `ensure_compiled!/1`
46
+
47
+ Elixir also includes `ensure_compiled/1` and `ensure_compiled!/1`
48
+ functions that are a superset of `ensure_loaded/1`.
49
+
50
+ Since Elixir's compilation happens in parallel, in some situations
51
+ you may need to use a module that was not yet compiled, therefore
52
+ it can't even be loaded.
53
+
54
+ When invoked, `ensure_compiled/1` and `ensure_compiled!/1` halt the
55
+ compilation of the caller until the module becomes available. Note
56
+ the distinction between `ensure_compiled/1` and `ensure_compiled!/1`
57
+ is important: if you are using `ensure_compiled!/1`, you are
58
+ indicating to the compiler that you can only continue if said module
59
+ is available.
60
+
61
+ If you are using `Code.ensure_compiled/1`, you are implying you may
62
+ continue without the module and therefore Elixir may return
63
+ `{:error, :unavailable}` for cases where the module is not yet available
64
+ (but may be available later on).
65
+
66
+ For those reasons, developers must typically use `Code.ensure_compiled!/1`.
67
+ In particular, do not do this:
68
+
69
+ case Code.ensure_compiled(module) do
70
+ {:module, _} -> module
71
+ {:error, _} -> raise ...
72
+ end
73
+
74
+ Finally, note you only need `ensure_compiled!/1` to check for modules
75
+ being defined within the same project. It does not apply to modules from
76
+ dependencies as dependencies are always compiled upfront.
77
+
78
+ In most cases, `ensure_loaded/1` is enough. `ensure_compiled!/1`
79
+ must be used in rare cases, usually involving macros that need to
80
+ invoke a module for callback information. The use of `ensure_compiled/1`
81
+ is even less likely.
82
+
33
83
## Compilation tracers
34
84
35
85
Elixir supports compilation tracers, which allows modules to observe constructs
@@ -1209,39 +1259,7 @@ defmodule Code do
1209
1259
If it succeeds in loading the module, it returns `{:module, module}`.
1210
1260
If not, returns `{:error, reason}` with the error reason.
1211
1261
1212
- ## Code loading on the Erlang VM
1213
-
1214
- Erlang has two modes to load code: interactive and embedded.
1215
-
1216
- By default, the Erlang VM runs in interactive mode, where modules
1217
- are loaded as needed. In embedded mode the opposite happens, as all
1218
- modules need to be loaded upfront or explicitly.
1219
-
1220
- Therefore, this function is used to check if a module is loaded
1221
- before using it and allows one to react accordingly. For example, the `URI`
1222
- module uses this function to check if a specific parser exists for a given
1223
- URI scheme.
1224
-
1225
- ## `ensure_compiled/1`
1226
-
1227
- Elixir also contains an `ensure_compiled/1` function that is a
1228
- superset of `ensure_loaded/1`.
1229
-
1230
- Since Elixir's compilation happens in parallel, in some situations
1231
- you may need to use a module that was not yet compiled, therefore
1232
- it can't even be loaded.
1233
-
1234
- When invoked, `ensure_compiled/1` halts the compilation of the caller
1235
- until the module given to `ensure_compiled/1` becomes available or
1236
- all files for the current project have been compiled. If compilation
1237
- finishes and the module is not available, an error tuple is returned.
1238
-
1239
- `ensure_compiled/1` does not apply to dependencies, as dependencies
1240
- must be compiled upfront.
1241
-
1242
- In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
1243
- must be used in rare cases, usually involving macros that need to
1244
- invoke a module for callback information.
1262
+ See the module documentation for more information on code loading.
1245
1263
1246
1264
## Examples
1247
1265
@@ -1292,30 +1310,28 @@ defmodule Code do
1292
1310
end
1293
1311
1294
1312
@ doc """
1295
- Ensures the given module is compiled and loaded.
1296
-
1297
- If the module is already loaded, it works as no-op. If the module was
1298
- not compiled yet, `ensure_compiled/1` halts the compilation of the caller
1299
- until the module given to `ensure_compiled/1` becomes available or
1300
- all files for the current project have been compiled. If compilation
1301
- finishes and the module is not available, an error tuple is returned.
1313
+ Similar to `ensure_compiled!/1` but indicates you can continue without said module.
1302
1314
1303
- Given this function halts compilation, use it carefully. In particular,
1304
- avoid using it to guess which modules are in the system. Overuse of this
1305
- function can also lead to deadlocks, where two modules check at the same time
1306
- if the other is compiled. This returns a specific unavailable error code,
1307
- where we cannot successfully verify a module is available or not.
1315
+ While `ensure_compiled!/1` indicates to the Elixir compiler you can
1316
+ only continue when said module is available, this function indicates
1317
+ you may continue compilation without said module.
1308
1318
1309
1319
If it succeeds in loading the module, it returns `{:module, module}`.
1310
1320
If not, returns `{:error, reason}` with the error reason.
1311
-
1312
1321
If the module being checked is currently in a compiler deadlock,
1313
1322
this function returns `{:error, :unavailable}`. Unavailable doesn't
1314
1323
necessarily mean the module doesn't exist, just that it is not currently
1315
1324
available, but it (or may not) become available in the future.
1316
1325
1317
- Check `ensure_loaded/1` for more information on module loading
1318
- and when to use `ensure_loaded/1` or `ensure_compiled/1`.
1326
+ Therefore, if you can only continue if the module is available, use
1327
+ `ensure_compiled!/1` instead. In particular, do not do this:
1328
+
1329
+ case Code.ensure_compiled(module) do
1330
+ {:module, _} -> module
1331
+ {:error, _} -> raise ...
1332
+ end
1333
+
1334
+ See the module documentation for more information on code loading.
1319
1335
"""
1320
1336
@ spec ensure_compiled ( module ) ::
1321
1337
{ :module , module }
@@ -1325,11 +1341,24 @@ defmodule Code do
1325
1341
end
1326
1342
1327
1343
@ doc """
1328
- Same as `ensure_compiled/1` but raises if the module cannot be compiled.
1344
+ Ensures the given module is compiled and loaded.
1345
+
1346
+ If the module is already loaded, it works as no-op. If the module was
1347
+ not compiled yet, `ensure_compiled!/1` halts the compilation of the caller
1348
+ until the module given to `ensure_compiled!/1` becomes available or
1349
+ all files for the current project have been compiled. If compilation
1350
+ finishes and the module is not available or is in a deadlock, an error
1351
+ is raised.
1352
+
1353
+ Given this function halts compilation, use it carefully. In particular,
1354
+ avoid using it to guess which modules are in the system. Overuse of this
1355
+ function can also lead to deadlocks, where two modules check at the same time
1356
+ if the other is compiled. This returns a specific unavailable error code,
1357
+ where we cannot successfully verify a module is available or not.
1329
1358
1330
- This is the preferred function to use if you want to ensure a module
1331
- is compiled and you want to raise in case it isn't.
1359
+ See the module documentation for more information on code loading.
1332
1360
"""
1361
+ @ doc since: "1.12.0"
1333
1362
@ spec ensure_compiled! ( module ) :: module
1334
1363
def ensure_compiled! ( module ) do
1335
1364
case ensure_compiled ( module , :hard ) do
0 commit comments