Skip to content

Commit 36231e4

Browse files
authored
Add dune cache clear command (#8975)
Signed-off-by: Nicolás Ojeda Bär <[email protected]>
1 parent 8befd24 commit 36231e4

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

bin/cache.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,27 @@ let size =
8080
Cmd.info "size" ~doc ~man
8181
in
8282
Cmd.v info
83-
@@ let+ machine_readble =
83+
@@ let+ machine_readable =
8484
Arg.(
8585
value
8686
& flag
8787
& info [ "machine-readable" ] ~doc:"Outputs size as a plain number of bytes.")
8888
in
8989
let size = Dune_cache.Trimmer.overhead_size () in
90-
if machine_readble
90+
if machine_readable
9191
then User_message.print (User_message.make [ Pp.textf "%Ld" size ])
9292
else User_message.print (User_message.make [ Pp.textf "%s" (Bytes_unit.pp size) ])
9393
;;
9494

95+
let clear =
96+
let info =
97+
let doc = "Clear the Dune cache." in
98+
let man = [ `P "Remove any traces of the Dune cache." ] in
99+
Cmd.info "clear" ~doc ~man
100+
in
101+
Cmd.v info @@ Term.(const Dune_cache_storage.clear $ const ())
102+
;;
103+
95104
let command =
96105
let info =
97106
let doc = "Manage Dune's shared cache of build artifacts." in
@@ -104,5 +113,5 @@ let command =
104113
in
105114
Cmd.info "cache" ~doc ~man
106115
in
107-
Cmd.group info [ trim; size ]
116+
Cmd.group info [ trim; size; clear ]
108117
;;

doc/changes/8975.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Add command `dune cache clear` to completely delete all traces of the Dune
2+
cache. (#8975, @nojb)

src/dune_cache_storage/dune_cache_storage.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,24 @@ let with_temp_file ?(prefix = "dune") ~suffix f =
338338
let with_temp_dir ?(prefix = "dune") ~suffix f =
339339
Fiber_util.Temp.with_temp_dir ~parent_dir:Layout.temp_dir ~prefix ~suffix ~f
340340
;;
341+
342+
let clear () =
343+
let rm_rf path = Path.rm_rf ~allow_external:true path in
344+
let rmdir path =
345+
try Path.rmdir path with
346+
| Unix.Unix_error ((ENOENT | ENOTEMPTY), _, _) -> ()
347+
in
348+
let rm_rf_all versions dir =
349+
List.iter versions ~f:(fun version ->
350+
let dir = dir version in
351+
rm_rf dir;
352+
Option.iter ~f:rmdir (Path.parent dir))
353+
in
354+
rm_rf_all Version.Metadata.all Layout.Versioned.metadata_storage_dir;
355+
rm_rf_all Version.File.all Layout.Versioned.file_storage_dir;
356+
rm_rf_all Version.Value.all Layout.Versioned.value_storage_dir;
357+
rm_rf Layout.temp_dir;
358+
(* Do not catch errors when deleting the root directory so that they are
359+
reported to the user. *)
360+
Path.rmdir Layout.root_dir
361+
;;

src/dune_cache_storage/dune_cache_storage.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,5 @@ module Raw_value : sig
138138
-> content_digest:Digest.t
139139
-> Util.Write_result.t
140140
end
141+
142+
val clear : unit -> unit

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Here we observe the documentation for the dune cache commands.
1313
functionality soon.
1414
1515
COMMANDS
16+
clear [OPTION]…
17+
Clear the Dune cache.
18+
1619
size [--machine-readable] [OPTION]…
1720
Query the size of the Dune cache.
1821
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Test for the "dune cache clear" command.
2+
3+
$ export DUNE_CACHE=enabled
4+
$ export DUNE_CACHE_ROOT=$PWD/dune-cache
5+
6+
$ cat >dune-project <<EOF
7+
> (lang dune 3.10)
8+
> EOF
9+
10+
$ cat >dune <<EOF
11+
> (rule (with-stdout-to foo (progn)))
12+
> EOF
13+
14+
$ dune build
15+
16+
$ ls $DUNE_CACHE_ROOT | sort -u
17+
files
18+
meta
19+
temp
20+
values
21+
22+
$ dune cache clear
23+
24+
$ ! test -d $DUNE_CACHE_ROOT
25+
26+
Next let us add some extra directories/files and check that they are not deleted
27+
by mistake.
28+
29+
$ dune build
30+
31+
$ mkdir -p $DUNE_CACHE_ROOT/extra; touch $DUNE_CACHE_ROOT/extra1 $DUNE_CACHE_ROOT/extra/extra2
32+
33+
$ dune cache clear
34+
Error:
35+
rmdir($TESTCASE_ROOT/dune-cache): Directory not empty
36+
[1]
37+
38+
$ find $DUNE_CACHE_ROOT -type f | sort -u
39+
$TESTCASE_ROOT/dune-cache/extra/extra2
40+
$TESTCASE_ROOT/dune-cache/extra1

0 commit comments

Comments
 (0)