Skip to content

Commit bc2e0e1

Browse files
cknittclaude
andauthored
Keep the caller's spelling of the project root in OCaml compiler-args (#8867)
* Keep the caller's spelling of the project root in OCaml compiler-args Rust rewatch does since #8639, and its new GenType test checks it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel <ck@cca.io> * Spell compiler-args' project root with backslashes on Windows, as Rust does Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel <ck@cca.io> * Tolerate a build that stopped before the second interrupt in run.sh Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f732de2 commit bc2e0e1

7 files changed

Lines changed: 52 additions & 15 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- OCaml rewatch: `compiler-args` keeps the project root spelled as the caller named the source, as Rust rewatch does since #8639. https://github.com/rescript-lang/rescript/pull/8867

‎rewatch-ocaml/build_preparation.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ let run ~(root_config : Config.t) ~prod ~features ~warn_error ~filter ~watch
242242
|> fun (regular, development) -> (List.rev regular, List.rev development)
243243
in
244244
let common_args dependency_dirs =
245-
Compiler_args.compiler_common_arguments ~config:package.compile_config
246-
~runtime ~dependency_dirs ~watch
245+
Compiler_args.compiler_common_arguments
246+
~project_root:package.compile_config.root
247+
~config:package.compile_config ~runtime ~dependency_dirs ~watch
247248
~gentype_dependency_args:package.gentype_dependency_args
248249
in
249250
Hashtbl.add packages package.root

‎rewatch-ocaml/compiler_args.ml‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ let parser_arguments ~(config : Config.t) ~contents ~path =
102102
path;
103103
]
104104

105-
let compiler_common_arguments ~(config : Config.t) ~runtime ~dependency_dirs
106-
~watch ~gentype_dependency_args =
105+
let compiler_common_arguments ~project_root ~(config : Config.t) ~runtime
106+
~dependency_dirs ~watch ~gentype_dependency_args =
107107
["-I"; Filename.concat Filename.parent_dir_name "ocaml"]
108108
@ ["-runtime-path"; runtime]
109109
@ List.concat_map (fun directory -> ["-I"; directory]) dependency_dirs
110110
@ compiler_flags ~watch config
111111
@ gentype_dependency_args
112-
@ ["-bs-package-name"; config.name; "-bs-project-root"; config.root]
112+
@ ["-bs-package-name"; config.name; "-bs-project-root"; project_root]
113113

114114
let compiler_arguments_with_common ~(config : Config.t) ~common_args
115115
~module_name ~source_kind ~has_interface ~path =
@@ -130,11 +130,11 @@ let compiler_arguments_with_common ~(config : Config.t) ~common_args
130130
@ interface_args @ common_args @ output_args
131131
@ [Source.ast_path path]
132132

133-
let compiler_arguments ~(config : Config.t) ~runtime ~dependency_dirs
134-
~module_name ~source_kind ~has_interface ~watch ~gentype_dependency_args
135-
~path =
133+
let compiler_arguments ~project_root ~(config : Config.t) ~runtime
134+
~dependency_dirs ~module_name ~source_kind ~has_interface ~watch
135+
~gentype_dependency_args ~path =
136136
compiler_arguments_with_common ~config
137137
~common_args:
138-
(compiler_common_arguments ~config ~runtime ~dependency_dirs ~watch
139-
~gentype_dependency_args)
138+
(compiler_common_arguments ~project_root ~config ~runtime ~dependency_dirs
139+
~watch ~gentype_dependency_args)
140140
~module_name ~source_kind ~has_interface ~path

‎rewatch-ocaml/compiler_args.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ val parser_arguments :
1717
config:Config.t -> contents:string -> path:string -> string list
1818

1919
val compiler_arguments :
20+
project_root:string ->
2021
config:Config.t ->
2122
runtime:string ->
2223
dependency_dirs:string list ->
@@ -29,6 +30,7 @@ val compiler_arguments :
2930
string list
3031

3132
val compiler_common_arguments :
33+
project_root:string ->
3234
config:Config.t ->
3335
runtime:string ->
3436
dependency_dirs:string list ->

‎rewatch-ocaml/compiler_args_command.ml‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ let runtime_path resolution package_root =
1010
let source_error path message =
1111
error (Printf.sprintf "Could not read source file %s: %s" path message)
1212

13+
(* An absolute path with "." and ".." resolved textually, without following
14+
symlinks, like Rust's [to_lexical_absolute]. Rust rebuilds the path from
15+
its components, so on Windows every separator becomes a backslash, also
16+
after the drive of a path such as [D:/project]. *)
17+
let lexical_absolute path =
18+
let path =
19+
if Filename.is_relative path then Filename.concat (Sys.getcwd ()) path
20+
else path
21+
in
22+
let rec normalize path =
23+
let directory = Filename.dirname path in
24+
let base = Filename.basename path in
25+
if directory = path then path
26+
else if base = Filename.current_dir_name then normalize directory
27+
else if base = Filename.parent_dir_name then
28+
Filename.dirname (normalize directory)
29+
else Filename.concat (normalize directory) base
30+
in
31+
let path = normalize path in
32+
if Sys.win32 then String.map (fun c -> if c = '/' then '\\' else c) path
33+
else path
34+
1335
let run ~verbosity path =
1436
let source =
1537
try
@@ -79,8 +101,17 @@ let run ~verbosity path =
79101
dependency.name config.name))
80102
in
81103
let runtime = runtime_path resolution config.root in
104+
(* As in Rust rewatch, editors get the project root spelled the way they
105+
named the source, for example through a symlink or a Windows short path:
106+
they compile the incremental copy of the source under that root. *)
107+
let project_root =
108+
Project_context.nearest_config_path
109+
(Filename.dirname (lexical_absolute path))
110+
|> Option.fold ~none:config.root ~some:Filename.dirname
111+
in
82112
let compiler_args =
83-
Compiler_args.compiler_arguments ~config ~runtime ~dependency_dirs
113+
Compiler_args.compiler_arguments ~project_root ~config ~runtime
114+
~dependency_dirs
84115
~module_name:(Source.module_name source)
85116
~source_kind ~has_interface ~watch:false ~gentype_dependency_args:[]
86117
~path:relative

‎rewatch-ocaml/tests/run.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,9 +1740,10 @@ if ! $windows_posix_shell; then
17401740
exit 1
17411741
fi
17421742
kill -INT "$second_interrupt_pid"
1743-
# The forwarded copy arrives a little later, as a separate signal.
1743+
# The forwarded copy arrives a little later, as a separate signal. On a fast
1744+
# machine the build may have stopped already; the checks below still apply.
17441745
sleep 0.2
1745-
kill -INT "$second_interrupt_pid"
1746+
kill -INT "$second_interrupt_pid" 2>/dev/null || true
17461747
if ! wait_for_pid_gone "$second_interrupt_pid" 600; then
17471748
echo "an interrupted in-process compile did not stop" >&2
17481749
exit 1

‎tests/rewatch_ounit_tests/config_tests.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ let compiler_job_tests =
289289
let compile_job =
290290
Compiler_process.compile_request ~build_dir ~config
291291
~common_args:
292-
(Compiler_args.compiler_common_arguments ~config ~runtime:"runtime"
293-
~dependency_dirs:[] ~watch:false ~gentype_dependency_args:[])
292+
(Compiler_args.compiler_common_arguments ~project_root:config.root
293+
~config ~runtime:"runtime" ~dependency_dirs:[] ~watch:false
294+
~gentype_dependency_args:[])
294295
module_ ~source_kind:Source.Implementation "src/A.res"
295296
in
296297
let rec argument_after expected = function

0 commit comments

Comments
 (0)