Skip to content

Commit ff00b58

Browse files
Update documentation to use autolocking
Signed-off-by: Marek Kubica <[email protected]>
1 parent 1b54548 commit ff00b58

File tree

9 files changed

+217
-104
lines changed

9 files changed

+217
-104
lines changed

doc/tutorials/dune-package-management/dependencies.md

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Much like in regular projects, to add a library we need to add a dependency to
99
it. For simplicity we will use the popular `fmt` library as an example, but any
1010
package 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

86108
As 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

130159
Given 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
:::
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(lang dune 3.21)
2+
(pkg enabled)

doc/tutorials/dune-package-management/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ setup
2424
dependencies
2525
pinning
2626
repos
27+
locking
2728
:::
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Locking Your Dependencies
2+
3+
In the default use-case Dune will automatically determine which packages to
4+
install, by reading the package constrains, determining compatible versions and
5+
installing the dependencies automatically.
6+
7+
For many projects this is a good and acceptable behavior as users often want to
8+
use new versions of their dependencies. However some projects might want to
9+
keep a fixed set of (transitive) dependencies that is only updated manually.
10+
11+
## Create a lock directory manually
12+
13+
If a lock directory exists in the source, Dune will use that to fix the exact
14+
version and source of dependencies. The default name of said lock directory is
15+
`dune.lock`. Lock directories are created with:
16+
17+
```
18+
$ dune pkg lock
19+
Solution for dune.lock:
20+
- ocaml.5.2.0
21+
- ocaml-base-compiler.5.2.0
22+
- ocaml-config.3
23+
```
24+
25+
Whenever Dune encounters a `dune.lock` folder, it will use the set of
26+
dependencies defined in the lock. It contains all the metadata about package
27+
names and versions, their dependencies and source locations that are necessary
28+
to build the project's dependencies.
29+
30+
On the next build, Dune will read the stored solver solution from the
31+
`dune.lock` directory, download and build the dependencies and then continue on
32+
building the project as usual.
33+
34+
The lock directory will not be updated until `dune pkg lock` is rerun.
35+
36+
:::{note}
37+
This approach is similar to using `opam switch export --full --freeze` to
38+
export the configuration of a switch.
39+
:::
40+
41+
Deleting the lock directory will cause Dune to fall back to automatically
42+
determining dependency versions via the declared package constraints.

doc/tutorials/dune-package-management/pinning.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,37 @@ dependencies.
2626

2727
::::
2828

29-
The next time the package is locked, Dune will use this repository instead of
30-
the information from the selected package repositories.
29+
The next time the project is built, Dune will use this repository instead of
30+
the information from the selected package repositories. Thus the `fmt` package
31+
will be built from the source in the specified Git repository rather than from
32+
the source tarball released in the `opam-repository`.
3133

34+
```sh
35+
dune build
3236
```
33-
$ dune pkg lock
34-
Solution for dune.lock:
35-
- base-unix.base
36-
- fmt.dev
37-
- ocaml.5.0.0
38-
- ocaml-base-compiler.5.0.0
39-
- ocaml-config.3
40-
- ocamlbuild.0.15.0+dune
41-
- ocamlfind.1.9.6+dune
42-
- topkg.1.0.7
37+
38+
will find a new solution, quoting `_build/log`:
39+
40+
```
41+
...
42+
43+
# Dependency solution for
44+
# _build/.sandbox/<sandbox-hash>/_private/default/.lock/dune.lock:
45+
# - base-unix.base
46+
# - fmt.dev
47+
# - ocaml.5.4.0
48+
# - ocaml-base-compiler.5.4.0
49+
# - ocaml-compiler.5.4.0
50+
# - ocaml-config.3
51+
# - ocamlbuild.0.16.1+dune
52+
# - ocamlfind.1.9.8+dune
53+
# - topkg.1.1.1
54+
...
4355
```
4456

4557
Unlike previously, the version of the `fmt` library that is picked is `dev`, to
4658
signify a development version.
4759

48-
The next time the project is built, the `fmt` package will be built from the
49-
source in the specified Git repository rather than from the source tarball
50-
released in the `opam-repository`.
5160

5261
```
5362
$ dune exec ./test.exe

doc/tutorials/dune-package-management/repos.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,56 @@ if it didn't exist):
1717

1818
:::{literalinclude} repos/dune-workspace
1919
:language: dune
20+
:emphasize-lines: 5-6,8-10
2021
:::
2122

2223
::::
2324

2425
In this case, we want to select a specific revision of the community repository
2526
instead of always using the most recent one as it would do by default. We
26-
define a new repository and configure the lock directory to use this
27+
define a new repository and configure the Dune solver to use this
2728
repository.
2829

2930
For more information about the stanzas refer to the {doc}`repositories stanza
3031
</reference/dune-workspace/repository>` as well as the {doc}`lock_dir stanza
3132
</reference/dune-workspace/lock_dir>`.
3233

33-
When relocking the dependencies, the list of packages that are found as
34-
dependencies changes accordingly:
34+
The next time the build system is run, instead of using the default
35+
repositories at their newest versions, the solver will check out the configured
36+
repositories at the defined revisions. These will then be used to determine the
37+
new solution, which will get used for downloading and building the
38+
dependencies.
3539

40+
```sh
41+
dune build
3642
```
37-
$ dune pkg lock
38-
Solution for dune.lock:
39-
- base-unix.base
40-
- fmt.0.9.0
41-
- ocaml.5.0.0
42-
- ocaml-base-compiler.5.0.0
43-
- ocaml-config.3
44-
- ocamlbuild.0.15.0+dune
45-
- ocamlfind.1.9.6+dune
46-
- topkg.1.0.
47-
```
4843

49-
Compared to before, the OCaml compiler version is older, which shows
50-
that we did indeed pick an older version of the package repository for locking.
44+
will thus log a new solution in `_build/log`. Note that a lot of package
45+
versions are different, as the state of the opam-repository is frozen at the
46+
specific commit:
47+
48+
```
49+
...
50+
# Dependency solution for
51+
# _build/.sandbox/<sandbox-hash>/_private/default/.lock/dune.lock:
52+
# - base-unix.base
53+
# - fmt.0.9.0
54+
# - ocaml.5.0.0
55+
# - ocaml-base-compiler.5.0.0
56+
# - ocaml-config.3
57+
# - ocamlbuild.0.16.1+dune
58+
# - ocamlfind.1.9.8+dune
59+
# - topkg.1.0.7
60+
...
61+
```
5162

5263
:::{note}
5364
This feature can also be used to make sure the locked dependencies are
5465
reproducible, as fixing all the package repository versions will lead to
5566
equivalent locking results.
5667
:::
68+
69+
:::{seealso}
70+
{doc}`/tutorials/dune-package-management/locking`
71+
More information how to keep package versions locked.
72+
:::

doc/tutorials/dune-package-management/repos/dune-workspace

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
(lang dune 3.17)
1+
(lang dune 3.21)
2+
3+
(pkg enabled)
24

35
(lock_dir
46
(repositories overlay specific-upstream))

0 commit comments

Comments
 (0)