Skip to content

Commit 3f824a4

Browse files
committed
Revert "Delay opening redirected files until execing cmd (ocaml#1635)"
This reverts commit 59c4daa. Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 70f2e69 commit 3f824a4

File tree

12 files changed

+69
-45
lines changed

12 files changed

+69
-45
lines changed

CHANGES.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ unreleased
1515
- Fix preprocessing for libraries with `(include_subdirs ..)` (#1624, fix #1626,
1616
@nojb, @rgrinberg)
1717

18-
- Delay opening redirected output files until executing commands in
19-
order to reduce the number of maximum number of open file
20-
descriptors (#1635, fixes #1633, @jonludlam)
21-
2218
- Do not generate targets for archive that don't match the `modes` field.
2319
(#1632, fix #1617, @rgrinberg)
2420

src/action_exec.ml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ type exec_context =
88
}
99

1010
let get_std_output : _ -> Process.std_output_to = function
11-
| None -> Terminal
12-
| Some fn -> File fn
11+
| None -> Terminal
12+
| Some (fn, oc) ->
13+
Opened_file { filename = fn
14+
; tail = false
15+
; desc = Channel oc }
16+
1317

1418
let exec_run_direct ~ectx ~dir ~env ~stdout_to ~stderr_to prog args =
1519
begin match ectx.context with
@@ -40,7 +44,7 @@ let exec_echo stdout_to str =
4044
Fiber.return
4145
(match stdout_to with
4246
| None -> print_string str; flush stdout
43-
| Some fn -> Io.write_file fn str)
47+
| Some (_, oc) -> output_string oc str)
4448

4549
let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
4650
match (t : Action.t) with
@@ -56,6 +60,15 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
5660
| Redirect (Stdout, fn, Echo s) ->
5761
Io.write_file fn (String.concat s ~sep:" ");
5862
Fiber.return ()
63+
| Redirect (outputs, fn, Run (Ok prog, args)) ->
64+
let out = Process.File fn in
65+
let stdout_to, stderr_to =
66+
match outputs with
67+
| Stdout -> (out, get_std_output stderr_to)
68+
| Stderr -> (get_std_output stdout_to, out)
69+
| Outputs -> (out, out)
70+
in
71+
exec_run_direct ~ectx ~dir ~env ~stdout_to ~stderr_to prog args
5972
| Redirect (outputs, fn, t) ->
6073
redirect ~ectx ~dir outputs fn t ~env ~stdout_to ~stderr_to
6174
| Ignore (outputs, t) ->
@@ -65,9 +78,12 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
6578
| Echo strs -> exec_echo stdout_to (String.concat strs ~sep:" ")
6679
| Cat fn ->
6780
Io.with_file_in fn ~f:(fun ic ->
68-
match stdout_to with
69-
| None -> Io.copy_channels ic stdout
70-
| Some fn -> Io.with_file_out fn ~f:(fun oc -> Io.copy_channels ic oc));
81+
let oc =
82+
match stdout_to with
83+
| None -> stdout
84+
| Some (_, oc) -> oc
85+
in
86+
Io.copy_channels ic oc);
7187
Fiber.return ()
7288
| Copy (src, dst) ->
7389
Io.copy_file ~src ~dst ();
@@ -179,16 +195,16 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
179195
Fiber.return ()
180196

181197
and redirect outputs fn t ~ectx ~dir ~env ~stdout_to ~stderr_to =
182-
(* We resolve the path to an absolute one here to ensure no
183-
Chdir actions change the eventual path of the file *)
184-
let out = Some (Path.to_absolute fn) in
198+
let oc = Io.open_out fn in
199+
let out = Some (fn, oc) in
185200
let stdout_to, stderr_to =
186201
match outputs with
187202
| Stdout -> (out, stderr_to)
188203
| Stderr -> (stdout_to, out)
189204
| Outputs -> (out, out)
190205
in
191-
exec t ~ectx ~dir ~env ~stdout_to ~stderr_to
206+
exec t ~ectx ~dir ~env ~stdout_to ~stderr_to >>| fun () ->
207+
close_out oc
192208

193209
and exec_list l ~ectx ~dir ~env ~stdout_to ~stderr_to =
194210
match l with

src/build.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ let symlink ~src ~dst =
249249
action ~targets:[dst] (Symlink (src, dst))
250250

251251
let create_file fn =
252-
action ~targets:[fn] (Redirect (Stdout, fn, Echo []))
252+
action ~targets:[fn] (Redirect (Stdout, fn, Progn []))
253253

