Skip to content

Commit 1db1f45

Browse files
authored
Dune cache: add enabled-except-user-rules setting (#10944)
Signed-off-by: Nicolás Ojeda Bär <[email protected]>
1 parent d6f1a27 commit 1db1f45

File tree

20 files changed

+97
-52
lines changed

20 files changed

+97
-52
lines changed

bin/common.ml

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
open Stdune
2-
open Dune_config
32
open Dune_config_file
43
module Console = Dune_console
54
module Graph = Dune_graph.Graph
@@ -412,12 +411,12 @@ let shared_with_config_file =
412411
let doc =
413412
Printf.sprintf
414413
"Enable or disable Dune cache (%s). Default is `%s'."
415-
(Arg.doc_alts_enum Config.Toggle.all)
416-
(Config.Toggle.to_string Dune_config.default.cache_enabled)
414+
(Arg.doc_alts_enum Dune_config.Cache.Toggle.all)
415+
(Dune_config.Cache.Toggle.to_string Dune_config.default.cache_enabled)
417416
in
418417
Arg.(
419418
value
420-
& opt (some (enum Config.Toggle.all)) None
419+
& opt (some (enum Dune_config.Cache.Toggle.all)) None
421420
& info [ "cache" ] ~docs ~env:(Cmd.Env.info ~doc "DUNE_CACHE") ~doc)
422421
and+ cache_storage_mode =
423422
let doc =
@@ -584,7 +583,6 @@ module Builder = struct
584583
; file_watcher : Dune_engine.Scheduler.Run.file_watcher
585584
; workspace_config : Dune_rules.Workspace.Clflags.t
586585
; cache_debug_flags : Dune_engine.Cache_debug_flags.t
587-
; cache_rules_default : bool
588586
; report_errors_config : Dune_engine.Report_errors_config.t
589587
; separate_error_messages : bool
590588
; stop_on_first_error : bool
@@ -936,20 +934,6 @@ module Builder = struct
936934
useful for Dune developers to make Dune tests of the digest cache more \
937935
reproducible.")
938936
and+ cache_debug_flags = cache_debug_flags_term
939-
and+ cache_rules_default =
940-
let default =
941-
Dune_lang.Toggle.of_bool !Dune_engine.Clflags.can_go_in_shared_cache_default
942-
in
943-
let doc =
944-
Printf.sprintf
945-
"Enable or disable caching rules (%s). Default is `%s'."
946-
(Arg.doc_alts_enum Config.Toggle.all)
947-
(Config.Toggle.to_string default)
948-
in
949-
Arg.(
950-
value
951-
& opt (enum Config.Toggle.all) default
952-
& info [ "cache-rules" ] ~docs ~env:(Cmd.Env.info ~doc "DUNE_CACHE_RULES") ~doc)
953937
and+ report_errors_config =
954938
Arg.(
955939
value
@@ -1026,7 +1010,6 @@ module Builder = struct
10261010
; config_from_config_file
10271011
}
10281012
; cache_debug_flags
1029-
; cache_rules_default = Dune_lang.Toggle.enabled cache_rules_default
10301013
; report_errors_config
10311014
; separate_error_messages
10321015
; stop_on_first_error
@@ -1236,14 +1219,18 @@ let init (builder : Builder.t) =
12361219
Dune_rules.Global.init ~capture_outputs:c.builder.capture_outputs;
12371220
let cache_config =
12381221
match config.cache_enabled with
1239-
| `Disabled -> Dune_cache.Config.Disabled
1240-
| `Enabled ->
1222+
| Disabled -> Dune_cache.Config.Disabled
1223+
| Enabled_except_user_rules | Enabled ->
12411224
Enabled
12421225
{ storage_mode = Option.value config.cache_storage_mode ~default:Hardlink
12431226
; reproducibility_check = config.cache_reproducibility_check
12441227
}
12451228
in
1246-
Log.info [ Pp.textf "Shared cache: %s" (Config.Toggle.to_string config.cache_enabled) ];
1229+
Log.info
1230+
[ Pp.textf
1231+
"Shared cache: %s"
1232+
(Dune_config.Cache.Toggle.to_string config.cache_enabled)
1233+
];
12471234
Log.info
12481235
[ Pp.textf
12491236
"Shared cache location: %s"
@@ -1277,7 +1264,10 @@ let init (builder : Builder.t) =
12771264
Dune_rules.Clflags.ignore_lock_dir := c.builder.ignore_lock_dir;
12781265
Dune_rules.Clflags.on_missing_dune_project_file
12791266
:= if c.builder.require_dune_project_file then Error else Warn;
1280-
Dune_engine.Clflags.can_go_in_shared_cache_default := c.builder.cache_rules_default;
1267+
(Dune_engine.Clflags.can_go_in_shared_cache_default
1268+
:= match config.cache_enabled with
1269+
| Disabled | Enabled_except_user_rules -> false
1270+
| Enabled -> true);
12811271
Log.info
12821272
[ Pp.textf
12831273
"Workspace root: %s"

doc/caching.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ or simply undoing some changes within the same workspace.
1515
Configuration
1616
=============
1717

18-
For now, Dune cache is an opt-in feature. There are three ways to enable it.
19-
Choose the one that is more convenient for you:
18+
There are three ways to configure the Dune cache. Choose the one that is more
19+
convenient for you:
2020

21-
* Add ``(cache enabled)`` to your Dune configuration file
21+
* Add ``(cache <setting>)`` to your Dune configuration file
2222
(``~/.config/dune/config`` by default).
23-
* Set the environment variable ``DUNE_CACHE`` to ``enabled``
24-
* Run Dune with the ``--cache=enabled`` flag.
23+
* Set the environment variable ``DUNE_CACHE`` to ``<setting>``
24+
* Run Dune with the ``--cache=<setting>`` flag.
25+
26+
Here, ``<setting>`` must be one of:
27+
28+
* ``disabled``: disables the Dune cache completely.
29+
30+
* ``enabled-except-user-rules``: enables the Dune cache, but excludes
31+
user-written rules. This setting is a conservative choice that can avoid
32+
breaking rules whose dependencies are not correctly specified. Currently the
33+
default.
34+
35+
* ``enabled``: enables the Dune cache unconditionally.
2536

2637
By default, Dune stores the cache in your ``XDG_CACHE_HOME`` directory on \*nix
2738
systems and ``%LOCALAPPDATA%\Microsoft\Windows\Temporary Internet Files\dune`` on Windows.

doc/changes/10944.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- A new Dune cache setting: `enabled-except-user-rules`, which enables the Dune
2+
cache, but excludes user-written rules from it. This is a conservative choice
3+
that can avoid breaking rules whose dependencies are not correctly
4+
specified. This is the current default. (#10944, @nojb)

doc/reference/config/cache.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ where ``<setting>`` is one of:
1212

1313
- ``enabled`` enables Dune cache.
1414

15+
- ``enabled-except-user-rules`` enables the Dune cache, but exclude user-written
16+
rules. This setting is a conservative choice that can avoid breaking rules
17+
whose dependencies are not correctly specified. Currently the default.
18+
1519
- ``disabled`` disables Dune cache.

src/dune_config_file/dune_config_file.ml

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ module Dune_config = struct
111111
end
112112

113113
module Cache = struct
114+
module Toggle = struct
115+
type t =
116+
| Disabled
117+
| Enabled_except_user_rules
118+
| Enabled
119+
120+
let to_string = function
121+
| Disabled -> "disabled"
122+
| Enabled_except_user_rules -> "enabled-except-user-rules"
123+
| Enabled -> "enabled"
124+
;;
125+
126+
let all =
127+
List.map
128+
~f:(fun x -> to_string x, x)
129+
[ Disabled; Enabled_except_user_rules; Enabled ]
130+
;;
131+
132+
let decode ~check =
133+
let open Dune_lang.Decoder in
134+
enum'
135+
[ to_string Disabled, return Disabled
136+
; to_string Enabled, return Enabled
137+
; ( to_string Enabled_except_user_rules
138+
, check (3, 17) >>> return Enabled_except_user_rules )
139+
]
140+
;;
141+
142+
let to_dyn = function
143+
| Disabled -> Dyn.variant "Disabed" []
144+
| Enabled_except_user_rules -> Dyn.variant "Enabled_except_user_rules" []
145+
| Enabled -> Dyn.variant "Enabled" []
146+
;;
147+
end
148+
114149
module Transport_deprecated = struct
115150
type t =
116151
| Daemon
@@ -153,7 +188,7 @@ module Dune_config = struct
153188
; concurrency : Concurrency.t field
154189
; terminal_persistence : Terminal_persistence.t field
155190
; sandboxing_preference : Sandboxing_preference.t field
156-
; cache_enabled : Config.Toggle.t field
191+
; cache_enabled : Cache.Toggle.t field
157192
; cache_reproducibility_check : Dune_cache.Config.Reproducibility_check.t field
158193
; cache_storage_mode : Cache.Storage_mode.t field
159194
; action_stdout_on_success : Action_output_on_success.t field
@@ -220,7 +255,7 @@ module Dune_config = struct
220255
; "terminal_persistence", field Terminal_persistence.to_dyn terminal_persistence
221256
; ( "sandboxing_preference"
222257
, field (Dyn.list Sandbox_mode.to_dyn) sandboxing_preference )
223-
; "cache_enabled", field Config.Toggle.to_dyn cache_enabled
258+
; "cache_enabled", field Cache.Toggle.to_dyn cache_enabled
224259
; ( "cache_reproducibility_check"
225260
, field
226261
Dune_cache.Config.Reproducibility_check.to_dyn
@@ -316,7 +351,7 @@ module Dune_config = struct
316351
; concurrency = (if Execution_env.inside_dune then Fixed 1 else Auto)
317352
; terminal_persistence = Clear_on_rebuild
318353
; sandboxing_preference = []
319-
; cache_enabled = `Enabled
354+
; cache_enabled = Enabled_except_user_rules
320355
; cache_reproducibility_check = Skip
321356
; cache_storage_mode = Some (Dune_cache_storage.Mode.default ())
322357
; action_stdout_on_success = Print
@@ -342,7 +377,7 @@ module Dune_config = struct
342377
field_o "terminal-persistence" (1, 0) Terminal_persistence.decode
343378
and+ sandboxing_preference =
344379
field_o "sandboxing_preference" (1, 0) Sandboxing_preference.decode
345-
and+ cache_enabled = field_o "cache" (2, 0) (enum Config.Toggle.all)
380+
and+ cache_enabled = field_o "cache" (2, 0) (Cache.Toggle.decode ~check)
346381
and+ _cache_transport_unused_since_3_0 =
347382
field_o
348383
"cache-transport"

src/dune_config_file/dune_config_file.mli

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Dune_config : sig
22
(** Dune configuration (visible to the user) *)
33

44
open Stdune
5-
open Dune_config
65
module Display : module type of Display
76

87
module Project_defaults : sig
@@ -29,6 +28,21 @@ module Dune_config : sig
2928
end
3029

3130
module Cache : sig
31+
module Toggle : sig
32+
type t =
33+
| Disabled
34+
| Enabled_except_user_rules
35+
| Enabled
36+
37+
val all : (string * t) list
38+
39+
val decode
40+
: check:(Dune_lang.Syntax.Version.t -> unit Dune_lang.Decoder.t)
41+
-> t Dune_lang.Decoder.t
42+
43+
val to_string : t -> string
44+
end
45+
3246
module Storage_mode : sig
3347
type t = Dune_cache_storage.Mode.t option
3448

@@ -61,7 +75,7 @@ module Dune_config : sig
6175
; concurrency : Concurrency.t field
6276
; terminal_persistence : Terminal_persistence.t field
6377
; sandboxing_preference : Sandboxing_preference.t field
64-
; cache_enabled : Config.Toggle.t field
78+
; cache_enabled : Cache.Toggle.t field
6579
; cache_reproducibility_check : Dune_cache.Config.Reproducibility_check.t field
6680
; cache_storage_mode : Cache.Storage_mode.t field
6781
; action_stdout_on_success : Action_output_on_success.t field

test/blackbox-tests/test-cases/directory-targets/cache-file-and-dir.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ This checks what happens when a file available in the cache is used in a directo
22

33
$ export DUNE_CACHE_ROOT=$PWD/.cache
44
$ export DUNE_CACHE=enabled
5-
$ export DUNE_CACHE_RULES=enabled
65
$ . ./helpers.sh
76

87
$ cat > dune-project << EOF

test/blackbox-tests/test-cases/directory-targets/cache-shared-subdir.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
We create 2 directory targets which share a whole subdirectory.
22

33
$ export DUNE_CACHE_ROOT=$PWD/.cache
4-
$ export DUNE_CACHE_RULES=enabled
54
$ export DUNE_CACHE=enabled
65
$ . ./helpers.sh
76

test/blackbox-tests/test-cases/directory-targets/cache.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
We test that directory targets can go in the shared cache. See #8067.
22

33
$ export DUNE_CACHE_ROOT=$PWD/.cache
4-
$ export DUNE_CACHE_RULES=enabled
54
$ export DUNE_CACHE=enabled
65

76
In project a, we create a rule with a directory target. The script that creates

test/blackbox-tests/test-cases/dune-cache/config.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Check that old cache configuration format works fine with an old language
2626
Test that DUNE_CACHE_ROOT can be used to control the cache location
2727

2828
$ export DUNE_CACHE_ROOT=$PWD/.cache
29-
$ export DUNE_CACHE_RULES=enabled
3029

3130
Build succeeds and the 'copy' mode is respected
3231

0 commit comments

Comments
 (0)