Skip to content

Commit 2c531a3

Browse files
committed
Display a warning when dune builds via RPC
When an instance of dune is already running, invoking `dune build` will still build the target, howwever other command line arguments are silently ignored by dune as the current RPC interface is too limited to express them. This may change over time, but currently it's unusual UX for `dune build` to behave differently depending on whether or not another instance of `dune build` is already running. To more accurately set user expectations, print a warning when dune is running with this reduced functionality. Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
1 parent 5802ce5 commit 2c531a3

5 files changed

Lines changed: 61 additions & 9 deletions

File tree

bin/build_cmd.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ let build =
207207
state of the lock could otherwise change between checking it and taking
208208
it. *)
209209
match Dune_util.Global_lock.lock ~timeout:None with
210-
| Error () ->
210+
| Error lock_held_by ->
211211
(* This case is reached if dune detects that another instance of dune
212212
is already running. Rather than performing the build itself, the
213213
current instance of dune will instruct the already-running instance to
@@ -217,6 +217,16 @@ let build =
217217
perform the RPC call.
218218
*)
219219
Scheduler.go_without_rpc_server ~common ~config (fun () ->
220+
if not (Common.Builder.equal builder Common.Builder.default)
221+
then
222+
User_warning.emit
223+
[ Pp.textf
224+
"Your build request is being forwarded to a running Dune instance%s. \
225+
Note that certain command line arguments may be ignored."
226+
(match (lock_held_by : Dune_util.Global_lock.Lock_held_by.t) with
227+
| Unknown -> ""
228+
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
229+
];
220230
build_via_rpc_server targets)
221231
| Ok () ->
222232
let request setup =

doc/changes/11836.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Print a warning when `dune build` runs over RPC (#11836, @gridbugs)

src/dune_util/global_lock.ml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ end
6666

6767
let locked = ref false
6868

69+
module Lock_held_by = struct
70+
type t =
71+
| Pid_from_lockfile of int
72+
| Unknown
73+
74+
let read_lock_file () =
75+
match Io.read_file (Path.build lock_file) with
76+
| exception _ -> Unknown
77+
| pid ->
78+
(match int_of_string_opt pid with
79+
| Some pid -> Pid_from_lockfile pid
80+
| None ->
81+
User_error.raise
82+
[ Pp.textf
83+
"Unexpected contents of build directory global lock file (%s). Expected \
84+
an integer PID. Found: %s"
85+
(Path.Build.to_string_maybe_quoted lock_file)
86+
pid
87+
]
88+
~hints:
89+
[ Pp.textf "Try deleting %s" (Path.Build.to_string_maybe_quoted lock_file) ])
90+
;;
91+
end
92+
6993
let lock ~timeout =
7094
match Config.(get global_lock) with
7195
| `Disabled -> Ok ()
@@ -90,20 +114,22 @@ let lock ~timeout =
90114
| `Success ->
91115
locked := true;
92116
Ok ()
93-
| `Failure -> Error ())
117+
| `Failure ->
118+
let lock_held_by = Lock_held_by.read_lock_file () in
119+
Error lock_held_by)
94120
;;
95121

96122
let lock_exn ~timeout =
97123
match lock ~timeout with
98124
| Ok () -> ()
99-
| Error () ->
125+
| Error lock_held_by ->
100126
User_error.raise
101127
[ Pp.textf
102128
"A running dune%s instance has locked the build directory. If this is not the \
103-
case, please delete %s"
104-
(match Io.read_file (Path.build lock_file) with
105-
| exception _ -> ""
106-
| pid -> sprintf " (pid: %s)" pid)
129+
case, please delete %S."
130+
(match lock_held_by with
131+
| Unknown -> ""
132+
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
107133
(Path.Build.to_string_maybe_quoted lock_file)
108134
]
109135
;;

src/dune_util/global_lock.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
33
Before starting rpc, writing to the build dir, this lock should be locked. *)
44

5-
(** attempt to acquire a lock. once a lock is locked, subsequent locks always
5+
module Lock_held_by : sig
6+
type t =
7+
| Pid_from_lockfile of int
8+
| Unknown
9+
end
10+
11+
(** Attempt to acquire a lock. once a lock is locked, subsequent locks always
612
succeed. Returns [Ok ()] if the lock is acquired within [timeout] seconds,
713
and [Error ()] otherwise. *)
8-
val lock : timeout:float option -> (unit, unit) result
14+
val lock : timeout:float option -> (unit, Lock_held_by.t) result
915

1016
val lock_exn : timeout:float option -> unit
1117

test/blackbox-tests/test-cases/watching/watching-eager-concurrent-build-command.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Build the project once before starting the watch server so the watch server star
99
$ dune build --watch &
1010
Success, waiting for filesystem changes...
1111
Success, waiting for filesystem changes...
12+
Success, waiting for filesystem changes...
1213
File "foo.ml", line 1, characters 9-21:
1314
1 | let () = print_endlin "Hello, World!"
1415
^^^^^^^^^^^^
@@ -26,6 +27,13 @@ Demonstrate that we can run "dune build" while the watch server is running.
2627
$ dune build
2728
Success
2829

30+
Demonstrate that a warning is displayed when extra arguments are passed to
31+
"dune build", since those arguments will be ignored.
32+
$ dune build --auto-promote 2>&1 | sed 's/pid: [0-9]*/pid: PID/g'
33+
Warning: Your build request is being forwarded to a running Dune instance
34+
(pid: PID). Note that certain command line arguments may be ignored.
35+
Success
36+
2937
Demonstrate that error messages are still printed by "dune build" when it's
3038
acting as an RPC client while running concurrently with an RPC server.
3139
$ echo 'let () = print_endlin "Hello, World!"' > foo.ml
@@ -40,3 +48,4 @@ acting as an RPC client while running concurrently with an RPC server.
4048

4149
$ dune shutdown
4250
$ wait
51+

0 commit comments

Comments
 (0)