Skip to content

Commit d68be8d

Browse files
authored
pkg: command to check if project uses package management (ocaml#11997)
Some users have been using the presence of a lockdir to determine whether a project indicates that package management should be used, for example when a text editor checks whether ocamllsp should be invoked via dune (as a dev tool) or from an opam switch. This is brittle, as dune may change the way projects indicate they intend to use package management in the future. This change introduces a new command "dune pkg enabled" which communicates via its exit status whether dune detects that a project intends to use package management (a status of 0 indicates package management is enabled). If/when dune changes how a project should indicate that it should use package management, the implementation of this command will be changed to match. Signed-off-by: Stephen Sherratt <[email protected]>
1 parent 99ffae6 commit d68be8d

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

bin/pkg/pkg.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ let man =
88
;;
99

1010
let subcommands =
11-
[ Lock.command; Print_solver_env.command; Outdated.command; Validate_lock_dir.command ]
11+
[ Lock.command
12+
; Print_solver_env.command
13+
; Outdated.command
14+
; Validate_lock_dir.command
15+
; Pkg_enabled.command
16+
]
1217
;;
1318

1419
let info name =

bin/pkg/pkg_common.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ module Lock_dirs_arg = struct
169169
| All
170170
| Selected of Path.Source.t list
171171

172+
let all = All
173+
172174
let term =
173175
Common.one_of
174176
(let+ arg =

bin/pkg/pkg_common.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ module Lock_dirs_arg : sig
6262
[Lock_dirs_arg.lock_dirs_of_workspace]. *)
6363
type t
6464

65+
(** Select all lockdirs *)
66+
val all : t
67+
6568
(** [Lock_dirs_arg.term] is a command-line argument that can be used to
6669
specify the lock directories to consider. This can then be passed to
6770
[Lock_dirs_arg.lock_dirs_of_workspace].

bin/pkg/pkg_enabled.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
open Import
2+
3+
let term =
4+
let+ builder = Common.Builder.term in
5+
let common, config = Common.init builder in
6+
Scheduler.go_with_rpc_server ~common ~config (fun () ->
7+
Memo.run
8+
@@
9+
let open Memo.O in
10+
let+ workspace = Workspace.workspace () in
11+
let lock_dir_paths =
12+
Pkg_common.Lock_dirs_arg.lock_dirs_of_workspace
13+
Pkg_common.Lock_dirs_arg.all
14+
workspace
15+
in
16+
let any_lockdir_exists =
17+
List.exists lock_dir_paths ~f:(fun lock_dir_path ->
18+
Path.exists (Path.source lock_dir_path))
19+
in
20+
if any_lockdir_exists then () else exit 1)
21+
;;
22+
23+
let info =
24+
let doc =
25+
"Check if the project indicates that dune's package management features should be \
26+
enabled. Exits with 0 if package management is enabled and 1 otherwise."
27+
in
28+
Cmd.info "enabled" ~doc
29+
;;
30+
31+
let command = Cmd.v info term

bin/pkg/pkg_enabled.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open Import
2+
3+
val command : unit Cmd.t
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Exercise the "dune pkg enabled" command which checks whether package management
2+
should be used.
3+
4+
$ . ./helpers.sh
5+
6+
$ mkrepo
7+
8+
$ cat >dune-workspace <<EOF
9+
> (lang dune 3.20)
10+
> (lock_dir
11+
> (path dune.lock)
12+
> (repositories mock))
13+
> (lock_dir
14+
> (path dune.other.lock)
15+
> (repositories mock))
16+
> (repository
17+
> (name mock)
18+
> (url "file://$(pwd)/mock-opam-repository"))
19+
> EOF
20+
21+
$ cat > dune-project <<EOF
22+
> (lang dune 3.13)
23+
> (package
24+
> (allow_empty)
25+
> (name foo))
26+
> EOF
27+
28+
When no lockdir is present pkg is not enabled:
29+
$ dune pkg enabled
30+
[1]
31+
32+
When the default lockdir is present pkg is enabled:
33+
$ dune pkg lock > /dev/null 2> /dev/null
34+
35+
$ dune pkg enabled
36+
37+
$ rm -r dune.lock
38+
39+
When a non-default lockdir is present, pkg is still enabled:
40+
$ dune pkg lock dune.other.lock > /dev/null 2> /dev/null
41+
$ dune pkg enabled

0 commit comments

Comments
 (0)