@@ -9,7 +9,8 @@ Much like in regular projects, to add a library we need to add a dependency to
99it. For simplicity we will use the popular ` fmt ` library as an example, but any
1010package from the [ package repository] ( https://ocaml.org/packages ) can be used.
1111
12- First we update the ` dune-project ` file to add a dependency on the opam package.
12+ To do so we update the ` dune-project ` file to add a dependency on the opam
13+ package.
1314
1415::::{dropdown} ` dune-project `
1516:icon: file-code
@@ -19,33 +20,27 @@ First we update the `dune-project` file to add a dependency on the opam package.
1920:emphasize-lines: 8
2021:::
2122
23+ Here we define the OPAM packages that we want to use, along with the version
24+ constraints these dependencies should adhere to.
25+
2226::::
2327
24- After this change to our project dependencies, we need to relock dependencies
25- to update our lock directory with the new packages.
28+ ::::{dropdown} ` dune-workspace `
29+ :icon: file-code
2630
27- ```
28- $ dune pkg lock
29- Solution for dune.lock:
30- - base-unix.base
31- - fmt.0.9.0
32- - ocaml.5.2.0
33- - ocaml-base-compiler.5.2.0
34- - ocaml-config.3
35- - ocamlbuild.0.15.0+dune
36- - ocamlfind.1.9.6+dune
37- - topkg.1.0.7
38- ```
31+ :::{literalinclude} dependencies/dune-workspace
32+ :language: dune
33+ :emphasize-lines: 2
34+ :::
3935
40- You can see a lot of new dependencies, among these ` fmt ` .
36+ In this file we direct Dune to enable package management in the current
37+ workspace. The ` pkg ` stanza configures Dune to manage the declared dependencies
38+ automatically.
4139
42- :::{note}
43- The list of packages being output includes all dependencies of your project,
44- including transitive dependencies.
45- :::
40+ ::::
4641
47- This will take care of installing the dependencies, but we still need to add it to
48- our build as a library as usual:
42+ This configuration will take care of installing the dependencies, but we still
43+ need to add it to our build as a library as usual:
4944
5045::::{dropdown} ` dune `
5146:icon: file-code
@@ -55,11 +50,12 @@ our build as a library as usual:
5550:emphasize-lines: 3
5651:::
5752
58- Adding a library dependency to our ` dune ` file via the ` libraries ` stanza.
53+ Adding a library dependency to our ` dune ` file via the ` libraries ` stanza. This
54+ is unchanged from the usual Dune workflow.
5955
6056::::
6157
62- This will allow us to use the ` Fmt ` module in our OCaml code.
58+ This change will allow us to use the ` Fmt ` module in our OCaml code.
6359
6460::::{dropdown} ` test.ml `
6561:icon: file-code
@@ -80,8 +76,34 @@ To build it we just call `build` again.
8076$ dune build
8177```
8278
83- which will download and install the new dependencies and build our project as
84- before.
79+ Dune will notice that the project depends on new packages. Thus it will re-run
80+ the internal dependency solver to find a solution for the set of packages to
81+ use. It will then use this new solution to download, build and install these
82+ dependencies automatically.
83+
84+ We can check the build log in ` _build/log ` and see the packages that the Dune
85+ solver has selected:
86+
87+ ```
88+ ...
89+ # Dependency solution for
90+ # _build/.sandbox/<sandbox-hash>/_private/default/.lock/dune.lock:
91+ # - base-unix.base
92+ # - fmt.0.11.0
93+ # - ocaml.5.4.0
94+ # - ocaml-base-compiler.5.4.0
95+ # - ocaml-compiler.5.4.0
96+ # - ocaml-config.3
97+ # - ocamlbuild.0.16.1+dune
98+ # - ocamlfind.1.9.8+dune
99+ # - topkg.1.1.1
100+ ...
101+ ```
102+
103+ :::{note}
104+ The list of packages being output includes all dependencies of your project,
105+ including transitive dependencies.
106+ :::
85107
86108As we see, the code works and uses ` fmt ` to do the pretty-printing:
87109
@@ -106,35 +128,42 @@ used for opam dependencies in the `dune-project` file.
106128
107129::::
108130
109- This ensures the ` fmt ` package to install will be compatible with
110- our request. These constraints will be taken into account the next time the
111- package is locked:
131+ This change ensures the ` fmt ` package to install will be compatible with our
132+ request. These constraints will be taken into account the next time the build
133+ system is ran.
112134
113- ```
114- $ dune pkg lock
115- Solution for dune.lock:
116- - base-unix.base
117- - fmt.0.9.0
118- - ocaml.5.2.0
119- - ocaml-base-compiler.5.2.0
120- - ocaml-config.3
121- - ocamlbuild.0.15.0+dune
122- - ocamlfind.1.9.6+dune
123- - topkg.1.0.7
135+ ``` sh
136+ dune build
124137```
125138
126- The version of ` fmt ` picked is indeed between ` 0.6 ` and ` 1.0 ` .
139+ Checking ` _build/log ` again reveals that our change was taken into account:
140+
141+ ```
142+ ...
143+ # Dependency solution for
144+ # _build/.sandbox/<sandbox-hash>/_private/default/.lock/dune.lock:
145+ # - base-unix.base
146+ # - fmt.0.9.0
147+ # - ocaml.5.4.0
148+ # - ocaml-base-compiler.5.4.0
149+ # - ocaml-compiler.5.4.0
150+ # - ocaml-config.3
151+ # - ocamlbuild.0.16.1+dune
152+ # - ocamlfind.1.9.8+dune
153+ # - topkg.1.1.1
154+ ...
155+ ```
127156
128157## Removing Dependencies
129158
130159Given all dependencies are defined in the ` dune-project ` file, removing a
131- dependency means to remove the dependency from the ` depends ` field of your
132- ` dune-project ` and relocking the project .
160+ dependency just means to remove the dependency from the ` depends ` field of your
161+ ` dune-project ` .
133162
134- The new lock directory will not depend on the package anymore, and in future
135- builds, the package will not be accessible as ` library ` anymore.
163+ From then on the project will not depend on the package anymore, and in future
164+ builds the package will not be accessible as ` library ` anymore.
136165
137166:::{note}
138- The removed dependency might still be part of the lock directory if some other
139- dependency of your project depends on it.
167+ The removed dependency might still be accessible if some other dependency of
168+ your project depends on it, thus if it is a transitive dependency .
140169:::
0 commit comments