254254
let remove_tree dir =
255255
arr (fun _ -> Action.Remove_tree dir)

src/process.ml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ let map_result
3333
type std_output_to =
3434
| Terminal
3535
| File of Path.t
36+
| Opened_file of opened_file
37+
38+
and opened_file =
39+
{ filename : Path.t
40+
; desc : opened_file_desc
41+
; tail : bool
42+
}
43+
44+
and opened_file_desc =
45+
| Fd of Unix.file_descr
46+
| Channel of out_channel
3647

3748
type purpose =
3849
| Internal_job
@@ -114,18 +125,19 @@ module Fancy = struct
114125
| Some dir -> sprintf "(cd %s && %s)" (Path.to_string dir) s
115126
in
116127
match stdout_to, stderr_to with
117-
| File fn1, File fn2 when Path.equal fn1 fn2 ->
128+
| (File fn1 | Opened_file { filename = fn1; _ }),
129+
(File fn2 | Opened_file { filename = fn2; _ }) when Path.equal fn1 fn2 ->
118130
sprintf "%s &> %s" s (Path.to_string fn1)
119131
| _ ->
120132
let s =
121133
match stdout_to with
122134
| Terminal -> s
123-
| File fn ->
135+
| File fn | Opened_file { filename = fn; _ } ->
124136
sprintf "%s > %s" s (Path.to_string fn)
125137
in
126138
match stderr_to with
127139
| Terminal -> s
128-
| File fn ->
140+
| File fn | Opened_file { filename = fn; _ } ->
129141
sprintf "%s 2> %s" s (Path.to_string fn)
130142

131143
let pp_purpose ppf = function
@@ -184,11 +196,19 @@ let get_std_output ~default = function
184196
| File fn ->
185197
let fd = Unix.openfile (Path.to_string fn)
186198
[O_WRONLY; O_CREAT; O_TRUNC; O_SHARE_DELETE] 0o666 in
187-
(fd, Some fd)
199+
(fd, Some (Fd fd))
200+
| Opened_file { desc; tail; _ } ->
201+
let fd =
202+
match desc with
203+
| Fd fd -> fd
204+
| Channel oc -> flush oc; Unix.descr_of_out_channel oc
205+
in
206+
(fd, Option.some_if tail desc)
188207

189208
let close_std_output = function
190209
| None -> ()
191-
| Some fd -> Unix.close fd
210+
| Some (Fd fd) -> Unix.close fd
211+
| Some (Channel oc) -> close_out oc
192212

193213
let gen_id =
194214
let next = ref (-1) in

src/process.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ type ('a, 'b) failure_mode =
1818
type std_output_to =
1919
| Terminal
2020
| File of Path.t
21+
| Opened_file of opened_file
22+
23+
and opened_file =
24+
{ filename : Path.t
25+
; desc : opened_file_desc
26+
; tail : bool
27+
(** If [true], the descriptor is closed after starting the command *)
28+
}
29+
30+
and opened_file_desc =
31+
| Fd of Unix.file_descr
32+
| Channel of out_channel
2133

2234
(** Why a Fiber.t was run *)
2335
type purpose =

src/stdune/path.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,6 @@ let of_filename_relative_to_initial_cwd fn =
643643

644644
let to_absolute_filename t = Kind.to_absolute_filename (kind t)
645645

646-
let to_absolute t = external_ (External.of_string (to_absolute_filename t))
647-
648646
let external_of_local x ~root =
649647
External.to_string (External.relative root (Local.to_string x))
650648

src/stdune/path.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ val of_filename_relative_to_initial_cwd : string -> t
7676
root has been set. [root] is the root directory of local paths *)
7777
val to_absolute_filename : t -> string
7878

79-
(** Convert any path to an absolute path *)
80-
val to_absolute : t -> t
81-
8279
val reach : t -> from:t -> string
8380

8481
(** [from] defaults to [Path.root] *)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
$ dune build
2-
bar
2+
foobar

test/blackbox-tests/test-cases/enabled_if/run.t

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ This rule is disabled, trying to build a should fail:
1515

1616
This one is enabled:
1717
$ dune build b
18-
Building file bError: Rule failed to generate the following targets:
19-
- b
20-
[1]
18+
Building file b

test/blackbox-tests/test-cases/inline_tests/run.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
(echo "\n")
5252
(echo "let () = print_int 43;;")))))
5353
run alias dune-file/runtest
54-
43
54+
414243

0 commit comments

Comments
 (0)