Skip to content

Commit aad05d3

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

File tree

8 files changed

+138
-103
lines changed

8 files changed

+138
-103
lines changed

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

Lines changed: 17 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
@@ -21,28 +22,6 @@ First we update the `dune-project` file to add a dependency on the opam package.
2122

2223
::::
2324

24-
After this change to our project dependencies, we need to relock dependencies
25-
to update our lock directory with the new packages.
26-
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-
```
39-
40-
You can see a lot of new dependencies, among these `fmt`.
41-
42-
:::{note}
43-
The list of packages being output includes all dependencies of your project,
44-
including transitive dependencies.
45-
:::
4625

4726
This will take care of installing the dependencies, but we still need to add it to
4827
our build as a library as usual:
@@ -80,8 +59,10 @@ To build it we just call `build` again.
8059
$ dune build
8160
```
8261

83-
which will download and install the new dependencies and build our project as
84-
before.
62+
This will recongnize that the project depends on new packages. Thus it will
63+
re-run the internal dependency solver to find a solution for the set of
64+
packages to use. It will then use this new solution to download, build and
65+
install these dependencies automatically.
8566

8667
As we see, the code works and uses `fmt` to do the pretty-printing:
8768

@@ -106,35 +87,24 @@ used for opam dependencies in the `dune-project` file.
10687

10788
::::
10889

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:
90+
This ensures the `fmt` package to install will be compatible with our request.
91+
These constraints will be taken into account the next time the build system is
92+
ran.
11293

94+
```sh
95+
dune build
11396
```
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
124-
```
125-
126-
The version of `fmt` picked is indeed between `0.6` and `1.0`.
12797

12898
## Removing Dependencies
12999

130100
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.
101+
dependency just means to remove the dependency from the `depends` field of your
102+
`dune-project`.
133103

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.
104+
From then on the project will not depend on the package anymore, and in future
105+
builds the package will not be accessible as `library` anymore.
136106

137107
:::{note}
138-
The removed dependency might still be part of the lock directory if some other
139-
dependency of your project depends on it.
108+
The removed dependency might still be accessible if some other dependency of
109+
your project depends on it, thus if it is a transitive dependency.
140110
:::

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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Locking Your Dependencies
2+
3+
In the default use-case Dune will automatically determine which packages to
4+
install, including solving compatible versions and installing their
5+
dependencies recursively.
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 dependencies that is only updated manually. This can be
10+
done in multiple ways.
11+
12+
## Create a lock directory
13+
14+
Dune locks dependencies by creating lock directories. The default lock
15+
directory is called `dune.lock`. To create a lock, run
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. The lock will not be updated until a user
27+
relocks by running `dune pkg lock` again.
28+
29+
On the next build, Dune will read the solution from the `dune.lock` directory,
30+
download and build the dependencies and then continue on building the project
31+
as usual.
32+
33+
:::{note}
34+
This approach is similar to using `opam switch export --full --freeze` to
35+
export the configuration of a switch.
36+
:::
37+
38+
## Pin the package repositories to a commit
39+
40+
A way to ensure that dependencies won't change due to package updates is to pin
41+
the package repositories to a fixed commit.
42+
43+
::::
44+
45+
::::{dropdown} `dune-workspace`
46+
:icon: file-code
47+
48+
:::{literalinclude} locking/dune-workspace
49+
:language: dune
50+
:::
51+
52+
53+
::::
54+
55+
On the next build, Dune will check out out these specific two repositories at
56+
the specified commit and use them for resolving all dependencies. It will then
57+
download and build the dependencies as usual.
58+
59+
:::{note}
60+
This approach is similar to pinning OPAM repositories by explicitely setting
61+
them to known versions using commands like `opam repository add <name> <fixed-url>`.
62+
:::
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(lang dune 3.21)
2+
(pkg enabled)
3+
4+
(repository
5+
(name overlay1)
6+
(url "https://github.com/ocaml-dune/opam-overlays.git#2a9543286ff0e0656058fee5c0da7abc16b8717d"))
7+
8+
(repository
9+
(name upstream1)
10+
(url "https://github.com/ocaml/opam-repository.git#2fd4164ca1e27b8c6027454c4844c1a1f6dca0bc"))
11+
12+
(lock_dir
13+
(repositories overlay1 upstream1))

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ dependencies.
2929
The next time the package is locked, Dune will use this repository instead of
3030
the information from the selected package repositories.
3131

32-
```
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
43-
```
44-
4532
Unlike previously, the version of the `fmt` library that is picked is `dev`, to
4633
signify a development version.
4734

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,19 @@ For more information about the stanzas refer to the {doc}`repositories stanza
3030
</reference/dune-workspace/repository>` as well as the {doc}`lock_dir stanza
3131
</reference/dune-workspace/lock_dir>`.
3232

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

5239
:::{note}
5340
This feature can also be used to make sure the locked dependencies are
5441
reproducible, as fixing all the package repository versions will lead to
5542
equivalent locking results.
5643
:::
44+
45+
:::{seealso}
46+
{doc}`/tutorials/dune-package-management/locking`
47+
More information how to keep package versions locked.
48+
:::

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,24 @@ just define the module as an executable.
5555

5656
After our project skeleton is set up, we can proceed to the next step.
5757

58-
## Locking Dependencies
58+
## Enable package management
5959

60-
After declaring the dependencies, you will need to tell Dune which package
61-
versions to use for your project. This is done by creating a lock directory.
62-
This is easily done with a new Dune command:
60+
So far we have done everything as in a regular Dune project. However, now we
61+
need to tell Dune that we want to use the package management feature. To do so
62+
we have to add a new stanza to our `dune-workspace` file, creating it if it
63+
doesn't exist.
6364

64-
```
65-
$ dune pkg lock
66-
Solution for dune.lock:
67-
- ocaml.5.2.0
68-
- ocaml-base-compiler.5.2.0
69-
- ocaml-config.3
70-
```
71-
72-
This will update all the required opam repositories, use the newest version of
73-
each and try to find a set of packages and versions that satisfy the
74-
constraints that your project dependencies declare.
65+
::::{dropdown} `dune-workspace`
66+
:icon: file-code
7567

76-
:::{note}
77-
The versions that get locked might be different from this tutorial, as we only
78-
specified the lower bound of `ocaml`; barring any additional configuration, Dune
79-
will pick the newest possible version for each dependency.
68+
:::{literalinclude} setup/dune-workspace
69+
:language: dune
70+
:emphasize-lines: 2
8071
:::
8172

73+
This new stanza will tell Dune to determine the packages that your project
74+
depends on, find the right versions, download, and build them on the next build.
75+
8276
## Build Project
8377

8478
To build the project, you can just use the regular Dune commands.
@@ -87,11 +81,22 @@ To build the project, you can just use the regular Dune commands.
8781
dune build
8882
```
8983

