Skip to content

Commit 23d7974

Browse files
authored
pkg: add dev tools to PATH when running dev tools (#12065)
This allows the ocamllsp dev tool to run the ocamlformat dev tool, assuming the latter is installed. Signed-off-by: Stephen Sherratt <[email protected]>
1 parent 0db629f commit 23d7974

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

bin/tools/tools_common.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
open! Import
22
module Pkg_dev_tool = Dune_rules.Pkg_dev_tool
33

4+
let add_dev_tools_to_path env =
5+
List.fold_left Pkg_dev_tool.all ~init:env ~f:(fun acc tool ->
6+
let dir = Pkg_dev_tool.exe_path tool |> Path.Build.parent_exn |> Path.build in
7+
Env_path.cons acc ~dir)
8+
;;
9+
410
let dev_tool_exe_path dev_tool = Path.build @@ Pkg_dev_tool.exe_path dev_tool
511

612
let dev_tool_build_target dev_tool =
@@ -47,7 +53,8 @@ let run_dev_tool workspace_root dev_tool ~args =
4753
~verb:"Running"
4854
~object_:(User_message.command (String.concat ~sep:" " (exe_name :: args))));
4955
Console.finish ();
50-
restore_cwd_and_execve workspace_root exe_path_string args Env.initial
56+
let env = add_dev_tools_to_path Env.initial in
57+
restore_cwd_and_execve workspace_root exe_path_string args env
5158
;;
5259

5360
let lock_build_and_run_dev_tool ~common ~config dev_tool ~args =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Test that the ocamllsp dev tool executes in an environment where other dev
2+
tools are in PATH.
3+
4+
$ . ../helpers.sh
5+
$ . ./helpers.sh
6+
7+
$ mkrepo
8+
$ mkpkg ocaml 5.2.0
9+
$ setup_ocamllsp_workspace
10+
11+
Make a fake ocamllsp package that prints out the PATH variable:
12+
$ mkpkg ocaml-lsp-server <<EOF
13+
> install: [
14+
> [ "sh" "-c" "echo '#!/bin/sh' > %{bin}%/ocamllsp" ]
15+
> [ "sh" "-c" "echo 'echo \$PATH' >> %{bin}%/ocamllsp" ]
16+
> [ "sh" "-c" "chmod a+x %{bin}%/ocamllsp" ]
17+
> ]
18+
> EOF
19+
20+
$ make_lockdir
21+
$ cat > dune.lock/ocaml.pkg <<EOF
22+
> (version 5.2.0)
23+
> EOF
24+
25+
Confirm that each dev tool's bin directory is now in PATH:
26+
$ dune tools exec ocamllsp | tr : '\n' | grep '_build/_private/default/.dev-tool'
27+
Solution for dev-tools.locks/ocaml-lsp-server:
28+
- ocaml.5.2.0
29+
- ocaml-lsp-server.0.0.1
30+
Running 'ocamllsp'
31+
$TESTCASE_ROOT/_build/_private/default/.dev-tool/earlybird/earlybird/target/bin
32+
$TESTCASE_ROOT/_build/_private/default/.dev-tool/utop/utop/target/bin
33+
$TESTCASE_ROOT/_build/_private/default/.dev-tool/ocaml-lsp-server/ocaml-lsp-server/target/bin
34+
$TESTCASE_ROOT/_build/_private/default/.dev-tool/odoc/odoc/target/bin
35+
$TESTCASE_ROOT/_build/_private/default/.dev-tool/ocamlformat/ocamlformat/target/bin
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Test that the ocamllsp dev tool can see the ocamlformat dev tool.
2+
3+
$ . ../helpers.sh
4+
$ . ./helpers.sh
5+
6+
$ mkrepo
7+
$ mkpkg ocaml 5.2.0
8+
9+
$ cat > dune-workspace <<EOF
10+
> (lang dune 3.16)
11+
> (lock_dir
12+
> (path "dev-tools.locks/ocaml-lsp-server")
13+
> (repositories mock))
14+
> (lock_dir
15+
> (path "dev-tools.locks/ocamlformat")
16+
> (repositories mock))
17+
> (lock_dir
18+
> (repositories mock))
19+
> (repository
20+
> (name mock)
21+
> (url "file://$(pwd)/mock-opam-repository"))
22+
> EOF
23+
24+
Make a fake ocamllsp package that prints out the PATH variable:
25+
$ mkpkg ocaml-lsp-server <<EOF
26+
> install: [
27+
> [ "sh" "-c" "echo '#!/bin/sh' > %{bin}%/ocamllsp" ]
28+
> [ "sh" "-c" "echo 'echo fake ocamllsp will now run fake ocamlformat:' >> %{bin}%/ocamllsp" ]
29+
> [ "sh" "-c" "echo 'ocamlformat' >> %{bin}%/ocamllsp" ]
30+
> [ "sh" "-c" "chmod a+x %{bin}%/ocamllsp" ]
31+
> ]
32+
> EOF
33+
34+
Make a fake ocamlformat
35+
$ mkpkg ocamlformat <<EOF
36+
> install: [
37+
> [ "sh" "-c" "echo '#!/bin/sh' > %{bin}%/ocamlformat" ]
38+
> [ "sh" "-c" "echo 'echo hello from fake ocamlformat' >> %{bin}%/ocamlformat" ]
39+
> [ "sh" "-c" "chmod a+x %{bin}%/ocamlformat" ]
40+
> ]
41+
> EOF
42+
43+
$ make_lockdir
44+
$ cat > dune.lock/ocaml.pkg <<EOF
45+
> (version 5.2.0)
46+
> EOF
47+
48+
$ dune tools install ocamlformat
49+
Solution for dev-tools.locks/ocamlformat:
50+
- ocamlformat.0.0.1
51+
52+
$ dune tools exec ocamllsp
53+
Solution for dev-tools.locks/ocaml-lsp-server:
54+
- ocaml.5.2.0
55+
- ocaml-lsp-server.0.0.1
56+
Running 'ocamllsp'
57+
fake ocamllsp will now run fake ocamlformat:
58+
hello from fake ocamlformat

0 commit comments

Comments
 (0)