90-
This will download, build, and install all your locked dependencies and then use
91-
those to build your project. This means that the first time building it will take
92-
longer than usual, as the dependencies need to be built first. Subsequent
93-
builds where all dependencies have been built before will be just as fast as
94-
before.
84+
Since this is the first time we have run the build system after enabling package
85+
management a number of things will happen:
86+
87+
1. It will download and update all required opam repositories to determine
88+
which packages are available.
89+
2. It will attempt to find a package solution that satisfies all dependency
90+
constraints.
91+
3. It will download the sources of the dependencies.
92+
4. It will build the dependencies in sandbox locations.
93+
5. It will install the dependencies in the build folder.
94+
6. It will build the project using the dependencies that it has installed.
95+
96+
This means that building the first time will take longer than usual, as the
97+
dependencies need to be built, possibly including the OCaml compiler.
98+
Subsequent builds where all dependencies have already been built will
99+
be significantly faster.
95100

96101
We can show that the package has been built successfully and works as expected:
97102

@@ -101,13 +106,16 @@ Hello, OCaml, Rust!
101106
```
102107

103108
:::{note}
104-
If you want to only build and fetch the project dependencies, you can use
109+
If you don't want to build your project, instead stopping at step 5, you can use
105110
the `@pkg-install` alias like so
106111

107112
```shell
108113
$ dune build @pkg-install
109114
```
110115

116+
This functionality can be useful to cache the installation of dependencies,
117+
somewhat similar to `opam switch create` followed by `opam install`.
118+
111119
See {doc}`/reference/aliases/pkg-install` for more information.
112120
:::
113121

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)

0 commit comments

Comments
 (0)